2012-01-13 73 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