2013-10-30 14 views
1

我需要從外部exe文件(例如記事本)打開一個外部窗口,並將它移動到預定義大小&的位置。Windows中的MoveWindow API 8

我想使用MoveWindow API,但似乎它不工作。我正在使用Windows 8 x64和VS2012。

這裏是我的代碼:

<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 

Public Sub NoveNotepad() 
    Dim ApplicationProcess = System.Diagnostics.Process.Start("Notepad.exe") 
    ApplicationProcess.WaitForInputIdle() 
    Dim ApplicationHandle = ApplicationProcess.MainWindowHandle 
    Dim z = MoveWindow(ApplicationHandle, 600, 600, 600, 600, True) ' THIS RETURNS TRUE 
End Sub 
+1

因此,您需要做文檔告訴您要做的事情。當MoveWindow返回false時,通過調用'GetLastError'找到更多信息。在.net中,您可以通過在DllImport屬性中將SetLastError設置爲True並讀取Marshal.GetLastWin32Error來完成此操作。請顯示結果。 –

+0

我在x64上運行Win8.1,並且在將「Shared」添加到API聲明後,該代碼正常工作。 –

+0

對不起,現在Api調用返回True。 至於共享,我在模塊中使用它,所以不需要使用共享。 – JPScerri

回答

0

你可以嘗試使用SetWindowPos()呢?不知道爲什麼MoveWindow()不會工作...

Private Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" _ 
    (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As Integer, _ 
    ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, _ 
    ByVal wFlags As Integer) As Integer 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    Dim ApplicationProcess = System.Diagnostics.Process.Start("Notepad.exe") 
    ApplicationProcess.WaitForInputIdle() 

    Dim ApplicationHandle = ApplicationProcess.MainWindowHandle 
    SetWindowPos(ApplicationHandle, 0, 600, 600, 600, 600, 0) 
End Sub 
+0

仍然沒有進展。一模一樣。 SetWindowsPos()返回1。 – JPScerri