天天看點

c# FindWindow 的用法,c#重寫api,FindWindow

FindWindow这个函数检索处理顶级窗口的类名和窗口名称匹配指定的字符串。

函数原型:

HWND FindWindow
(
LPCSTR lpClassName,
LPCSTR lpWindowName
);           

IpClassName :指向一个指定了类名的空结束字符串,或一个标识类名字符串的成员的指针。如果该参数为一个成员,则它必须为前次调用theGlobafAddAtom函数产生的全局成员。该成员为16位,必须位于IpClassName的低 16位,高位必须为 0。

IpWindowName:指向一个指定了窗口名(窗口标题)的空结束字符串。如果该参数为空,则为所有窗口全匹配。

C# 中的用法

using System.Runtime.InteropServices;

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern IntPtr FindWindow(string strClass, string strWindow);           

使用:

IntPtr __handle;
            __handle = FindWindow("Notepad", null);
            if (__handle!=IntPtr.Zero){
            
            }
            __handle = FindWindow(null, "无标题 - 记事本");
            if (__handle != IntPtr.Zero)
            {

            }           

返回结果:

如果函数成功,返回值为具有指定类名和窗口名的窗口句柄;

如果函数失败,返回值为0。

繼續閱讀