日期:2014-05-20  浏览次数:20879 次

从一个程序操作另一个程序的按钮
我查询了网上很多资料。最后代码如下。
[DllImport( "user32.dll ")]
                public   static   extern   IntPtr   FindWindowEx(IntPtr   hwndParent,   IntPtr   hwndChildAfter,   string   lpszClass,   string   lpszWindow);

[DllImport( "user32.dll ",   CharSet   =   CharSet.Auto)]
                public   static   extern   IntPtr   PostMessage(IntPtr   hwnd,   int   wMsg,   IntPtr   wParam,   IntPtr   lParam);

                private   void   button1_Click(object   sender,   EventArgs   e)
                {
                        Process   p   =   Process.Start(textBoxNewFormLocation.Text);
                        IntPtr   ActiveWindowHandle   =   p.MainWindowHandle;
                        IntPtr   hwnd_button   =   FindWindowEx(ActiveWindowHandle,   new   IntPtr(0),   "WindowsForms10.BUTTON.app3 ",   "button4 ");                        
                        const   int   BM_CLICK   =   0x00F5;
                        Message   msg   =   Message.Create(hwnd_button,   BM_CLICK,   new   IntPtr(0),   new   IntPtr(0));
                        PostMessage(msg.HWnd,   msg.Msg,   msg.WParam,   msg.LParam);  
                }

问题是第2步获取按钮句柄总是失败,获取的值为0.
是我语法有误么?
请指教。


------解决方案--------------------
试试这个:
IntPtr hwnd_button = FindWindowEx(ActiveWindowHandle, null, "button4 ");
------解决方案--------------------
改一下:
FindWindowEx(ActiveWindowHandle, new IntPtr(0), null, "button4 ");