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

发布于 2024-07-18 02:52:40 字数 400 浏览 7 评论 0原文

我在论坛上看到了这段 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;
}

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

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

#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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

如梦 2024-07-25 02:52:40

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);
    }
}
}

PInvoke.net is your friend :-)

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);
    }
}
}
梦巷 2024-07-25 02:52:40

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

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

当然,这相当技术性,但基本上您可以使用 p/invoke 来调用 FindWindow 和 SendMessage API 函数来完成您想要的任务。 =)

The site www.pinvoke.net has a lot of the information you require. For instance, this article on SendMessage and 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. =)

心意如水 2024-07-25 02:52:40

我之前曾在博客中介绍过如何最小化& 使用 C# 中的 P/Invoke 最大化:
http://improve.dk/minimizing-and-maximizing-windows/

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

萌能量女王 2024-07-25 02:52:40

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

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

大约第四个结果下来对你的情况有帮助。

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

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

approximately the fourth result down helps in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文