Windows 8中的MoveWindow API

时间:2013-10-30 14:27:23

标签: windows winforms winapi windows-8

我需要从外部exe(例如记事本)打开外部窗口并移动&将其大小调整为预定义的大小&位置。

我正在尝试使用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 个答案:

答案 0 :(得分: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