Advertisement
Guest User

move windows

a guest
Jul 10th, 2011
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.70 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. /* Declare Windows procedure */
  4. LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
  5.  
  6. /* Make the class name into a global variable */
  7. wchar_t szClassName[ ] = L"WindowsApp";
  8.  
  9. int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nFunsterStil)
  10. {
  11.     HWND hwnd;        /* This is the handle for our window */
  12.     MSG messages;      /* Here messages to the application are saved */
  13.     ZeroMemory(&messages, sizeof(MSG));
  14.     WNDCLASSEX wincl;    /* Data structure for the windowclass */
  15.  
  16.     /* The Window structure */
  17.     wincl.hInstance = hThisInstance;
  18.     wincl.lpszClassName = szClassName;
  19.     wincl.lpfnWndProc = WindowProcedure;                /* This function is called by windows */
  20.     wincl.style = CS_DBLCLKS;                           /* Catch double-clicks */
  21.     wincl.cbSize = sizeof (WNDCLASSEX);
  22.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);     /* Use default icon and mouse-pointer */
  23.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  24.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  25.     wincl.lpszMenuName = NULL;                          /* No menu */
  26.     wincl.cbClsExtra = 0;                               /* No extra bytes after the window class */
  27.     wincl.cbWndExtra = 0;                               /* structure or the window instance */
  28.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;  /* Use Windows's default color as the background*/
  29.  
  30.     /* Register the window class, and if it fails quit the program */
  31.     if (!RegisterClassEx (&wincl))
  32.         return 0;
  33.  
  34.     /* The class is registered */
  35.     /* WS_EX_TOPMOST - always on top | WS_EX_NOACTIVATE - Prevent mouse input from activating the window*/
  36.     /* WS_POPUPWINDOW - no title bar | WS_SIZEBOX - resizable */
  37.     hwnd = CreateWindowEx (WS_EX_TOPMOST | WS_EX_NOACTIVATE, szClassName, L"App", WS_POPUPWINDOW | WS_SIZEBOX,
  38.         CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hThisInstance, NULL);
  39.  
  40.     /* Make the window visible on the screen */
  41.     ShowWindow (hwnd, nFunsterStil);
  42.  
  43.     while(messages.message != WM_QUIT)
  44.     {
  45.         // IF there is a Windows message then process it.
  46.         if(PeekMessage(&messages, 0, 0, 0, PM_REMOVE))
  47.         {
  48.             TranslateMessage(&messages);
  49.             DispatchMessage(&messages);
  50.         }
  51.         // ELSE, do other stuff.
  52.         else
  53.         {  
  54.             //SetWindowPos(hwnd, NULL, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
  55.         }
  56.     }
  57.  
  58.     return messages.wParam;
  59. }
  60.  
  61.  
  62. /* This function is called by the Windows function DispatchMessage() */
  63.  
  64. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  65. {
  66.     static int lastMouseX = 0;
  67.     static int lastMouseY = 0;
  68.     switch (message)         /* handle the messages */
  69.     {
  70.     case WM_LBUTTONDOWN:
  71.         lastMouseX = LOWORD(lParam);
  72.         lastMouseY = HIWORD(lParam);
  73.         break;
  74.     case WM_LBUTTONUP:
  75.         break;
  76.     //case WM_NCMOUSEMOVE:
  77.     case WM_MOUSEMOVE:
  78.         if(wParam == MK_LBUTTON)
  79.         {
  80.             int currentMouseX = LOWORD(lParam);
  81.             int currentMouseY = HIWORD(lParam);
  82.             int deltaX = currentMouseX - lastMouseX;
  83.             int deltaY = currentMouseY - lastMouseY;
  84.             RECT prc;
  85.             GetWindowRect(hwnd, &prc);
  86.             SetWindowPos(hwnd, NULL, prc.left + deltaX, prc.top + deltaY, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
  87.             lastMouseX = currentMouseX;
  88.             lastMouseY = currentMouseY;
  89.         }
  90.         break;
  91.     case WM_DESTROY:
  92.     case WM_RBUTTONUP:
  93.         PostQuitMessage (0);    /* send a WM_QUIT to the message queue */
  94.         break;
  95.  
  96.     //case WM_MOVING:   /* fix for a bug in windows that prevents contents of the windows from being displayed */
  97.     case WM_SIZING:            
  98.         {
  99.             RECT *prc = (RECT *)lParam;
  100.             SetWindowPos(hwnd, NULL, prc->left, prc->top, prc->right - prc->left, prc->bottom - prc->top, 0);
  101.         }
  102.         break;
  103.     default:           /* for messages that we don't deal with */
  104.         return DefWindowProc (hwnd, message, wParam, lParam);
  105.     }
  106.  
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement