找到一个可见的按钮

时间:2012-01-13 14:48:59

标签: c# button window

我“窃取”了代码 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个类,但是我需要的类不在那里。         我非常绝望         我希望有一个人可以帮助我,         感谢

       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 个答案:

答案 0 :(得分: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。