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);