Visual Basic & MoveWindow sending external app window way off screen

932 views Asked by At

I'm trying to position a couple of external windows from within my application but either end up with just a piece of the title bar on screen near the top left or the window goes far to the right and ends up at x = 32767

The function declarations are in a module and the Move_Notepad Sub is part of a form.

    Option Explicit On

    Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Declare Function MoveWindow Lib "user32.dll" Alias "MoveWindow" (ByVal hwnd As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal bRepaint As Long) As Long


    Public Sub Move_Notepad()
         'Move NotePad to 1,1 and set its Width and height to 800,720 (pixels)
         Dim GoWindow As Long
         hWinHand = FindWindow(vbNullString, "Untitled - Notepad")
         GoWindow = MoveWindow(hWinHand, 1, 1, 800, 720, 1)
    End Sub

    Private Sub bMove_Notepad_Click(sender As Object, e As EventArgs) Handles bMove_Notepad.Click
        Move_Notepad()
    End Sub
1

There are 1 answers

0
IanJF On

I installed the PInvoke.net Visual Studio Extension extension and got the following code which finallyworked for me:

    <DllImport("user32.dll")>
    Public 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
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    Public Function FindWindow(
    ByVal lpClassName As String,
    ByVal lpWindowName As String) As IntPtr
    End Function