gpt4 book ai didi

c# - 如何以编程方式最小化打开的窗口文件夹

转载 作者:可可西里 更新时间:2023-11-01 08:06:16 26 4
gpt4 key购买 nike

如何获取已打开文件夹的列表、枚举并以编程方式最小化每个文件夹?

有时,当从应用程序中的一种形式跳转到另一种形式时,一些打开的文件夹确实会从工具中窃取焦点。防止这种情况是我们客户的重中之重。客户是视障人士,因此他们只能通过屏幕阅读器访问机器。最小化其他窗口(文件夹)根本不是问题,实际上是必需的。

我试过这个:

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

正如预期的那样,它并没有什么好处。

更新:

根据这里的答案,我试过这个:

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

这会创建大量不可见的资源管理器窗口,这些窗口会突然突然出现在任务栏中。我在处理 Windows API 方面有点菜鸟,所以代码本身实际上会有所帮助。

最佳答案

请试试这个(代码有点乱,但出于目的你应该能够通过它;))

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Text;
using System.Globalization;

namespace WindowsFormsApplication20
{
static class Program
{
[STAThread]
static void Main()
{
foreach (IntPtr handle in EnumerateProcessWindowHandles(Process.GetProcessesByName("explorer")[0].Id))
{
SendMessage(handle, WM_SYSCOMMAND, SC_MINIMIZE, 0);
}
}

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

static string GetDaClassName(IntPtr hWnd)
{
int nRet;
StringBuilder ClassName = new StringBuilder(100);
//Get the window class name
nRet = GetClassName(hWnd, ClassName, ClassName.Capacity);
if (nRet != 0)
{
return ClassName.ToString();
}
else
{
return null;
}
}

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)
{
string className = GetDaClassName(hWnd);

switch (className)
{
case null:
break;
case "ExploreWClass":
handles.Add(hWnd);
break;
case "CabinetWClass":
handles.Add(hWnd);
break;
default:
break;
}

return true;
};

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

return handles;
}

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, int wParam, int lParam);

const int WM_SYSCOMMAND = 274;
const int SC_MINIMIZE = 0xF020;
}
}

最好的问候,

祖布鲁卡

关于c# - 如何以编程方式最小化打开的窗口文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9254037/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com