How to determine which monitor an application is using and how to get a handle to it?

I'm doing an application that is invoking another window from the main one. My question is how to determine on which monitor (in case there are 2 or more) the main application window is and how to get a handle to that monitor? So far my code looks like this:

RECT desktop;
const HWND hDesktop = GetDesktopWindow();
GetWindowRect( hDesktop, &desktop );

int width = SInt32( desktop.right / 2 );
int height = SInt32( desktop.bottom / 2 );

OpenNewWindow( width, height );

But this is only getting the handle to the desktop (the main monitor) and right and bottom are the resolution sizes of the main monitor. I'm writing this on C++ Thank you!

Upvotes: 2

Views: 1153

Answers (1)

I Found a solution:

        HMONITOR currentMonitor = MonitorFromWindow( GetActiveWindow(), MONITOR_DEFAULTTONEAREST );
        MONITORINFO activeMonitorInfo;
        activeMonitorInfo.cbSize = sizeof( MONITORINFO );
        GetMonitorInfo( currentMonitor, (LPMONITORINFO) &activeMonitorInfo );

        int width = SInt32( ( activeMonitorInfo.rcMonitor.right - activeMonitorInfo.rcMonitor.left ) * 0.75 );
        int height = SInt32( ( activeMonitorInfo.rcMonitor.bottom - activeMonitorInfo.rcMonitor.top ) * 0.75 );

        OpenNewWIndow( width, height );

It happens that GetActiveWindow returns a handle to the current active window and the rest is easy.

Upvotes: 3

Related Questions