Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- int gScreenHeight; /* A global variable used to store the height of the screen. */
- const int TM = 20; /* Change this value if you want a different top margin. */
- const int BM = 60; /* Change this value if you want a different bottom margin, but mind the task bar. */
- const int timerInterval = 40; /* Increase this value if you want smoother movement (be careful with that). */
- void peepWindow()
- {
- /* Retrieve the handle to the foreground window. */
- HWND hwnd = GetForegroundWindow();
- /* Obtain dimensions of the foreground window. */
- RECT wndRect;
- GetWindowRect(hwnd, &wndRect);
- int wndHeight = wndRect.bottom - wndRect.top;
- /* Check if the foreground window height is greater than the screen height excluding the previously set margins. */
- if(wndHeight > gScreenHeight - TM - BM)
- {
- /* Obtain the mouse cursor position. */
- POINT mousePoint;
- GetCursorPos(&mousePoint);
- /* Calculate a new position of the foreground window. */
- double C = ((double)(wndHeight))/((double)(TM + BM - gScreenHeight));
- int newWndPos = (int)((1.0+C)*((double)(mousePoint.y)) - C*((double)(TM)));
- /* Place the foreground window in the new position. */
- SetWindowPos(hwnd, NULL, wndRect.left, newWndPos, 100, 100, SWP_NOSIZE);
- }
- }
- LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
- {
- switch(Message)
- {
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- break;
- }
- case WM_TIMER:
- {
- peepWindow();
- break;
- }
- default:
- return DefWindowProc(hwnd, Message, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
- {
- /* Set the global variable. */
- gScreenHeight = GetSystemMetrics(SM_CYSCREEN);
- WNDCLASSEX wc;
- HWND hwnd;
- MSG Msg;
- memset(&wc,0,sizeof(wc));
- wc.cbSize = sizeof(WNDCLASSEX);
- wc.lpfnWndProc = WndProc;
- wc.hInstance = hInstance;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
- wc.lpszClassName = "WindowClass";
- wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
- wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
- if(!RegisterClassEx(&wc))
- {
- MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
- return 0;
- }
- hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","winPeep",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 640,
- 480,
- NULL,NULL,hInstance,NULL);
- if(hwnd == NULL)
- {
- MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
- return 0;
- }
- /* Create a timer. */
- SetTimer(hwnd, 1, timerInterval, (TIMERPROC)NULL);
- while(GetMessage(&Msg, NULL, 0, 0) > 0)
- {
- TranslateMessage(&Msg);
- DispatchMessage(&Msg);
- }
- return Msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment