Google Answers Logo
View Question
 
Q: Reposition a window to a second monitor C++ ( No Answer,   1 Comment )
Question  
Subject: Reposition a window to a second monitor C++
Category: Computers > Programming
Asked by: techvayne-ga
List Price: $15.00
Posted: 24 Aug 2005 21:32 PDT
Expires: 23 Sep 2005 21:32 PDT
Question ID: 560105
I am writing an application that has a button that needs a simple
function that will move a window based on the handle (hWnd) from one
monitor, onto another monitor in a multi-monitor configuration. Of
course, the function should have a prototype similar to this:

MoveToMonitor (HWND hWndMon1, HWND hWndMon2);

Of course, it doesn't have to be like this(use whatever you think is
the best approach). I have done research into this and would like this
to use straight Win32 API and not MFC. To maybe save a little bit of
time, I have found these functions that might provide all of the
functionality that is needed:

EnumDisplayMonitors
EnumDisplayDevices
SetWindowPos

It might be important to note that I am most interested in moving the
upper left corner of the window because the window might span more
than one monitor.

I would also like this to move RELATIVELY from the top-left corner. In
other words, if the window originated on a monitor that has an 800x600
resolution to a window that has a 1600x1200 resolution, it will be
twice as far from the top-left corner of the window in pixels.

Clarification of Question by techvayne-ga on 24 Aug 2005 21:37 PDT
In the above prototype, it should be more like this:

MoveToMonitor (HWND hWnd, HWND hWndMon);

The first HWND is the handle of the window to move, and the second
HWND is the handle to the destination monitor.
Answer  
There is no answer at this time.

Comments  
Subject: Re: Reposition a window to a second monitor C++
From: pianoboy77-ga on 25 Aug 2005 15:02 PDT
 
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

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy