|
|
【窗口程序】Win32全透明Aero窗口 |
一派护法 十九级 |
|
一派护法 十九级 |
【C++代码】 #include <Dwmapi.h> #include <tchar.h> #include <Windows.h> #include <Commctrl.h>
#define TRANSPARENT_COLOR RGB(0, 0, 0)
#pragma comment(lib, "Comctl32.lib") #pragma comment(lib, "Dwmapi.lib")
#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { MARGINS margins = {-1};
switch (uMsg) { case WM_CREATE: DwmExtendFrameIntoClientArea(hWnd, &margins); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return FALSE; }
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hbrBackground = CreateSolidBrush(TRANSPARENT_COLOR); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); LoadIconMetric(NULL, IDI_APPLICATION, LIM_SMALL, &wcex.hIconSm); wcex.hInstance = hInstance; wcex.lpfnWndProc = WndProc; wcex.lpszClassName = TEXT("MainWindow"); wcex.lpszMenuName = NULL; wcex.style = NULL; RegisterClassEx(&wcex);
HWND hWnd = CreateWindow(wcex.lpszClassName, TEXT("My Window"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); if (!hWnd) return 1; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);
MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
|
一派护法 十九级 |
默认的透明色是黑色,可以用下面的方法更换成其他颜色。 【更换透明色的方法】 #include <Dwmapi.h> #include <tchar.h> #include <Windows.h> #include <Commctrl.h>
#define TRANSPARENT_COLOR RGB(122, 122, 222) // 新的透明色
#pragma comment(lib, "Comctl32.lib") #pragma comment(lib, "Dwmapi.lib")
#pragma comment(linker, "\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { MARGINS margins = {-1};
switch (uMsg) { case WM_CREATE: SetLayeredWindowAttributes(hWnd, TRANSPARENT_COLOR, 0, LWA_COLORKEY); // 设置透明色 DwmExtendFrameIntoClientArea(hWnd, &margins); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hWnd, uMsg, wParam, lParam); } return FALSE; }
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hbrBackground = CreateSolidBrush(TRANSPARENT_COLOR); // 默认用透明色填充客户区 wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION); LoadIconMetric(NULL, IDI_APPLICATION, LIM_SMALL, &wcex.hIconSm); wcex.hInstance = hInstance; wcex.lpfnWndProc = WndProc; wcex.lpszClassName = TEXT("MainWindow"); wcex.lpszMenuName = NULL; wcex.style = NULL; RegisterClassEx(&wcex);
HWND hWnd = CreateWindowEx(WS_EX_LAYERED, wcex.lpszClassName, TEXT("My Window"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL); // 添加了扩展样式WS_EX_LAYERED if (!hWnd) return 1; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd);
MSG msg; while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
|
一派护法 十九级 |
此时,如果注释掉DwmExtendFrameIntoClientArea(hWnd, &margins);的话,那么窗口背景就是完全透明的(但是不能点击窗口下面的内容)
|
一派护法 十九级 |
有一个bug:休眠后唤醒计算机,aero效果消失。
|
一派护法 十九级 |
有一个bug:休眠后唤醒计算机,aero效果消失。
所以应该加一段处理休眠后唤醒消息的程序,重新执行DwmExtendFrameIntoClientArea(hWnd, &margins);
|
|
|
|