Guest User

winPeep

a guest
Oct 21st, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <windows.h>
  2.  
  3. int gScreenHeight; /* A global variable used to store the height of the screen. */
  4. const int TM = 20; /* Change this value if you want a different top margin. */
  5. const int BM = 60; /* Change this value if you want a different bottom margin, but mind the task bar. */
  6. const int timerInterval = 40; /* Increase this value if you want smoother movement (be careful with that). */
  7.  
  8. void peepWindow()
  9. {
  10.     /* Retrieve the handle to the foreground window. */
  11.     HWND hwnd = GetForegroundWindow();
  12.     /* Obtain dimensions of the foreground window. */
  13.     RECT wndRect;
  14.     GetWindowRect(hwnd, &wndRect);
  15.     int wndHeight = wndRect.bottom - wndRect.top;
  16.     /* Check if the foreground window height is greater than the screen height excluding the previously set margins. */
  17.     if(wndHeight > gScreenHeight - TM - BM)
  18.     {
  19.         /* Obtain the mouse cursor position. */
  20.         POINT mousePoint;
  21.         GetCursorPos(&mousePoint);
  22.         /* Calculate a new position of the foreground window. */
  23.         double C = ((double)(wndHeight))/((double)(TM + BM - gScreenHeight));
  24.         int newWndPos = (int)((1.0+C)*((double)(mousePoint.y)) - C*((double)(TM)));
  25.         /* Place the foreground window in the new position. */
  26.         SetWindowPos(hwnd, NULL, wndRect.left, newWndPos, 100, 100, SWP_NOSIZE);
  27.     }
  28. }
  29.  
  30. LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  31. {
  32.     switch(Message)
  33.     {
  34.         case WM_DESTROY:
  35.         {
  36.             PostQuitMessage(0);
  37.             break;
  38.         }
  39.         case WM_TIMER:
  40.         {
  41.             peepWindow();
  42.             break;
  43.         }
  44.         default:
  45.             return DefWindowProc(hwnd, Message, wParam, lParam);
  46.     }
  47.     return 0;
  48. }
  49.  
  50. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  51. {
  52.     /* Set the global variable. */
  53.     gScreenHeight = GetSystemMetrics(SM_CYSCREEN);
  54.    
  55.     WNDCLASSEX wc;
  56.     HWND hwnd;
  57.     MSG Msg;
  58.  
  59.     memset(&wc,0,sizeof(wc));
  60.     wc.cbSize        = sizeof(WNDCLASSEX);
  61.     wc.lpfnWndProc   = WndProc;
  62.     wc.hInstance     = hInstance;
  63.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  64.     wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  65.     wc.lpszClassName = "WindowClass";
  66.     wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  67.     wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  68.  
  69.     if(!RegisterClassEx(&wc))
  70.     {
  71.         MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
  72.         return 0;
  73.     }
  74.  
  75.     hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","winPeep",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
  76.         CW_USEDEFAULT,
  77.         CW_USEDEFAULT,
  78.         640,
  79.         480,
  80.         NULL,NULL,hInstance,NULL);
  81.  
  82.     if(hwnd == NULL)
  83.     {
  84.         MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
  85.         return 0;
  86.     }
  87.    
  88.     /* Create a timer. */
  89.     SetTimer(hwnd, 1, timerInterval, (TIMERPROC)NULL);
  90.  
  91.     while(GetMessage(&Msg, NULL, 0, 0) > 0)
  92.     {
  93.         TranslateMessage(&Msg);
  94.         DispatchMessage(&Msg);
  95.     }
  96.     return Msg.wParam;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment