【问题标题】:Minimize a folder最小化文件夹
【发布时间】:2014-01-12 14:57:21
【问题描述】:

我想用 C# 最小化一个窗口

例如:我使用

打开了这条路径 E:\
process.start(E:\)

我想在某个事件中最小化这条路径。

我怎样才能做到这一点?

【问题讨论】:

标签: c# .net


【解决方案1】:

以下示例控制台应用程序代码将最小化在 E:\ 上打开的所有 shell 浏览器视图:

class Program
{
    static void Main(string[] args)
    {
        // add a reference to "Microsoft Shell Controls and Automation" COM component
        // also add a 'using Shell32;'
        Shell shell = new Shell();
        dynamic windows = shell.Windows(); // this is a ShellWindows object
        foreach (dynamic window in windows)
        {
            // window is an WebBrowser object
            Uri uri = new Uri((string)window.LocationURL);
            if (uri.LocalPath == @"E:\")
            {
                IntPtr hwnd = (IntPtr)window.HWND; // WebBrowser is also an IWebBrowser2 object
                MinimizeWindow(hwnd);
            }
        }
    }

    static void MinimizeWindow(IntPtr handle)
    {
        const int SW_MINIMIZE = 6;
        ShowWindow(handle, SW_MINIMIZE);
    }

    [DllImport("user32.dll")]
    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}

它使用Shell Objects for Scripting。注意这里必须使用动态关键字,因为没有很酷的类型库,因此也没有智能感知。

【讨论】:

    【解决方案2】:

    Shell32.Shell objShell = new Shell32.Shell(); objShell.MinimizeAll(); 这将帮助您最小化所有窗口不仅是所有文件夹(类似于 windows + M !!!

    【讨论】:

    • OP的问题不是最小化所有窗口,而是最小化他之前打开的路径。
    • 你知道答案吗?我现在也在寻找同样的东西!
    • objShell.ToggleDesktop 显示桌面(和切换),相当于windows + D :)
    【解决方案3】:

    你的问题不是很清楚。如果您使用的是 TreeView 控件,请参见 MSDNTreeview class。然后,您可以:随意展开或折叠项目。

    【讨论】:

      【解决方案4】:

      您可以使用配置文件或变量

      【讨论】:

        【解决方案5】:

        这是一个可能的解决方案,只会最小化你打开的窗口:

        private int explorerWindowNumber;
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MINIMIZE = 0xF020;
        
        [DllImport("user32.dll", SetLastError = true)]
        public static extern int GetForegroundWindow();
        
        [DllImport("user32.dll")]
        public static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
        
        public void button1_Click(object sender, EventArgs e)
        {
            //Start my window
            StartMyExplorer();
        }
        
        private void StartMyExplorer()
        {
            Process.Start("D:\\");
            Thread.Sleep(1000);
            //Get the window id (int)
            explorerWindowNumber = GetForegroundWindow();
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            //Minimize the window i created
            SendMessage(explorerWindowNumber, WM_SYSCOMMAND, SC_MINIMIZE, 0);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-04
          • 2021-02-06
          相关资源
          最近更新 更多