- #include <windows.h>
- #include <gdiplus.h>
-
-
- ULONG_PTR gdiplusStartupToken;
-
-
- LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
-
-
- typedef BOOL(*UPDATELAYEREDWINDOWFUNCTION)(HWND,HDC,POINT*,SIZE*,HDC,POINT*,COLORREF,BLENDFUNCTION*,DWORD);
-
-
- char szClassName[ ] = "PNGDialog";
-
- int WINAPI WinMain (HINSTANCE hThisInstance,
- HINSTANCE hPrevInstance,
- LPSTR lpszArgument,
- int nCmdShow)
- {
-
-
- Gdiplus::GdiplusStartupInput gdiInput;
- Gdiplus::GdiplusStartup(&gdiplusStartupToken,&gdiInput,NULL);
-
- HWND hwnd;
- MSG messages;
- WNDCLASSEX wincl;
-
-
- wincl.hInstance = hThisInstance;
- wincl.lpszClassName = szClassName;
- wincl.lpfnWndProc = WindowProcedure;
- wincl.style = CS_DBLCLKS;
- wincl.cbSize = sizeof (WNDCLASSEX);
-
-
- wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
- wincl.lpszMenuName = NULL;
- wincl.cbClsExtra = 0;
- wincl.cbWndExtra = 0;
-
- wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
-
-
- if (!RegisterClassEx (&wincl))
- return 0;
-
-
- hwnd = CreateWindowEx (
- WS_EX_LAYERED|WS_EX_TOPMOST|WS_EX_TOOLWINDOW,
- szClassName,
- "PNGDialog Example Application",
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 500,
- 500,
- HWND_DESKTOP,
- NULL,
- hThisInstance,
- NULL
- );
-
-
- ShowWindow (hwnd, nCmdShow);
- LONG style = ::GetWindowLong(hwnd,GWL_STYLE);
- if(style&WS_CAPTION)
- style^=WS_CAPTION;
- if(style&WS_THICKFRAME)
- style^=WS_THICKFRAME;
- if(style&WS_SYSMENU)
- style^=WS_SYSMENU;
- ::SetWindowLong(hwnd,GWL_STYLE,style);
-
- style = ::GetWindowLong(hwnd,GWL_EXSTYLE);
- if(style&WS_EX_APPWINDOW)
- style^=WS_EX_APPWINDOW;
- ::SetWindowLong(hwnd,GWL_EXSTYLE,style);
-
-
-
-
-
- RECT wndRect;
- ::GetWindowRect(hwnd,&wndRect);
- SIZE wndSize = {wndRect.right-wndRect.left,wndRect.bottom-wndRect.top};
- HDC hdc = ::GetDC(hwnd);
- HDC memDC = ::CreateCompatibleDC(hdc);
- HBITMAP memBitmap = ::CreateCompatibleBitmap(hdc,wndSize.cx,wndSize.cy);
- ::SelectObject(memDC,memBitmap);
-
- Gdiplus::Image image(L"pic.png");
- Gdiplus::Graphics graphics(memDC);
- graphics.DrawImage(&image,0,0,wndSize.cx,wndSize.cy);
-
-
-
-
-
- HMODULE hUser32 = ::LoadLibrary("User32.dll");
- if(!hUser32)
- {
- return FALSE;
- }
- UPDATELAYEREDWINDOWFUNCTION UpdateLayeredWindow = (UPDATELAYEREDWINDOWFUNCTION)::GetProcAddress(hUser32,"UpdateLayeredWindow");
- if(!UpdateLayeredWindow)
- {
- return FALSE;
- }
-
- HDC screenDC = GetDC(NULL);
- POINT ptSrc = {0,0};
-
-
-
-
-
-
- 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(memDC);
- ::DeleteObject(memBitmap);
-
-
- while (GetMessage (&messages, NULL, 0, 0))
- {
-
- TranslateMessage(&messages);
-
- DispatchMessage(&messages);
- }
- Gdiplus::GdiplusShutdown(gdiplusStartupToken);
-
- return messages.wParam;
- }
-
-
-
-
- LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_DESTROY:
- PostQuitMessage (0);
- break;
- case WM_LBUTTONDOWN:
-
- break;
- default:
- return DefWindowProc (hwnd, message, wParam, lParam);
- }
- return 0;
- }
本文出自 “冰狐浪子的博客” 博客,请务必保留此出处http://bhlzlx.blog.51cto.com/3389283/949818
纯win32实现PNG图片透明窗体,布布扣,bubuko.com
纯win32实现PNG图片透明窗体
原文:http://www.cnblogs.com/lidabo/p/3701249.html