此 lambda 表达式的 C# 2.0 等效代码是什么

发布于 2025-01-08 01:41:05 字数 1137 浏览 1 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生共白头 2025-01-15 01:41:05

创建一个新方法来传递到 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

Create a new method to pass into EnumThreadWindows like this:

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

    return true;
}

And make a delegate to go along with it:

public delegate bool EnumThreadDelegate (IntPtr hWnd, IntPtr lParam);

Then call the function like this:

foreach (ProcessThread pt in proc.Threads)
{
    EnumThreadWindows((uint)pt.Id, new EnumThreadDelegate(EnumThreadCallback), IntPtr.Zero); 
}

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

流云如水 2025-01-15 01:41:05
delegate bool EnumThreadDelegate(IntPtr hWnd, 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;
}
delegate bool EnumThreadDelegate(IntPtr hWnd, 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文