How to find a desktop window (by window name) in Windows 8.1 Update 2 OS, using the Win32 API FindWindow() in PowerShell environment?

后端 未结 4 996
鱼传尺愫
鱼传尺愫 2021-01-19 05:53

I don\'t remember having any problem finding a window in older Windows OS\'s, but, I\'m not succeeding in Windows 8.1 Update 2 OS, using PowerShell v4.0.

4条回答
  •  遥遥无期
    2021-01-19 06:33

    The following code works fine for me, thanks to @zyq's advice

    [System.Windows.Forms.MessageBox]::Show('Test', 'PowerShell Dialog', 
            [Windows.Forms.MessageBoxButtons]::OK, 
            [Windows.Forms.MessageBoxIcon]::Information, 
            [Windows.Forms.MessageBoxDefaultButton]::Button1,
            [Windows.Forms.MessageBoxOptions]::ServiceNotification
            )
    
    $Win32API = Add-Type -Name Funcs -Namespace Win32 -PassThru -MemberDefinition @'
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, IntPtr lpWindowName);
    
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindow(IntPtr lpClassName, string lpWindowName);
    '@
    
    $Win32API::FindWindow('#32770',       'PowerShell Dialog')
    $Win32API::FindWindow([IntPtr]::Zero, 'PowerShell Dialog')
    $Win32API::FindWindow('#32770',       [IntPtr]::Zero)
    $Win32API::FindWindow('Notepad',      [IntPtr]::Zero)
    

提交回复
热议问题