如何在vb中使与应用程序无关的窗口最小化或最大化其窗口状态?

use*_*631 4 vb.net minimize maximize visual-studio

如果您在任务管理器中注意到,当您右键单击正在运行的任务时,您会看到许多选项,其中包括“最小化”和“最大化”。有没有办法在vb中实现这一点?

Joh*_*nFx 5

这是您正在寻找的代码的示例。它将循环遍历所有活动进程并最小化所有窗口。

在您的应用程序中,您可能需要使用类似的方法Process.GetProcessesByName来查找您想要操作的特定窗口。

Imports System.Runtime.InteropServices

Module ManipulateWindows

    Const SW_HIDE As Integer = 0
    Const SW_RESTORE As Integer = 1
    Const SW_MINIMIZE As Integer = 2
    Const SW_MAXIMIZE As Integer = 3

    <DllImport("User32")> _
    Private Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer
    End Function

    Public Sub Main()

        'iterate through all the open processes.
        For Each p As Process In Process.GetProcesses    

            'Get the Window Handle
            Dim hWnd as integer = CType(p.MainWindowHandle, Integer)

            'Write out the title of the main window for the process.
            System.Console.WriteLine(p.MainWindowTitle)

            'Minimize the Window
            ShowWindow(hWnd, SW_MINIMIZE)
        Next p    
    End Sub    
End Module
Run Code Online (Sandbox Code Playgroud)