1

I'm following the advice given in this StackOverflow article to move a process I start to the monitor opposite of the windows form that is running. Unfortunately it's not working. Here's what's happening: I call Process.Start() which opens a TIF or PDF file with the user's default viewer for that image type. When running the code below, the image viewer will either:

1) Start in the same window/monitor that the windows form is in, and do nothing; or

2) Start in the window opposite the windows form, and move to the monitor of the windows form.

As you can probably tell, this is the opposite of what I wish to achieve. If the viewer opens in the same monitor of the form, I want to move it to the opposite form; otherwise, don't move it.

Below is my code used. I can't tell what I'm doing wrong, since I seem to be following the guidelines from the article and MSDN.

    Dim path As String = _gridQueue.CurrentRow.Cells("Path").Value.ToString()
    Dim mainWindowHandle As Integer = 0

    Using p As Process = Process.Start(path)
        p.WaitForInputIdle()
        _processIds.Add(p.Id)
        mainWindowHandle = p.MainWindowHandle
    End Using

    ' Set window to second monitor.
    Dim currentMonitor As Screen = Screen.FromControl(Me)
    Dim otherMonitor As Screen = Nothing

    If (currentMonitor Is Screen.AllScreens(0)) Then
        otherMonitor = Screen.AllScreens(1)
    Else
        otherMonitor = Screen.AllScreens(0)
        currentMonitor = Screen.AllScreens(1)
    End If

    Dim area As Rectangle = otherMonitor.WorkingArea
    SetWindowPos(mainWindowHandle, CType(SpecialWindowHandles.HWND_TOP, IntPtr), otherMonitor.WorkingArea.Left, otherMonitor.WorkingArea.Top, area.Size.Width, area.Size.Height, SetWindowPosFlags.ShowWindow)

Any help would be greatly appreciated. Thanks.

Community
  • 1
  • 1
Paul
  • 1,375
  • 2
  • 21
  • 55

0 Answers0