- Debugging WIN32 focus bugs
- // Check the focus ten times a second
- // Change hwndMain to your main application window
- // Note that this won't work if you have multiple UI threads
- ::SetTimer(hwndMain, 1, 100, HighlightTimerProc);
- LRESULT CALLBACK HighlightWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_NCHITTEST:
- return HTTRANSPARENT;
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- VOID CALLBACK HighlightTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
- {
- // static locals are bad
- static bool initialised = false;
- static HWND hwndHighlight = 0;
- if (!initialised)
- {
- HINSTANCE hInstance = 0;
- WNDCLASSEX wcex;
- wcex.cbSize = sizeof(WNDCLASSEX);
- wcex.style = CS_HREDRAW | CS_VREDRAW;
- wcex.lpfnWndProc = HighlightWndProc;
- wcex.cbClsExtra = 0;
- wcex.cbWndExtra = 0;
- wcex.hInstance = hInstance;
- wcex.hIcon = 0;
- wcex.hCursor = 0;
- wcex.hbrBackground = (HBRUSH)(COLOR_HIGHLIGHTTEXT);
- wcex.lpszMenuName = 0;
- wcex.lpszClassName = L"HighlightWindowClasss";
- wcex.hIconSm = 0;
- ATOM atomHighlightClass = RegisterClassEx(&wcex);
- hwndHighlight = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW,
- (LPCTSTR)atomHighlightClass, L"", WS_POPUP,
- CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
- // Set opacity to 200/255
- SetLayeredWindowAttributes(hwndHighlight, 0, 200, LWA_ALPHA);
- initialised = true;
- }
- static HWND hwndCurrentHighlight = 0;
- HWND hwndFocus = GetFocus();
- if (hwndFocus != hwndCurrentHighlight)
- {
- if (hwndFocus == 0)
- {
- ShowWindow(hwndHighlight, SW_HIDE);
- }
- else
- {
- RECT rect;
- GetWindowRect(hwndFocus, &rect);
- MoveWindow(hwndHighlight, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, false);
- ShowWindow(hwndHighlight, SW_SHOW);
- }
- hwndCurrentHighlight = hwndFocus;
- }
- }