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();
{
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