65.9K
CodeProject is changing. Read more.
Home

Alpha Blending Dialog Using GDI+

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.60/5 (9 votes)

Apr 14, 2006

viewsIcon

46622

downloadIcon

1993

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
OSZAR »