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

Ale*_*lex 7 c#

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

cry*_*ted 13

试试以下,

    // 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)
Run Code Online (Sandbox Code Playgroud)

编辑: 您可以使用以下声明

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

编辑: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);
        }

    }
Run Code Online (Sandbox Code Playgroud)

  • 我不得不使用 `FindWindow(null, caption);` 而不是 `FindWindow(String.Empty, caption);` (3认同)