为了用API private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);和private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, string lpszClass, string lpszWindow);这两个函数。。 开了二三十个页面查资料了,看的我快吐血了,lpszClass说是类名,MD也不说清楚,一开始我还以为是自己写的页面的类名,后来发现不是。貌似是控件的系统类名,用了也不对,那是自己设定的控件名?好像也不对。lpszWindow应该是控件的Text.已经抓狂了!用FindWindow成功找到了主窗体的句柄,但是用FindWindowEx死活找不到窗体上托盘图标的句柄。MMD叼函数参数说明能不能清楚点啊?后面两个查询条件lpszClass和lpszWindow到底是什么啊,控件系统类名Botton?还是自己命的名BottonName?还是控件上的文本BottonText????以上是牢骚,可以掠过。 [DllImport("User32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam); IntPtr defaultWnd = IntPtr.Zero;
IntPtr notifyIconWnd = IntPtr.Zero;
defaultWnd = FindWindow(null, "主窗体");//这个成功找到了
//我要找主窗体上的NotifyIcon,NotifyIcon属性如下:
//Name:notifyIcon1 Text:notifyIconText
notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, "NotifyIcon", null);//找不到
notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, "NotifyIcon", "notifyIcon1");//找不到
notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, "NotifyIcon", "notifyIconText");//找不到
notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, null,"notifyIconText");//找不到
notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, null,"notifyIcon1");//找不到
//死活就是找不到。救命啊!!!!!!!!!!!!!
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam); IntPtr defaultWnd = IntPtr.Zero;
IntPtr notifyIconWnd = IntPtr.Zero;
defaultWnd = FindWindow(null, "主窗体");//这个成功找到了
//我要找主窗体上的NotifyIcon,NotifyIcon属性如下:
//Name:notifyIcon1 Text:notifyIconText
notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, "NotifyIcon", null);//找不到
notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, "NotifyIcon", "notifyIcon1");//找不到
notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, "NotifyIcon", "notifyIconText");//找不到
notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, null,"notifyIconText");//找不到
notifyIconWnd = FindWindowEx(defaultWnd, IntPtr.Zero, null,"notifyIcon1");//找不到
//死活就是找不到。救命啊!!!!!!!!!!!!!
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "TrayNotifyWnd", null);
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "SysPager", null);
vHandle = FindWindowEx(vHandle, IntPtr.Zero, "ToolbarWindow32", null);
参考:
http://topic.csdn.net/u/20091018/23/91a416ee-4452-4e6d-ba40-54c9a0f805c0.html?71809
参考帖子里说“Shell_TrayWnd”就是操作任务栏是这一特殊的窗口的类,包括是托盘。
那我这么写IntPtr vHandle = FindWindow("Shell_TrayWnd", null);
IntPtr notifyIconWnd = FindWindowEx(vHandle, IntPtr.Zero, null, "MyNotifyIconName");
//这么找也找不到啊
这样不行,不是标准的windows窗口。用2楼的方法。用spy++查一下就知道了。
2L的方法我试了啊,在别人的Blog里也看到了,那四行完了最后获取的是个什么句柄啊?而且我这个是标准的windows窗口啊,要找的NotifyIcon也是窗体里的控件啊,和找Button有什么区别吗?
想想不对,我要找托盘图标的句柄干什么?就算找到了它的click事件也是写在Form里的。我的目的是要调用已经打开的程序窗口的方法或者属性。
现在已经成功获得了defaultWnd = FindWindow(null, "主窗体");怎么用这个defaultWnd来实例化Form?或者怎样能直接用defaultWnd去触发Form里的事件?