c#如何使用user32 dll处理特定窗口

时间:2011-04-19 07:22:57

标签: c#

如何使用用户32 .dll处理特定窗口?我在c#工作。谢谢。有人能给我一个简短的例子吗?:)我很感激! (方法:getwindowtext,show window,enumwindowcallback normal)。

1 个答案:

答案 0 :(得分:12)

尝试以下,

    // For Windows Mobile, replace user32.dll with coredll.dll
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

// Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.

[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

// You can also call FindWindow(default(string), lpWindowName) or FindWindow((string)null, lpWindowName)

修改 您可以使用以下声明

 // Find window by Caption
        public static IntPtr FindWindow(string windowName)
        {
            var hWnd = FindWindow(windowName, null); 
            return hWnd;
        }

修改:2     这是代码的简明版本:

   public class WindowFinder
    {
        // For Windows Mobile, replace user32.dll with coredll.dll
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        public static IntPtr FindWindow(string caption)
        {
            return FindWindow(String.Empty, caption);
        }

    }