在 C# 中最小化所有打开的窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/785054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 01:02:46  来源:igfitidea点击:

Minimizing all open windows in C#

c#.netwindowminimize

提问by anonymous coward

I saw this C++ code on a forum which minimizes all open windows

我在一个论坛上看到了这个 C++ 代码,它最小化了所有打开的窗口

#define MIN_ALL        419
#define MIN_ALL_UNDO   416

int main(int argc, char* argv[])
{
    HWND lHwnd = FindWindow("Shell_TrayWnd",NULL);
    SendMessage(lHwnd,WM_COMMAND,MIN_ALL,0);
    Sleep(2000);
    SendMessage(lHwnd,WM_COMMAND,MIN_ALL_UNDO,0);
    return 0;
}

How can I access the FindWindow and SendMessage API function and the HWND type in C#.net?

如何在 C#.net 中访问 FindWindow 和 SendMessage API 函数以及 HWND 类型?

采纳答案by Stanislav Kniazev

PInvoke.net is your friend :-)

PInvoke.net 是您的朋友 :-)

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1 {
class Program {
    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true)]
    static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);

    const int WM_COMMAND = 0x111;
    const int MIN_ALL = 419;
    const int MIN_ALL_UNDO = 416;

    static void Main(string[] args) {
        IntPtr lHwnd = FindWindow("Shell_TrayWnd", null);
        SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL, IntPtr.Zero); 
        System.Threading.Thread.Sleep(2000);
        SendMessage(lHwnd, WM_COMMAND, (IntPtr)MIN_ALL_UNDO, IntPtr.Zero);
    }
}
}

回答by J. Steen

The site www.pinvoke.net has a lot of the information you require. For instance, this article on SendMessage and FindWindow:

网站 www.pinvoke.net 有很多您需要的信息。例如,这篇关于 SendMessage 和 FindWindow 的文章:

http://www.pinvoke.net/default.aspx/user32.SendMessagehttp://www.pinvoke.net/default.aspx/user32.FindWindow

http://www.pinvoke.net/default.aspx/user32.SendMessage http://www.pinvoke.net/default.aspx/user32.FindWindow

It's rather technical - of course - but basically you use p/invoke to call on the FindWindow and SendMessage API functions to accomplish what you want. =)

这是相当技术性的 - 当然 - 但基本上你使用 p/invoke 来调用 FindWindow 和 SendMessage API 函数来完成你想要的。=)

回答by PatTech

Not exactly the easiest way, but the manual way is to call the C++ implementation. http://pinvoke.nethelps:

不完全是最简单的方,但手动方是调用 C++ 实现。 http://pinvoke.net帮助:

findwindow search results: http://pinvoke.net/search.aspx?search=findwindow&namespace=[All]

findwindow 搜索结果:http://pinvoke.net/search.aspx?search=findwindow&namespace=[All ]

approximately the fourth result down helps in your case.

大约第四个结果对您的情况有所帮助。

回答by Mark S. Rasmussen

I've previously blogged on how to minimize & maximize using P/Invoke from C#: http://improve.dk/minimizing-and-maximizing-windows/

我以前写过关于如何使用 C# 中的 P/Invoke 最小化和最大化的博客:http: //improve.dk/minimizing-and-maximizing-windows/

回答by Groo

A similar result can be achieved by sending these keyboard events to bring the Taskbar popup menu and send it the letter "M":

类似的结果可以通过发送这些键盘来带来任务栏弹出菜单并发送字母“M”来实现:

public class DesktopHelper
{
    [DllImport("user32.dll")]
    static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, int dwExtraInfo);

    /// <summary>
    /// Shows the desktop.
    /// </summary>
    public static void ShowDesktop()
    {
        keybd_event(0x5B, 0, 0, 0);
        keybd_event(0x4D, 0, 0, 0);
        keybd_event(0x5B, 0, 0x2, 0);
    }
}