Minimizing all open windows in C#

拥有回忆 提交于 2019-11-26 09:08:46

问题


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?


回答1:


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



回答2:


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. =)




回答3:


A similar result can be achieved by sending these keyboard events to bring the Taskbar popup menu and send it the letter "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);
    }
}



回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/785054/minimizing-all-open-windows-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!