Click here to Skip to main content
15,895,746 members
Articles / Multimedia / GDI+

Alpha Blending Dialog Using GDI+

Rate me:
Please Sign up or sign in to vote.
2.60/5 (11 votes)
13 Apr 2006 46.3K   2K   28   3
Alpha blending dialog using GDI+
Sample Image - gdiplusanphablend.jpg

Introduction

I've been using GDI+ for about a week now, and I must say it is a welcome upgrade.

This is a small example, it's very simple. I use a timer to change the alpha value of a dialog.

In OnPaint(), I use a buffer to improve the drawing of dialog:

C++
  CPaintDC dc(this); 
  Graphics graphics(dc.m_hDC); 
  Bitmap bitmap(L"pic1.bmp");
  ImageAttributes imgatt;
  UINT width = bitmap.GetWidth();
  UINT height = bitmap.GetHeight(); 
  { // color matrix
   ColorMatrix colormatrix = {
    1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f, m_fAnpha, 0.0f,
    0.0f, 0.0f, 0.0f, 0.0f, 1.0f
   }; 
   imgatt.SetColorMatrix(&colormatrix, ColorMatrixFlagsDefault, ColorAdjustTypeBitmap);
   SolidBrush brush(Color(255, 255,255, 255));
   Bitmap bmBuffer(width, height); 
   Graphics* graphBuffer = Graphics::FromImage(&bmBuffer);
   graphBuffer->FillRectangle(&brush, Rect(0, 0, width, height));
   graphBuffer->DrawImage(
    &bitmap, 
    Rect(0, 0, width, height),
    0, 0,
    width,
    height,
    UnitPixel,
    &imgatt
   );
   graphics.DrawImage(&bmBuffer, 0, 0, width, height);
  }

And code in OnTimer():

C++
 static float incr=0.01f;
 if (m_fAnpha >= 1.0f || m_fAnpha <= 0.01f)
 {
  incr = -incr;
 }
 m_fAnpha = m_fAnpha + incr;
 InvalidateRect(NULL, FALSE);

History

  • 14th April, 2006: Initial post

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
Vietnam Vietnam
Study at Hanoi University of Technology 2001-2006

Comments and Discussions

 
GeneralMember functions, class pointers and namespaces Pin
Dr. Versaeg16-Sep-08 0:20
Dr. Versaeg16-Sep-08 0:20 
GeneralAnpha -> Alpha Pin
Jörgen Sigvardsson14-Apr-06 4:32
Jörgen Sigvardsson14-Apr-06 4:32 
GeneralRe: Anpha -> Alpha Pin
Le Thanh Cong16-Apr-06 20:16
Le Thanh Cong16-Apr-06 20:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.