p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   Simulating button click using Win32 API in .NET (http://p2p.wrox.com/showthread.php?t=37119)

debsoft January 12th, 2006 07:59 AM

Simulating button click using Win32 API in .NET
 
Hi All,

I'm developing a .NET winform which contains a button. On click of that button, certain actions are performed. I want to automate the button click programmatically using win32 API in .NET.
Here goes my code...

Code:

private void button1_Click(object sender, System.EventArgs e)
        {
            System.IntPtr hwnd = Win32API.FindWindow("","Form1");
            //get first child handle...
            System.IntPtr ptrChild = Win32API.GetWindow(hwnd, Win32API.GW_CHILD) ;
            while(!ptrChild.Equals(IntPtr.Zero))
            {
                if(ptrChild == Win32API.FindWindow("","button2"))
                {
                    //send the left mouse button "down" message to the button...
                    SendMessage(ptrChild, WM_LBUTTONDOWN, 0, IntPtr.Zero);
                    //send the left mouse button "up" message to the button...
                    SendMessage(ptrChild, WM_LBUTTONUP, 0, IntPtr.Zero);
                    //send the button state message to the button, telling it to handle its events...
                    SendMessage(ptrChild, Win32API.BM_SETSTATE, 1, IntPtr.Zero);
                }
                else
                {
                    ptrChild = Win32API.GetWindow(ptrChild, Win32API.GW_HWNDNEXT) ;
                }
            }
        }



The main form name is "Form1" which has two buttons -- button1 and button2. On clicking button1, I've written code which calls Win32 APIs to simulate the click of button2. But the code is not working at all...I'm not able to get the handle of the main form "Form1".

What can be wrong?? Can somebody help me out with this???
As per my understanding, it's should be simple Win32 API calls but something might be wrong in the code...

Thanks in advance for any help.

Debsoft

Ankur_Verma January 12th, 2006 11:41 AM

Ur actual problem seems to be that FindWindow is not working, is it?
Because as far as simulating the button click is concerned it can he done with
these couple of lines (having declared the DLLImports and Constants off course)

SendMessage(button2.Handle, WM_LBUTTONDOWN, IntPtr.Zero, IntPtr.Zero);
    SendMessage(button2.Handle, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);

However, you've chosen taking the FindWindow route to get to the button handle.
If that’s necessary for your program, let us know, so that people here could help u
with FindWindow as well.


Regards
Ankur Verma

debsoft January 12th, 2006 12:12 PM

Very true, Ankur...Thanks for the help.

How can one get the handle to the main window of an app (Web app or Winforms)??

--debsoft

Ankur_Verma January 12th, 2006 05:56 PM

Just use the Handle property of the main form object, like i used the Handle
property of the butoon2 object.

Regards
Ankur Verma

debsoft January 13th, 2006 11:10 AM

In continuation to our discussion, suppose a main form is there and I'm able to get the handles for all of its constituent controls.

Now one of the control in the Main form is a datagrid... and it displays records to user (in tabular form) and one of its column is a unbound column, say a checkbox or a radio button which allows user to select record(s) as the case may be.

Now my requirement is that I need a handle to this checkbox control of the datagrid. Is there a way to do that in Win32 Programming?

Can somebody help??

TIA
Debsoft

Ankur_Verma January 15th, 2006 02:25 PM

I havent tried it but cant you use DataGrid's Items property to get to that item and use it's Handle
property direcly instead?

Regards
Ankur Verma

debsoft January 17th, 2006 09:57 AM

Thanks Ankur for your reply. I'm using only Win32 API and not any .NET code to access controls on a form or for accessing child controls of a control on a form.

To simply put it, I need Win32 APIs for getting the handle to the checkbox in the datagrid...I won't be writing any code. The Windows app that I'm accessing may not be at all a .NET app...so I can't access the controls on the form programmatically...have to use win32 APIs....

Hope this answers your query.

--Debsoft


Quote:

quote:Originally posted by Ankur_Verma
 I havent tried it but cant you use DataGrid's Items property to get to that item and use it's Handle
property direcly instead?

Regards
Ankur Verma


Ankur_Verma January 17th, 2006 11:30 AM

I could tell Debsoft. I was just trying to get you to say it yourself.:D
This is most likely what you are supposed to be doing when you are
talking about API’s and .NET apps in a same query.:D

Cool stuff man. Anyways I say that this makes the task even simpler.
Ya, because it WILL work if you just replace the FindWindow mapping
Code with this:

Code:

    [DllImport("user32.dll", EntryPoint="FindWindowExW",  SetLastError=true,
         CharSet=CharSet.Unicode)]
    public static extern IntPtr FindWindowEx( IntPtr Ph,IntPtr Ch,
        IntPtr Ph1,
        String lpWindowName);

And modify your code a bit more this way

Code:

        System.IntPtr hwnd = FindWindowEx(IntPtr.Zero,IntPtr.Zero,IntPtr.Zero,"Form1");
        System.IntPtr ptrChild = GetWindow(this.Handle, GW_CHILD) ;
        while(!ptrChild.Equals(IntPtr.Zero))
        {
            if(ptrChild == FindWindowEx(hwnd,IntPtr.Zero,IntPtr.Zero,"button2"))
            {
                SendMessage(ptrChild, WM_LBUTTONDOWN, 0, IntPtr.Zero);
                SendMessage(ptrChild, WM_LBUTTONUP, 0, IntPtr.Zero);
                SendMessage(ptrChild, BM_SETSTATE,1 , IntPtr.Zero);
                break;
            }
            else
            {
                ptrChild = GetWindow(ptrChild, GW_HWNDNEXT) ;
            }
        }

I'm giving you the FindWindowEx code 'cause I had it with me, and didnt have to
write it. Also somthing I remember about FindWindow not able to find child window
handle... so not sure about FindWindow but this peice of code is working.
I think FindWindow should work as well. Do write back if there's anything else
you wanna discuss.

Regards
Ankur Verma

mkandibanda December 3rd, 2007 12:23 AM

I have the same situation as debsoft.

"To simply put it, I need Win32 APIs for getting the handle to the checkbox in the datagrid...I won't be writing any code. The Windows app that I'm accessing may not be at all a .NET app...so I can't access the controls on the form programmatically...have to use win32 APIs...."

If anyone has any suggestions on how to solve this, please let me know.

samjudson December 3rd, 2007 04:52 AM

If you're not writing it in .Net then I wouldn't ask the question on a C# forum, I'd go find a C++ or Windows API forum and ask there. Or search on Google, as I'm sure this question will have been asked many times before.

/- Sam Judson : Wrox Technical Editor -/


All times are GMT -4. The time now is 06:33 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.