gpt4 book ai didi

lambda - 这个 lambda 表达式的 C# 2.0 等效代码是什么

转载 作者:行者123 更新时间:2023-12-04 05:56:49 25 4
gpt4 key购买 nike

我需要一个通过打开的资源管理器窗口枚举的功能。这是我得到的代码:

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

foreach (ProcessThread thread in Process.GetProcessById(processID).Threads)
{ //what is the magic going on beneath this?? :o
EnumThreadWindows(thread.Id, (hWnd, lParam) => { handles.Add(hWnd); return true;}, IntPtr.Zero);
}
return handles;
}

代码在这里继续,如下所示:
[DllImport("coredll.dll")]
static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_MINIMIZED = 6;

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

我的问题是,在第一个代码块中,如何替换 lambda 表达式,以便我可以在 VS 2005 中使用 C# 2.0 编译代码。

最佳答案

创建一个新方法传递给 EnumThreadWindows像这样:

static bool EnumThreadCallback(IntPtr hWnd, IntPtr lParam)
{
// Close the enumerated window.
PostMessage(hWnd, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

return true;
}

并让一个代表一起去:
public delegate bool EnumThreadDelegate (IntPtr hWnd, IntPtr lParam);

然后像这样调用函数:
foreach (ProcessThread pt in proc.Threads)
{
EnumThreadWindows((uint)pt.Id, new EnumThreadDelegate(EnumThreadCallback), IntPtr.Zero);
}

来源: http://www.pinvoke.net/default.aspx/user32/EnumThreadWindows.html

关于lambda - 这个 lambda 表达式的 C# 2.0 等效代码是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9415294/

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