此 lambda 表达式的 C# 2.0 等效代码是什么
我需要一个通过打开的资源管理器窗口进行枚举的功能。这是我得到的代码:
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 编译代码。
I need a functionality that enumerates through open explorer windows. And here is a code I've got:
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;
}
And the code continues here like this:
[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);
}
}
My question is, in the first block of code, how do I replace the lambda expression so that I can compile the code using C# 2.0 in VS 2005.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
创建一个新方法来传递到
EnumThreadWindows
中,如下所示:并创建一个委托来配合它:
然后像这样调用该函数:
来源: http://www.pinvoke.net/default.aspx/user32/EnumThreadWindows.html
Create a new method to pass into
EnumThreadWindows
like this:And make a delegate to go along with it:
Then call the function like this:
Source: http://www.pinvoke.net/default.aspx/user32/EnumThreadWindows.html