Advertisement
Guest User

Elliptic Window Flickering Problem

a guest
Apr 3rd, 2013
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.16 KB | None | 0 0
  1. #include <windows.h>
  2. #include <windowsx.h>
  3.  
  4. HWND CreateTestWindow(WNDPROC);
  5.  
  6. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
  7.     static bool moving{};
  8.  
  9.     switch (msg) {
  10.         case WM_DESTROY: {
  11.             PostQuitMessage(0);
  12.             return 0;
  13.         }
  14.  
  15.         case WM_LBUTTONDOWN: {
  16.             moving = true;
  17.             return 0;
  18.         }
  19.  
  20.         case WM_LBUTTONUP: {
  21.             moving = false;
  22.             return 0;
  23.         }
  24.  
  25.         case WM_MOUSEMOVE: {
  26.             static POINT old{0, 0};
  27.  
  28.             if (moving) {
  29.                 RECT r;
  30.                 GetWindowRect(hwnd, &r);
  31.  
  32.                 int x = GET_X_LPARAM(lParam);
  33.                 int y = GET_Y_LPARAM(lParam);
  34.  
  35.                 RECT newPos;
  36.                 newPos.left = r.left + x - old.x;
  37.                 newPos.top = r.top + y - old.y;
  38.  
  39.                 SetWindowRgn(hwnd, nullptr, FALSE);
  40.  
  41.                 SetWindowPos(hwnd, nullptr, newPos.left, newPos.top, 0, 0,
  42.                     SWP_NOZORDER | SWP_NOSIZE | SWP_NOREDRAW
  43.                 );
  44.  
  45.                 SetWindowRgn(hwnd, CreateEllipticRgn(200, 200, 600, 400), FALSE);
  46.             }
  47.  
  48.             old.x = GET_X_LPARAM(lParam);
  49.             old.y = GET_Y_LPARAM(lParam);
  50.  
  51.             return 0;
  52.         }
  53.     }
  54.  
  55.     return DefWindowProc(hwnd, msg, wParam, lParam);
  56. }
  57.  
  58. int main() {
  59.     HWND hwnd = CreateTestWindow(WndProc);
  60.     SetWindowRgn(hwnd, CreateEllipticRgn(200, 200, 600, 400), TRUE);
  61.  
  62.     MSG msg;
  63.     while (GetMessage(&msg, nullptr, 0, 0)) {
  64.         TranslateMessage(&msg);
  65.         DispatchMessage(&msg);
  66.     }
  67. }
  68.  
  69. HWND CreateTestWindow(WNDPROC f) {
  70.     WNDCLASS wc;
  71.     ZeroMemory(&wc, sizeof wc);
  72.     wc.hInstance = GetModuleHandle(nullptr);
  73.     wc.lpfnWndProc = f;
  74.     wc.lpszClassName = "Test Window Class";
  75.     wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
  76.  
  77.     RegisterClass(&wc);
  78.  
  79.     return CreateWindow("Test Window Class", "Test Window",
  80.         WS_OVERLAPPEDWINDOW | WS_VISIBLE,
  81.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  82.         nullptr, nullptr, GetModuleHandle(nullptr), nullptr
  83.     );
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement