透明GIF图片显示控件

透明窗体参见:

https://msdn.microsoft.com/en-us/library/ms997507.aspx

CreateWindowEx是需要设置属性

WS_EX_LAYERED | WS_EX_TRANSPARENT

这两个属性很重要.

绘制操作可以参照此函数做:

OnPaint函数中响应:

void GIFStatic::DrawPNG(HWND hWnd, HDC hdc, int width, int height)
{
	TCHAR *pngFile = L"C:\\Users\\dyh\\Desktop\\文档\\ico.ooopic.com.png";
	/********************************************
	*   step 1.
	*   Using Gdiplus to load the image
	********************************************/
	RECT wndRect;
	::GetWindowRect(hWnd, &wndRect);
	SIZE wndSize = { wndRect.right - wndRect.left, wndRect.bottom - wndRect.top };
	if (hdc == NULL) hdc = ::GetWindowDC(hWnd);

	HDC memDC = ::CreateCompatibleDC(hdc);
	HBITMAP memBitmap = ::CreateCompatibleBitmap(hdc, wndSize.cx, wndSize.cy);
	::SelectObject(memDC, memBitmap);

	Gdiplus::Image image(pngFile);
	Gdiplus::Graphics graphics(memDC);
	graphics.DrawImage(&image, 0, 0, wndSize.cx, wndSize.cy);
	// get screen dc 
	HDC screenDC = hdc;
	POINT ptSrc = { 0, 0 };

	/*********************************************
	*   step 3.
	*   Use UpdateLayeredWindow to Draw the Window
	*
	*********************************************/
	BLENDFUNCTION blendFunction;
	blendFunction.AlphaFormat = AC_SRC_ALPHA;
	blendFunction.BlendFlags = 0;
	blendFunction.BlendOp = AC_SRC_OVER;
	blendFunction.SourceConstantAlpha = 255;
	UpdateLayeredWindow(hWnd, screenDC, &ptSrc, &wndSize, memDC, &ptSrc, 0, &blendFunction, 2);

	::DeleteDC(screenDC);
	::DeleteDC(memDC);
	::DeleteObject(memBitmap);
}
完成此函数后需要使用MoveWindow函数将窗体移动回去.

否则窗体会移动至左上角落.

代码目录请见下面:

http://download.csdn.net/detail/ab7936573/9496109

你可能感兴趣的:(c++)