Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 12th, 2006, 07:59 AM
Authorized User
 
Join Date: Jun 2003
Location: Noida, UP, India.
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
__________________
cheers,
debsoft
Reply With Quote
  #2 (permalink)  
Old January 12th, 2006, 11:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

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
Reply With Quote
  #3 (permalink)  
Old January 12th, 2006, 12:12 PM
Authorized User
 
Join Date: Jun 2003
Location: Noida, UP, India.
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #4 (permalink)  
Old January 12th, 2006, 05:56 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

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

Regards
Ankur Verma
Reply With Quote
  #5 (permalink)  
Old January 13th, 2006, 11:10 AM
Authorized User
 
Join Date: Jun 2003
Location: Noida, UP, India.
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #6 (permalink)  
Old January 15th, 2006, 02:25 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

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
Reply With Quote
  #7 (permalink)  
Old January 17th, 2006, 09:57 AM
Authorized User
 
Join Date: Jun 2003
Location: Noida, UP, India.
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Reply With Quote
  #8 (permalink)  
Old January 17th, 2006, 11:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

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
Reply With Quote
  #9 (permalink)  
Old December 3rd, 2007, 12:23 AM
Registered User
 
Join Date: Dec 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
Reply With Quote
  #10 (permalink)  
Old December 3rd, 2007, 04:52 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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 -/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between win32 API and FCL adie BOOK: Beginning Visual Basic 2005 ISBN: 978-0-7645-7401-6 0 July 26th, 2007 05:56 PM
Simulating Double Click... savagesoft BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 1 May 14th, 2007 10:41 AM
simulating click event simmohall BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 March 13th, 2005 06:28 PM
Win32 API Programming visit_anand_it Visual C++ 2 September 7th, 2004 11:46 PM
how to use win32 API? csc820203 C# 1 June 23rd, 2004 02:12 AM



All times are GMT -4. The time now is 08:04 AM.


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