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

4

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


将自己的应用程序最小化/最大化,还是在另一个应用程序上进行操作? - JohnFx
好的,我想让我的应用程序最大化/最小化另一个与我的应用程序无关的应用程序,就像任务管理器一样。 - user959631
1个回答

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

嘿,老兄,非常感谢。你是刚刚想出来的吗,还是之前用过?如果你是刚刚想出来的,我爱死你了!哈哈 =P 谢谢 - user959631
之前使用过这种技术。不得不查找一些常量和API方法(ShowWindow),但除此之外,都是从头开始编写的。请不要将其用于恶意行为(例如创建一个超级应用程序,将所有其他内容最小化,以便成为桌面之王)。 - JohnFx
笑死,桌面之王。不,我想用它来关闭一些总是弹出的烦人东西,然后关闭其进程并最大化我的窗口 =] 无论如何还是谢谢。 - user959631

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接