Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Alpha Blending Dialog Using GDI+

0.00/5 (No votes)
14 Apr 2006 1  
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:

  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():

 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