locked
Setting the window location of an external application

    Question

  • I have a VB 2008 Express Edition Application where I am launching a third-party application, for instance MS notepad. I need to place the window either at the upper left or the upper right corner of the screen. I have not been able to make it work using the Windows API as suggested from articles and hints found on the internet. I tried SetWindowPos, ShowWindow, MoveWindow; none worked. Any help with this issue would be greatly appreciated. Following is an excerpt of the code that i am using to test it:

     

    Code Snippet

                test1PSInfo = New System.Diagnostics.ProcessStartInfo("notepad.exe")
                test1PSInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
                test1PSInfo.Arguments = """c:\build.xml"""
                test1Process = System.Diagnostics.Process.Start(test1PSInfo)
                Dim handle As System.IntPtr = test1Process.MainWindowHandle

                Dim b As Boolean = MoveWindow(handle.ToInt32(), 0, 0, 0, 0, True)

     

     

     

     

    Tuesday, March 04, 2008 1:43 AM

Answers

  • Hi FgInx,

    Try the following code snippet. It shoulds satisfy your requirement.

    Imports System.Runtime.InteropServices
    Public Class Form1
        <DllImport("user32.dll")> _
        Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
                                   ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean

        End Function
        Dim calculatorhandle As IntPtr
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim p As New Process
            p.StartInfo.FileName = "notepad.exe"   '' put your execution application here
            p.Start() '---- Start the process
            p.WaitForInputIdle() '---- Wait for the message loop to be created on this process
            Debug.WriteLine(p.MainWindowHandle.ToString & ": " & p.MainWindowTitle)
            calculatorhandle = p.MainWindowHandle
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MoveWindow(calculatorhandle, Me.Right + 100, Me.Bottom + 100, 200, 200, True)
        End Sub
    End Class


    Best regards,
    Riquel
    Thursday, March 06, 2008 2:14 PM
    Moderator

All replies

  • Hi FgInx,

    Try the following code snippet. It shoulds satisfy your requirement.

    Imports System.Runtime.InteropServices
    Public Class Form1
        <DllImport("user32.dll")> _
        Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, _
                                   ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean

        End Function
        Dim calculatorhandle As IntPtr
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim p As New Process
            p.StartInfo.FileName = "notepad.exe"   '' put your execution application here
            p.Start() '---- Start the process
            p.WaitForInputIdle() '---- Wait for the message loop to be created on this process
            Debug.WriteLine(p.MainWindowHandle.ToString & ": " & p.MainWindowTitle)
            calculatorhandle = p.MainWindowHandle
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            MoveWindow(calculatorhandle, Me.Right + 100, Me.Bottom + 100, 200, 200, True)
        End Sub
    End Class


    Best regards,
    Riquel
    Thursday, March 06, 2008 2:14 PM
    Moderator
  • Thanks. This solution works perfectly.

     

    Tuesday, March 11, 2008 7:29 PM