2012-01-13 70 views
0

我从 http://improve.dk/archive/2007/04/07/finding-specific-windows.aspx找到一个可见的按钮

“偷”的代码,但不是写的类名,标题和处理到我要检查,如果某个按钮是可见的控制台。如果按钮是可见的我想最大化窗口。

我改变了这部分=>

private static bool foundWindow(int handle)  
    { 
     bool buttonCheck = false; 
     IntPtr hButton = FindWindowEx((IntPtr)handle, IntPtr.Zero, "AfxWnd90u21", null); 
     if (hButton != IntPtr.Zero) 
     { 
      buttonCheck = true; 
     } 

     if (buttonCheck) 
     { 
      ShowWindowAsync(handle, (int)3); // maximize the window 
     } 

     return true; 
    } 
the button class is `AfxWnd90u` and the instance is `21`. I wrote this in autoit before and AfxWnd90u21 is 100 % correct. 

the problem is that i cant find the button with AfxWnd90u21. if i only use 

    IntPtr hButton = FindWindowEx((IntPtr)handle, IntPtr.Zero, "AfxWnd90u", null); 

all windows get maximized. 

It has to be something with the instance. 

i hope you can help me, 

thanks 

最新编辑 我只是想用 “GetClassName” 找到的类名。我发现每个句柄有190个课,但是我需要的课不在那里。 IAM真的绝望 我希望有人能帮助我, 感谢

 private static bool foundWindow(int handle) 
      { 
       int i = 0; 
       IntPtr hWnd = (IntPtr)handle; 

       // System.Windows.Forms.Control control = System.Windows.Forms.Control.FromHandle(hWnd); 
       StringBuilder sbClass = new StringBuilder(256); 

       while (hWnd != IntPtr.Zero) 
       { 
        ++i; 


        /////////////////////////////////////////////////// 
        ////////////// Compare if the classname exists///// 
        GetClassName((int)hWnd, sbClass, sbClass.Capacity); 
        if (sbClass.ToString().Equals("AfxWnd90u21")) 
        { 
         MessageBox.Show(sbClass.ToString()); 
        } 
        /////////////////////////////////////////////////// 


        ////// trying to find the correct class with findwindowEX////////// 
        IntPtr hButton = FindWindowEx(hWnd, IntPtr.Zero, "AfxWnd90u21", null); 



        if (hButton != IntPtr.Zero) 
        { 
         MessageBox.Show("true"); 
         ShowWindowAsync(handle, (int)2); // maximize the window 
        } 
        hWnd = FindWindowEx(IntPtr.Zero, hWnd, null, null); 
       } 
       MessageBox.Show(""+i); 
       return true; 
      } 

回答

1

http://msdn.microsoft.com/en-us/library/windows/desktop/ms633500%28v=vs.85%29.aspx

lpszWindow [in, optional] 

    Type: LPCTSTR 

The window name (the window's title). If this parameter is NULL, 
all window names match. 

貌似这个API,为了匹配的情况下,你需要给你的情况下,唯一的窗口名。或者,你可以通过手动搜索所有的孩子到一个控件,然后自己检查实例。

但是,如果你走得那么远,将父对象转换为Control对象,并遍历它的.Controls成员更容易。您可以使用反射来检查控件的类型等。

至手柄转换为控制: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.fromhandle.aspx

迭代使用你喜欢的任何循环风格Control.Controls。

+0

谢谢, 我试图迭代儿童,但它没有奏效。你能给我一个例子吗?给控件添加一个句柄,并通过它的成员迭代它? 我刚刚发现这@MSDN:http://msdn.microsoft.com/en-us/library/ms908149.aspx sry,但我完全混淆 – 2012-01-13 15:28:45

+0

@MaikKlein更新 – 2012-01-13 15:52:26

+0

好吧, 但我仍然有点困惑。我得到了类型转换System.Windows.Forms.Control control = System.Windows.Forms.Control.FromHandle(hWnd); 我检查了控件的所有成员函数,唯一可以帮助的函数是control.contains,但它需要一个控件作为参数 我如何用控件对象来检查类? 或者我可以检查您之前发布的“lpszWindow”类吗? – 2012-01-13 21:59:25