-
-
Save dsebastien/a9f803713294d2b87217 to your computer and use it in GitHub Desktop.
CPP Move foreground window to monitor where the mouse is located
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// WindowsFun.cpp : Defines the entry point for the console application. | |
// | |
#include "stdafx.h" | |
#include <iostream> | |
#include <windows.h> | |
#include "utils.h" | |
using namespace std; | |
int main() | |
{ | |
std::cout << "Windows Fun v1.0\n"; | |
std::cout << "----------------\n"; | |
std::cout << "Press a key to start the fun experiments\n"; | |
std::getchar(); | |
int seconds = 3; | |
std::cout << "Sleeping for " << seconds << " seconds.\nFeel free to select another Window / move the mouse elsewhere. After that time, the selected Window will be reattached to the monitor where the mouse is located\n"; | |
Sleep(seconds * 1000); | |
// ----------------------------------------------------------------------------------- | |
// Find the window to re-attach to another monitor (if needed) | |
// In ConEmu the Window is probably known already | |
// ----------------------------------------------------------------------------------- | |
std::cout << "Getting the current foreground window\n"; | |
HWND hForegroundWindow = GetForegroundWindow(); | |
flash(hForegroundWindow); | |
std::cout << "Getting the monitor from the window\n"; | |
HMONITOR hMonitorOfForegroundWindow = MonitorFromWindow(hForegroundWindow, MONITOR_DEFAULTTONEAREST); | |
if (NULL == hMonitorOfForegroundWindow) { | |
std::cout << "Could not find the monitor of the foreground window\n"; | |
std::cout << "Press a key to exit\n"; | |
std::getchar(); | |
return 0; | |
} | |
std::cout << "Monitor of the foreground window found\n"; | |
std::cout << "Foreground window monitor handle: " << hMonitorOfForegroundWindow << "\n"; | |
MONITORINFO monitorInfoOfForegroundWindow; | |
monitorInfoOfForegroundWindow.cbSize = sizeof(MONITORINFO); // necessary as per https://msdn.microsoft.com/en-us/library/windows/desktop/dd144901(v=vs.85).aspx | |
if (!GetMonitorInfo(hMonitorOfForegroundWindow, &monitorInfoOfForegroundWindow)) { | |
std::cout << "Could not get the monitor info of monitor where the foreground window is located\n"; | |
std::cout << "Press a key to exit\n"; | |
std::getchar(); | |
return 0; | |
} | |
// ----------------------------------------------------------------------------------- | |
// Find the monitor where the mouse is located | |
// ----------------------------------------------------------------------------------- | |
std::cout << "Identifying the monitor where the mouse is located\n"; | |
POINT point; | |
GetCursorPos(&point); | |
std::cout << "Cursor position: "; | |
displayPoint(point.x, point.y); | |
std::cout << "\n"; | |
HMONITOR hMonitorWhereMouseIsLocated = MonitorFromPoint(point, MONITOR_DEFAULTTONEAREST); | |
if (NULL == hMonitorWhereMouseIsLocated) { | |
std::cout << "The monitor where the mouse is could not be found\n"; | |
std::cout << "Press a key to exit\n"; | |
std::getchar(); | |
return 0; | |
} | |
std::cout << "Monitor where mouse is found\n"; | |
std::cout << "Monitor handle where mouse located: " << hMonitorWhereMouseIsLocated << "\n"; | |
MONITORINFO monitorInfoOfMouse; | |
monitorInfoOfMouse.cbSize = sizeof(MONITORINFO); // necessary as per https://msdn.microsoft.com/en-us/library/windows/desktop/dd144901(v=vs.85).aspx | |
if (!GetMonitorInfo(hMonitorWhereMouseIsLocated, &monitorInfoOfMouse)) { | |
std::cout << "Could not get the monitor info of the monitor where the mouse is located\n"; | |
std::cout << "Press a key to exit\n"; | |
std::getchar(); | |
return 0; | |
} | |
// ----------------------------------------------------------------------------------- | |
// Find out whether the window is currently on the screen where the mouse is located | |
// ----------------------------------------------------------------------------------- | |
std::cout << "Checking whether the window is currently on the screen where the mouse is located\n"; | |
bool foregroundWindowNeedsToBeReattached = hMonitorOfForegroundWindow != hMonitorWhereMouseIsLocated; | |
if (!foregroundWindowNeedsToBeReattached) { | |
std::cout << "The foreground window is on the same monitor as the mouse. No need to reattach it to another monitor!\n"; | |
std::cout << "Press a key to exit\n"; | |
std::getchar(); | |
return 0; | |
} | |
std::cout << "The foreground window is on a different screen than the mouse; it needs to be attached to the other monitor!\n"; | |
// ----------------------------------------------------------------------------------- | |
// Reattach the foreground window to the monitor where the mouse is located | |
// ----------------------------------------------------------------------------------- | |
std::cout << "Info of the foreground window monitor: "; | |
displayRect(monitorInfoOfForegroundWindow.rcMonitor.left, monitorInfoOfForegroundWindow.rcMonitor.top, monitorInfoOfForegroundWindow.rcMonitor.right, monitorInfoOfForegroundWindow.rcMonitor.bottom); | |
std::cout << "\n"; | |
//std::cout << "Left: " << monitorInfoOfForegroundWindow.rcMonitor.left << "\n"; | |
//std::cout << "Top: " << monitorInfoOfForegroundWindow.rcMonitor.top << "\n"; | |
//std::cout << "Right: " << monitorInfoOfForegroundWindow.rcMonitor.right << "\n"; | |
//std::cout << "Bottom: " << monitorInfoOfForegroundWindow.rcMonitor.bottom << "\n"; | |
std::cout << "Info of the monitor where the mouse is located: "; | |
displayRect(monitorInfoOfMouse.rcMonitor.left, monitorInfoOfMouse.rcMonitor.top, monitorInfoOfMouse.rcMonitor.right, monitorInfoOfMouse.rcMonitor.bottom); | |
std::cout << "\n"; | |
//std::cout << "Left: " << monitorInfoOfMouse.rcMonitor.left << "\n"; | |
//std::cout << "Top: " << monitorInfoOfMouse.rcMonitor.top << "\n"; | |
//std::cout << "Right: " << monitorInfoOfMouse.rcMonitor.right << "\n"; | |
//std::cout << "Bottom: " << monitorInfoOfMouse.rcMonitor.bottom << "\n"; | |
std::cout << "Calculating the offsets\n"; | |
//todo calculate the difference between both monitors | |
std::cout << "Getting the current window position\n"; | |
RECT rectSourcePosition; | |
if(!GetWindowRect(hForegroundWindow, &rectSourcePosition)){ | |
std::cout << "Could not get the current window position\n"; | |
std::cout << "Press a key to exit\n"; | |
std::getchar(); | |
return 0; | |
} | |
std::cout << "Source window position: "; | |
displayRect(rectSourcePosition.left, rectSourcePosition.top, rectSourcePosition.right, rectSourcePosition.bottom); | |
std::cout << "\n"; | |
// old: monitorInfoOfForegroundWindow | |
// new: monitorInfoOfMouse | |
int xOffset = monitorInfoOfMouse.rcMonitor.left - monitorInfoOfForegroundWindow.rcMonitor.left; | |
int yOffset = monitorInfoOfMouse.rcMonitor.top - monitorInfoOfForegroundWindow.rcMonitor.top; | |
RECT rectDestinationPosition; | |
rectDestinationPosition.top = rectSourcePosition.top + yOffset; | |
rectDestinationPosition.bottom = rectSourcePosition.bottom + yOffset; | |
rectDestinationPosition.left = rectSourcePosition.left + xOffset; | |
rectDestinationPosition.right = rectSourcePosition.right + xOffset; | |
std::cout << "Destination window position: "; | |
displayRect(rectDestinationPosition.left, rectDestinationPosition.top, rectDestinationPosition.right, rectDestinationPosition.bottom); | |
std::cout << "\n"; | |
bool repaint = true; | |
if (!MoveWindow(hForegroundWindow, | |
rectDestinationPosition.left, | |
rectDestinationPosition.top, | |
rectSourcePosition.right - rectSourcePosition.left, | |
rectSourcePosition.bottom - rectSourcePosition.top, | |
repaint)) { | |
std::cout << "Could not move the window to its new location\n"; | |
std::cout << "Press a key to exit\n"; | |
std::getchar(); | |
return 0; | |
} | |
else { | |
std::cout << "The foreground window was moved successfully to its new location!\n"; | |
flash(hForegroundWindow); | |
} | |
// ----------------------------------------------------------------------------------- | |
std::cout << "All done, enjoy & press a key to exit\n"; | |
std::getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment