public class Note { //声明 API 函数 [DllImport( "User32.dll" , EntryPoint = "SendMessage" )] private static extern IntPtr SendMessage( int hWnd, int msg, IntPtr wParam, IntPtr lParam); [DllImport( "User32.dll" , EntryPoint = "FindWindow" )] private static extern int FindWindow( string lpClassName, string lpWindowName); //定义消息常数 public const int CUSTOM_MESSAGE = 0X400 + 2; //自定义消息 //向窗体发送消息的函数 public void SendMsgToMainForm( int MSG) { int WINDOW_HANDLER = FindWindow( null , "协同标绘" ); if (WINDOW_HANDLER == 0) { throw new Exception( "Could not find Main window!" ); } long result = SendMessage(WINDOW_HANDLER, CUSTOM_MESSAGE, new IntPtr(14), IntPtr.Zero).ToInt64(); } } |
在协同标绘窗口里拦截消息的函数:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | protected override void WndProc( ref System.Windows.Forms.Message msg) { switch (msg.Msg) { case Note.CUSTOM_MESSAGE: //处理消息 { switch (msg.WParam.ToString()) { case "11" : //对象添加 string s = "mdesheng" ; addOne(s); break ; case "12" : //对象更新 string sn = "m" ; updateID(sn); break ; case "13" : //对象删除 deleteID( "5" ); break ; case "14" : //与会者信息更新 updateClientInfor( "in_1_张三_在线" ); break ; } } break ; default : base .WndProc( ref msg); //调用基类函数处理非自定义消息。 break ; } <br data-filtered= "filtered" >} private void button1_Click( object sender, EventArgs e) { Note a = new Note(); a.SendMsgToMainForm(Note.CUSTOM_MESSAGE); } |
FindWindow()函数的用法。要在C#里使用该API,写出FindWindow()函数的声明:
[DllImport("coredll.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
这个函数有两个参数,第一个是要找的窗口的类,第二个是要找的窗口的标题,是窗体的Text名字,不是name。在搜索的时候不一定两者都知道,但至少要知道其中的一个。有的窗口的标题是比较容易得到的,如"计算器",所以搜索时应使用标题进行搜索。但有的软件的标题不是固定的,如"记事本",如果打开的文件不同,窗口标题也不同,这时使用窗口类搜索就比较方便。如果找到了满足条件的窗口,这个函数返回该窗口的句柄,否则返回0。 看例子
1 2 3 4 5 6 7 8 9 | IntPtr ParenthWnd = new IntPtr(0); ParenthWnd = FindWindow( null , "Word Mobile" ); //判断这个窗体是否有效 if (ParenthWnd != IntPtr.Zero) { MessageBox.Show( "找到窗口" ); } else MessageBox.Show( "没有找到窗口" ); |
从上面的讨论中可以看出,如果要搜索的外部程序的窗口标题比较容易得到,问题是比较简单的。可如果窗口的标题不固定或者根本就没有标题,怎么得到窗口的类呢?如果你安装了Visual C++,你可以使用其中的Spy,在Spy++中有一个FindWindow工具,它允许你使用鼠标选择窗口,然后Spy++会显示这个窗口的类。
在Win32 API中还有一个FindWindowEx,它非常适合寻找子窗口。
// define
[DllImport("User32.dll")]
public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);
[DllImport("User32.dll")]
static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
const int WM_LBUTTONDOWN = 0x201;
const int WM_LBUTTONUP = 0x0202;
// sample, u should use spy++ to find windows class name and control class name
IntPtr hwndWin = FindWindow("TfrmMain", "window title");
if (hwndWin.Equals(IntPtr.Zero) == false)
{
IntPtr hwndBtn = FindWindowEx(hwndWin, IntPtr.Zero, "TButton", "control text");
if (hwndBtn.Equals(IntPtr.Zero) == false)
{
SendMessage(hwndBtn, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
SendMessage(hwndBtn, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)