1
votes

I have an Winform application that receives a command line parameter that specify if it would be displayed on the main Monitor of the second Monitor via a I-Omega USB Dual Monitor connector.

The application runs on a laptop and uses two monitor. 1. The first monitor just connect to the laptop VGA port. I run the "MyApplication.exe 1" 2. The second monitor is connected via USB dual monitor VGA port. I run the second instance "MyApplication.exe 2"

I was able to force the second instance of the application to go to the second montior by using Screen.AllScreens and changing the Form's location. I specified in the Power Setting of the laptop not to go on Standby or turnoff monitor if laptop is folded. When I close the laptop monitor, both instance of the application remains on their respective monitor. But when I reopen the laptop, the second instance moves the the first screen.

My question is there a monitor open event in .NET? Or are there other settings to retain the second instance to the second monitor/Screen?

I know there is a WM_POWERBROADCAST event but, I already set the laptop not to sleep.

1
Not sure about monitor open/close event, but you can try positioning your forms in form activated event handler.Maheep
Look at the SystemEvents class. Guessing at DisplaySettingsChanged or UserPreferenceChanged events.Hans Passant

1 Answers

1
votes

I was able to do this not by windows event but by using a timer. Basically the timer would check for the location of the Window with the desired screen. If it does not match the desired screen, I would adjust the location again to move to the desired monitor. I added tolerance of 4(WinXP)~8(Win7)pixels when checking if the top-left location is equal. The top and left location becomes (-4,-4) when in maximized state. When in minimized state, I dont check the location.

I used the ShowOnMonitor method from a previous question: How do I ensure a form displays on the "additional" monitor in a dual monitor scenario?

private void autoAdjustPositionTimer_Tick(object sender, EventArgs e) {

    if (WindowState == FormWindowState.Minimized)
    {
        return;
    }

    autoAdjustPositionTimer.Stop();
    try
    {
        Screen[] sc;
        sc = Screen.AllScreens;

        int showOnMonitor = ExpectedMonitorIndex; // i.e. 1
        if (showOnMonitor >= sc.Length)
        {
            showOnMonitor = 0;
        }

        int offsetTolerance = 8;

        // Check the top left position only
        int xDiff = Math.Abs(Location.X - sc[showOnMonitor].Bounds.Left);
        int yDiff = Math.Abs(Location.Y - sc[showOnMonitor].Bounds.Top);
        if (xDiff > offsetTolerance || yDiff > offsetTolerance){
            // Method to move the position
            ShowOnMonitor(showOnMonitor);
        }
    }
    finally
    {
        autoAdjustPositionTimer.Start();
    }
}