透明窗口,连鼠标键盘事件也不接受,全透明

windows的透明窗口,如果设置了LWA_ALPHA和WS_EX_TRANSPARENT,就会连鼠标键盘事件也给透明过去,这个功能还不错。可以在屏上显示水印。

关键代码:

SetWindowLong(m_hWnd, GWL_EXSTYLE, WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_TOPMOST);
 SetLayeredWindowAttributes(m_hWnd, RGB(0,0,0), 120, LWA_ALPHA);
 SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW);

demo代码:

static LRESULT WINAPI UIWndProcBase(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
 if (msg == WM_PAINT)
 {
  PAINTSTRUCT ps;
  BeginPaint(hWnd, &ps);
  //OnPaint(ps.hdc);
  EndPaint(hWnd, &ps);
 }
 else if (msg == WM_DESTROY)
 {
  PostQuitMessage(0);
 }
 return DefWindowProc(hWnd, msg, wParam, lParam);
}
 
void UIRun()
{
 WNDCLASSEX wc = {sizeof(wc)};
 wc.hInstance     = GetModuleHandle(0);
 wc.lpszClassName = _T("uiclassname");
 wc.lpfnWndProc   = &UIWndProcBase;
 wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
 RegisterClassEx(&wc);
 wstring ws = L"aaabbcd";
 HWND hParent = CreateWindowExW(WS_EX_TRANSPARENT, _T("EDIT"), _T(""),  WS_POPUP, 0, 0, 100, 100, 0, NULL, wc.hInstance, NULL);
 HWND m_hWnd = CreateWindowEx(0, wc.lpszClassName, ws.c_str(),  WS_POPUP, 0, 0, 100, 100,
  hParent, NULL, wc.hInstance, NULL);
 
 SetWindowLong(m_hWnd, GWL_EXSTYLE, WS_EX_LAYERED|WS_EX_TRANSPARENT|WS_EX_TOPMOST);
 SetLayeredWindowAttributes(m_hWnd, RGB(0,0,0), 120, LWA_ALPHA);
 SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE|SWP_NOSIZE|SWP_NOMOVE|SWP_SHOWWINDOW);
 
 MSG msg;
 while (GetMessage(&msg, 0, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
}
 
int main()
{
 UIRun();
 
 return 0;
}
此条目发表在开发分类目录。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

*