我查询了网上很多资料。最后代码如下。
[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.
是我语法有误么?
请指教。

解决方案 »

  1.   

    试试这个:
      IntPtr hwnd_button = FindWindowEx(ActiveWindowHandle, null, "button4");
      

  2.   

    改一下:
    FindWindowEx(ActiveWindowHandle, new IntPtr(0), null, "button4");
      

  3.   

    好的。谢谢你。果然正确了。马上结帖。能告诉我一下原因么?为什么类名用null呢?是网上的资料有误?
      

  4.   

    因为你指定的类名:WindowsForms10.BUTTON.app3
    不是正确的类的名字,当为NULL的时候系统会忽略这个参数,所在让其为null就行了.
      

  5.   

    谢谢了。刚解决完又遇到开始看的一篇文章里的问题了。
    把这个button移到一个tabcontrol上,马上所有命令都没用了。
      

  6.   

    晕,居然不是用name而是text查找