I am trying to create a transparent window using the Win32 API, which can be dragged around the screen, and the background of the window will remain transparent and show the windows behind it.
I do this by doing the following:
ATOM MyRegisterClass(HINSTANCE hInstance){
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = NULL;
wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = NULL;
return RegisterClassEx(&wcex);}
String: wcex.hbrBackground = NULL; responsible for the transparency of the window during initialization.
Window creation is performed in:
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow){
HWND hWnd;
hInst = hInstance;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;}
And the way the window remains transparent when dragging is:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_MOVING:
InvalidateRect(hWnd, NULL, TRUE);
break;
default:
return = DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;}
Where:
InvalidateRect (hWnd, NULL, TRUE); the line causes the window to redraw when the event moves.
The problem is that the background of the window does not redraw in areas that are not "new."
An example in words
, (, .. - WindowB), , WindowA, WindowB, WindowB ( WindowA - ). WindowA , WindowB .
WindowB, WindowB, WindowA ( , WindowA).
WindowA, , WindowB, WindowA, , , WindowA, InvalidateRect (hWnd, NULL, ); .
, , WindowB , " " - , WindowA WindowA .
, InvalidateRect (hWnd, NULL, TRUE); , " ".
:
http://imgur.com/gallery/2uVX4yj/new
.
- , Visual Studio 2010 Win32:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow){
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
HACCEL hAccelTable;
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TEST2, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_TEST2));
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;}