I've never done multiple-monitor programming, but I found a great
article for you with lots of sample source code using straight Win32
API:
http://www.microsoft.com/msj/0697/monitor/monitor.aspx
As a first step, I would see if you can get their sample program
compiling and running (I haven't tried it myself, but you'll want to
make sure the helper functions all work so you can use them).
After quickly analyzing the source code (in figure 10), I'm thinking
your function would look something like this, assuming we're making
use of their helper functions:
//------------------------------------------------------------------
void MoveWindowToMonitor(HWND hwnd, HWND hwndMon, BOOL fWork)
{
RECT rc_win; // window dimensions
RECT rc_mon; // monitor dimensions (where window currently resides)
RECT rc_mon_new; // destination monitor dimensions
// Use the helper functions to retrieve all the dimensions
GetWindowRect(hwnd, &rc_win);
GetMonitorRect(hwnd, &rc_mon, fWork);
GetMonitorRect(hwndMon, &rc_mon_new, fWork);
// Get current window X & Y coordinates as percentage distances
double percX = (rc_win.left - rc_mon.left) /
(1.0 * rc_mon.right - rc_mon.left);
double percY = (rc_win.top - rc_mon.top) /
(1.0 * rc_mon.bottom - rc_mon.top);
// Apply percentages to new monitor's dimensions to get proper
// relative coords for placing window.
int newX = percX * (rc_mon_new.right - rc_mon_new.left) + rc_mon_new.left;
int newY = percY * (rc_mon_new.bottom - rc_mon_new.top) + rc_mon_new.top;
// Move the window to the new coords.
SetWindowPos(hwnd, NULL, newX, newY, 0, 0,
SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
//------------------------------------------------------------
I have no idea if that function I just wrote is correct syntactically
or if my logic is even correct. Again, I just skimmed the sample code
and threw together the above function based on what I saw, so I may be
way off base, but hopefully you can get something working by tweaking
things just a little :)
Good Luck!
Google search criteria used:
position window on "second monitor" C++ MFC |