Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 2.21 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Debugging WIN32 focus bugs
  2. // Check the focus ten times a second
  3. // Change hwndMain to your main application window
  4. // Note that this won't work if you have multiple UI threads
  5. ::SetTimer(hwndMain, 1, 100, HighlightTimerProc);
  6.        
  7. LRESULT CALLBACK HighlightWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  8. {
  9.     switch (message)
  10.     {
  11.     case WM_NCHITTEST:
  12.         return HTTRANSPARENT;
  13.     default:
  14.         return DefWindowProc(hWnd, message, wParam, lParam);
  15.     }
  16.     return 0;
  17. }
  18.  
  19. VOID CALLBACK HighlightTimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
  20. {
  21.     // static locals are bad
  22.     static bool initialised = false;
  23.     static HWND hwndHighlight = 0;
  24.  
  25.     if (!initialised)
  26.     {
  27.         HINSTANCE hInstance = 0;
  28.  
  29.         WNDCLASSEX wcex;
  30.  
  31.         wcex.cbSize = sizeof(WNDCLASSEX);
  32.  
  33.         wcex.style          = CS_HREDRAW | CS_VREDRAW;
  34.         wcex.lpfnWndProc    = HighlightWndProc;
  35.         wcex.cbClsExtra     = 0;
  36.         wcex.cbWndExtra     = 0;
  37.         wcex.hInstance      = hInstance;
  38.         wcex.hIcon          = 0;
  39.         wcex.hCursor        = 0;
  40.         wcex.hbrBackground  = (HBRUSH)(COLOR_HIGHLIGHTTEXT);
  41.         wcex.lpszMenuName   = 0;
  42.         wcex.lpszClassName  = L"HighlightWindowClasss";
  43.         wcex.hIconSm        = 0;
  44.  
  45.         ATOM atomHighlightClass = RegisterClassEx(&wcex);
  46.  
  47.         hwndHighlight = CreateWindowEx(WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW,
  48.             (LPCTSTR)atomHighlightClass, L"", WS_POPUP,
  49.             CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
  50.  
  51.         // Set opacity to 200/255
  52.         SetLayeredWindowAttributes(hwndHighlight, 0, 200, LWA_ALPHA);
  53.  
  54.         initialised = true;
  55.     }
  56.  
  57.     static HWND hwndCurrentHighlight = 0;
  58.  
  59.     HWND hwndFocus = GetFocus();
  60.     if (hwndFocus != hwndCurrentHighlight)
  61.     {
  62.         if (hwndFocus == 0)
  63.         {
  64.             ShowWindow(hwndHighlight, SW_HIDE);
  65.         }
  66.         else
  67.         {
  68.             RECT rect;
  69.             GetWindowRect(hwndFocus, &rect);
  70.             MoveWindow(hwndHighlight, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, false);
  71.             ShowWindow(hwndHighlight, SW_SHOW);
  72.         }
  73.         hwndCurrentHighlight = hwndFocus;
  74.     }
  75. }