65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 GDI+ 进行 Alpha 混合对话框

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.60/5 (9投票s)

2006年4月14日

viewsIcon

46930

downloadIcon

2022

使用 GDI+ 进行 Alpha 混合对话框

Sample Image - gdiplusanphablend.jpg

引言

我使用 GDI+ 大约一周了,必须说它是一个受欢迎的升级。

这是一个小例子,非常简单。我使用定时器来改变对话框的 Alpha 值。

OnPaint() 中,我使用缓冲区来改善对话框的绘制

  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);
  }

以及 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);

历史

  • 2006年4月14日:初始发布
© . All rights reserved.