none
move notepad window

    Question

  • Hey

    How can I move an external application. For example, notepad.location = 200, 200

    how would I tell the program what notepad is?

    Thanks


    Thanks

    Tuesday, May 01, 2012 11:09 PM

Answers

  • Hi,

    you could use the SetWindowPos api: http://www.pinvoke.net/default.aspx/user32.setwindowpos

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll", EntryPoint:="SetWindowPos")> _
        Public Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As Integer, x As Integer, Y As Integer, cx As Integer, cy As Integer, _
            wFlags As Integer) As IntPtr
        End Function
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim p As New Process()
            p.StartInfo.FileName = "Notepad.exe"
            p.Start()
    
            While p.MainWindowHandle = IntPtr.Zero
                System.Threading.Thread.Sleep(10)
            End While
    
            'const short SWP_NOMOVE = 0X2;
            Const SWP_NOSIZE As Short = 1
            Const SWP_NOZORDER As Short = &H4
            Const SWP_SHOWWINDOW As Integer = &H40
    
            Dim handle As IntPtr = p.MainWindowHandle
            If handle <> IntPtr.Zero Then
                SetWindowPos(handle, 0, 200, 200, 0, 0, _
                 SWP_NOZORDER Or SWP_NOSIZE Or SWP_SHOWWINDOW)
            End If
        End Sub
    End Class

    Regards,

      Thorsten


    Wednesday, May 02, 2012 3:56 AM

All replies

  • Hi,

    you could use the SetWindowPos api: http://www.pinvoke.net/default.aspx/user32.setwindowpos

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll", EntryPoint:="SetWindowPos")> _
        Public Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As Integer, x As Integer, Y As Integer, cx As Integer, cy As Integer, _
            wFlags As Integer) As IntPtr
        End Function
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim p As New Process()
            p.StartInfo.FileName = "Notepad.exe"
            p.Start()
    
            While p.MainWindowHandle = IntPtr.Zero
                System.Threading.Thread.Sleep(10)
            End While
    
            'const short SWP_NOMOVE = 0X2;
            Const SWP_NOSIZE As Short = 1
            Const SWP_NOZORDER As Short = &H4
            Const SWP_SHOWWINDOW As Integer = &H40
    
            Dim handle As IntPtr = p.MainWindowHandle
            If handle <> IntPtr.Zero Then
                SetWindowPos(handle, 0, 200, 200, 0, 0, _
                 SWP_NOZORDER Or SWP_NOSIZE Or SWP_SHOWWINDOW)
            End If
        End Sub
    End Class

    Regards,

      Thorsten


    Wednesday, May 02, 2012 3:56 AM
  • Argh so complicated. I thought it would be as easy as me.location etc. anyway do you know how to put in the string for ex: IF it contains Notepad, instead of notepad.exe?

    Thanks

    Wednesday, May 02, 2012 3:58 AM
  • Hi,

    you could use the SetWindowPos api: http://www.pinvoke.net/default.aspx/user32.setwindowpos

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        <DllImport("user32.dll", EntryPoint:="SetWindowPos")> _
        Public Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As Integer, x As Integer, Y As Integer, cx As Integer, cy As Integer, _
            wFlags As Integer) As IntPtr
        End Function
    
        Public Sub New()
            InitializeComponent()
        End Sub
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim p As New Process()
            p.StartInfo.FileName = "Notepad.exe"
            p.Start()
    
            
            'const short SWP_NOMOVE = 0X2;
            Const SWP_NOSIZE As Short = 1
            Const SWP_NOZORDER As Short = &H4
            Const SWP_SHOWWINDOW As Integer = &H40
    
            Dim handle As IntPtr = p.MainWindowHandle
            If handle <> IntPtr.Zero Then
                SetWindowPos(handle, 0, 200, 200, 0, 0, _
                 SWP_NOZORDER Or SWP_NOSIZE Or SWP_SHOWWINDOW)
            End If
        End Sub
    End Class

    Regards,

      Thorsten


    why?:

    While p.MainWindowHandle = IntPtr.Zero
        System.Threading.Thread.Sleep(10)
    End While
    

    you can use:

    p.waitforinputidle


    thanks for any help

    Wednesday, May 02, 2012 4:02 AM
  • You can use the SendWindow API:
    Private Sub Main()
    	Dim notepad = Process.GetProcessesByName("notepad").Single()
    
    	Dim rect = New Rect()
    	GetWindowRect(notepad.MainWindowHandle, rect)
    	MoveWindow(notepad.MainWindowHandle, 200, 200, rect.Right - rect.Left, rect.Bottom - rect.Top, True)
    End Sub
    
    Friend Structure Rect
    	Public Left As Integer
    	Public Top As Integer
    	Public Right As Integer
    	Public Bottom As Integer
    End Structure
    
    <DllImport("user32.dll", CharSet := CharSet.Auto, CallingConvention := CallingConvention.StdCall, ExactSpelling := True, SetLastError := True)> _
    Friend Shared Function GetWindowRect(ByVal handle As IntPtr, ByRef rect As Rect) As Boolean
    End Function
    
    <DllImport("user32.dll", CharSet := CharSet.Auto, CallingConvention := CallingConvention.StdCall, ExactSpelling := True, SetLastError := True)> _
    Friend Shared Sub MoveWindow(ByVal handle As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal repaint As Boolean)
    End Sub

    Matteo Migliore

    Bloghttp://blogs.ugidotnet.org/matteomigliore
    Twitterhttp://twitter.com/matteomigliore
    CodePlex

    Wednesday, May 02, 2012 4:06 AM