如何以编程方式最小化打开的窗口中的文件夹 [英] How to programmatically minimize opened window folders

查看:154
本文介绍了如何以编程方式最小化打开的窗口中的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能得到的文件夹打开列表,列举通过它和每个文件夹编程最小化?

有时一些打开的文件夹执行从工具窃取焦点中的应用程序到另一个从一种形式跳跃时。 preventing这是为我们的客户高优先级的。客户是视障人士,所以他们只能通过屏幕阅读器访问机器。最大限度地减少其它窗口(文件夹)是不是所有的问题,实际上是一个要求。

我试过这样:

 的foreach(在Process.GetProcessesByName进程p(资源管理器))
{
    p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
}

正如预期的那样它确实没有好。

更新

答案在这里,我想这样的:

 委托布尔EnumThreadDelegate(IntPtr的的HWND,IntPtr的lParam的);    函数[DllImport(user32.dll中)]
    静态外部布尔EnumThreadWindows(INT dwThreadId,EnumThreadDelegate lpfn,IntPtr的lParam的);    静态的IEnumerable< IntPtr的> EnumerateProcessWindowHandles(INT进程ID)
    {
        清单< IntPtr的>处理=新的List< IntPtr的>();        EnumThreadDelegate addWindowHandle =委托(IntPtr的的HWND,IntPtr的参数)
        {
            handles.Add(HWND);
            返回true;
        };        的foreach(在Process.GetProcessById(进程ID).Threads ProcessThread螺纹)
            EnumThreadWindows(thread.Id,addWindowHandle,IntPtr.Zero);        返回的句柄;
    }    const int的SW_MINIMIZED = 6;    函数[DllImport(user32.dll中)]
    静态外部INT的ShowWindow(IntPtr的的HWND,INT的nCmdShow);    私人无效的button1_Click(对象发件人,EventArgs的发送)
    {
        的foreach(IntPtr的手柄EnumerateProcessWindowHandles(Process.GetProcessesByName(资源管理器)[0] .ID))
            的ShowWindow(手柄,SW_MINIMIZED);
    }

这创造了一大堆看不见的资源管理器窗口的在taksbar突然罗列出来没有在那里。我在处理的Windows API有点小白,所以code本身实际上将帮助。


解决方案

有一个不太哈克的解决方案比这里可以接受的答案:的最小化

它基于的壳牌对象的脚本 。示例:

  const int的SW_SHOWMINNOACTIVE = 7;函数[DllImport(user32.dll中)]
静态外部布尔的ShowWindow(IntPtr的的HWND,INT的nCmdShow);静态无效MinimizeWindow(IntPtr的手柄)
{
    的ShowWindow(手柄,SW_SHOWMINNOACTIVE);
}//调用它:的foreach(在新的外壳()。视窗的IWebBrowser2窗口())
{
    如果(window.Name ==Windows资源管理器)
        MinimizeWindow((IntPtr的)window.HWND);
}

可以使用 Internet Explorer中取得同样的事情对象模型

  //添加引用Microsoft Internet控制COM组件
//还添加了使用SHDOCVW;'
的foreach(在新ShellWindows的IWebBrowser2窗口())
{
    如果(window.Name ==Windows资源管理器)
        MinimizeWindow((IntPtr的)window.HWND);
}

How can I get the list of opened of folders, enumerate through it and minimize each folder programmatically?

At times some opened folders do steal focus from the tool when jumping from one form in the application to another. Preventing this is of high priority for our client. The customers are visually impaired people, so they access the machine only via screen readers. Minimizing other windows (folders) is not at all a problem, in fact a requirement.

I tried this:

foreach (Process p in Process.GetProcessesByName("explorer"))
{
    p.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
}

As expected it did no good.

Update:

From the answers here, I tried this:

    delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);

    [DllImport("user32.dll")]
    static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);

    static IEnumerable<IntPtr> EnumerateProcessWindowHandles(int processID)
    {
        List<IntPtr> handles = new List<IntPtr>();

        EnumThreadDelegate addWindowHandle = delegate(IntPtr hWnd, IntPtr param)
        {
            handles.Add(hWnd);
            return true;
        };

        foreach (ProcessThread thread in Process.GetProcessById(processID).Threads)                              
            EnumThreadWindows(thread.Id, addWindowHandle, IntPtr.Zero);

        return handles;
    }

    const int SW_MINIMIZED = 6;

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

    private void button1_Click(object sender, EventArgs e)
    {
        foreach (IntPtr handle in EnumerateProcessWindowHandles(Process.GetProcessesByName("explorer")[0].Id))
            ShowWindow(handle, SW_MINIMIZED);
    }

This creates a whole lot of invisible explorer windows to be suddenly listed in the taksbar out of no where. I am bit noob in dealing with Windows API, so the code itself will actually help.

解决方案

There is a less 'hacky' solution than the accepted answer available here: Minimize a folder

It's based on the Shell Objects for Scripting. Sample:

const int SW_SHOWMINNOACTIVE = 7;

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

static void MinimizeWindow(IntPtr handle)
{
    ShowWindow(handle, SW_SHOWMINNOACTIVE);
}

//call it like:

foreach (IWebBrowser2 window in new Shell().Windows())
{
    if (window.Name == "Windows Explorer")
        MinimizeWindow((IntPtr)window.HWND);
}

The same thing can be achieved using the Internet Explorer Object Model

// add a reference to "Microsoft Internet Controls" COM component
// also add a 'using SHDocVw;'
foreach (IWebBrowser2 window in new ShellWindows())
{
    if (window.Name == "Windows Explorer")
        MinimizeWindow((IntPtr)window.HWND);
}

这篇关于如何以编程方式最小化打开的窗口中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆