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

还是从一个程序控制另一个程序的问题
代码如下:
[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),   null,   "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);  
                }


问题是:
当被控制的程序中控件在tabcontrol上的时候,不能找到,因为FindWindowEx使用的是按text而不是name搜索,tabcontrol没有text....
tabcontrol--> tabpage--> groupbox--> button
第一步都走不通,后面的自然没办法了。
第二个问题:控制端如果把button放到一个tabcontrol上也不能正常得到结果了。

------解决方案--------------------
像这样的操作一般来说是有限的,这就像两个家庭一样,怎么能随便的知道对方家里都有什么呢.除非是做为小偷去人家家里.
------解决方案--------------------
如果你愿意,倒也是能做到的,你可以用如下的两个API最后找到要处理的句柄:
[DllImport( "user32.dll ", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);

[DllImport( "user32.dll ", ExactSpelling = true)]
private static extern bool EnumChildWindows(HandleRef hwndParent, EnumChildrenCallback lpEnumFunc, HandleRef lParam);