Sign in to follow this  
Followers 0
AkSnowman

ControlClick

2 posts in this topic

Hello everyone, I recently started to convert a project I start in AutoIt to C#, using the AutoItX3.Assembly.dll. All was going fine and dandy until I tried to send a control click to a specific Control ID. Basically, when I use the following line everything works. 

AutoItX.ControlClick(hWnd, AutoItX.ControlGetHandle(hWnd, "#327701"), "left", 1, X, Y);

I run into the issue when I want to perform a control click on a control in which I only know the ID, which is an Integer. 

AutoItX.ControlClick(hWnd, 1126 /*<--Need the handle*/, "left", 1, X, Y);

The parameters for the ControlGetHandle are the IntPtr for the window handle and a string for the control. I tried using  the GetDlgItem, as microsofts website suggested it can be used to find the handle of any control given its parent and ID but it always returns 0. 

[DllImport("user32.dll")]
static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);

Its a fairly simple task in Autoit, and it works perfectly with this line:

ControlClick($hWnd, "", 1126, "left",1, X, Y);

Does anyone know of a way that I can retrieve the control handle from the control ID in c#?

 

Share this post


Link to post
Share on other sites



I don't know if you can edit previous posts but I couldn't find the button..Anyway,

My problem was that I didn't have the handle to the control that I wanted to perform the click on. I used spy++ to retrieve the handle of the control that I wanted, just to test the GetDlgItem, and it worked! This made me realize that when it was returning 0 it was because there was no control ID associated with the controls I was passing to it. Basically what I did was this, searched through all children of a main window and checked the ID, if any, to the ID I was looking for, then I checked all childrens children and so on and so forth. If the control ID wasn't found then 0 would be returned, otherwise the handle was returned. This was all based off of this article: FindWindowEx. The code I am now using to find the Control handle of a specific control id is this:

[DllImport("user32.dll")]
        static extern int GetDlgCtrlID(IntPtr hwndCtl);
        [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        
        static List<IntPtr> GetAllChildrenWindowHandles(IntPtr hParent, int maxCount)
        {
            List<IntPtr> result = new List<IntPtr>();
            int ct = 0;
            IntPtr prevChild = IntPtr.Zero;
            IntPtr currChild = IntPtr.Zero;
            while (true && ct < maxCount)
            {
                currChild = FindWindowEx(hParent, prevChild, null, null);
                if (currChild == IntPtr.Zero) break;
                result.Add(currChild);
                prevChild = currChild;
                ++ct;
            }
            return result;
        }
        
        static IntPtr FindHandle(IntPtr hParent, int ControlID)
        {

            //Console.WriteLine("Handle: {0:X}", hParent);
            IntPtr ret = IntPtr.Zero;
            foreach (IntPtr _Hand in GetAllChildrenWindowHandles(hParent, int.MaxValue))
            {
                //Console.WriteLine("->Checking {0:x}, ID: {1:x}", (int)_Hand, GetDlgCtrlID(_Hand));
                if (GetDlgCtrlID(_Hand) == ControlID)
                {
                    Console.WriteLine("Found the handle: {0:x}", (int)_Hand);
                    return _Hand;
                }
            }
            foreach (IntPtr _hand in GetAllChildrenWindowHandles(hParent, int.MaxValue))
            {
                IntPtr val =  FindHandle(_hand, ControlID);
                if (val != IntPtr.Zero)
                    return val;
            }
            return ret;
        }

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0