1.查找窗口
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
lpClassName:要找的窗口类,一般null
lpWindowName:要找的窗口标题
2.发送消息到一个或多个窗口
[DllImport("User32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);
hwnd:窗口句柄(类似FindWindow返回值)
wMsg:区别于其他消息的常量值,16进制数
wParam:通常是一个与消息有关的常量值,也可能是窗口或控件的句柄 0
lParam:一个指向内存中数据的指针 0
3.查找子窗体
[DllImport("User32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);
hwndParent:查找子窗体的父窗体句柄,若为null则以桌面为父窗体
hwndChildAfter:子窗体句柄
lpszClass:只想一个指定了类名的空结束字符串,或标识一个类名字符串成员的指针
lpszWindow:指向一个指定了窗体名(窗体标题)的空结束字符串
4.获取进程接口ID
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
hwnd:窗口句柄,由FindWindow获取
ID:存放进程ID的变量
5.减少内存占用
[DllImport("KERNEL32.DLL", EntryPoint = "SetProcessWorkingSetSize", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
internal static extern bool SetProcessWorkingSetSize(IntPtr pProcess, int dwMinimumWorkingSetSize, int dwMaximumWorkingSetSize);
[DllImport("KERNEL32.DLL", EntryPoint = "GetCurrentProcess", SetLastError = true, CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr GetCurrentProcess();
调用:IntPtr pHandle = GetCurrentProcess();
SetProcessWorkingSetSize(pHandle,-1,-1);