Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Simulate Mouse Click On WebBrowser Control

Sample C# code for executing any type of mouse click on a WebBrowser control by calling a set of unmanaged functions from the user32.dll library.

On Friday, September 5th 2008 at 05:55 AM
By Andrew Pociu (View Profile)
*****   (Rated 4.3 with 9 votes)
Contextual Ads
More C# Resources
Advertisement
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9.  
  10. namespace BrowserMouseClick
  11. {
  12.     public partial class Form1 : Form
  13.     {
  14.         [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
  15.         static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
  16.  
  17.         [DllImport("user32.dll", SetLastError = true)]
  18.         static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
  19.  
  20.         [DllImport("user32.dll", CharSet = CharSet.Auto)]
  21.         static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
  22.  
  23.         public Form1()
  24.         {
  25.             InitializeComponent();
  26.         }
  27.  
  28.         private void Form1_Load(object sender, EventArgs e)
  29.         {
  30.             webBrowser1.Navigate("http://www.devpub.com");
  31.         }
  32.  
  33.         private void btnMouseClick_Click(object sender, EventArgs e)
  34.         {
  35.             int x = 100; // X coordinate of the click
  36.             int y = 80; // Y coordinate of the click
  37.             IntPtr handle = webBrowser1.Handle;
  38.             StringBuilder className = new StringBuilder(100);
  39.             while (className.ToString() != "Internet Explorer_Server") // The class control for the browser
  40.             {
  41.                 handle = GetWindow(handle, 5); // Get a handle to the child window
  42.                 GetClassName(handle, className, className.Capacity);
  43.             }
  44.  
  45.             IntPtr lParam = (IntPtr)((y << 16) | x); // The coordinates
  46.             IntPtr wParam = IntPtr.Zero; // Additional parameters for the click (e.g. Ctrl)
  47.             const uint downCode = 0x201; // Left click down code
  48.             const uint upCode = 0x202; // Left click up code
  49.             SendMessage(handle, downCode, wParam, lParam); // Mouse button down
  50.             SendMessage(handle, upCode, wParam, lParam); // Mouse button up
  51.         }
  52.     }
  53. }
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by 中国朋友 China Friend on Friday, February 27th 2009 at 10:16 AM

非常感谢这位外国朋友,Thanks!you help me!

by kazim on Friday, June 26th 2009 at 12:24 PM

how you have added event as by defualt this event is not shown

by rhovel on Sunday, August 2nd 2009 at 10:09 AM

i got this error when im trying to run this program




Error 1 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?) C:\sandbox\mouseclick\mosuseclick\mosuseclick\Program.cs 18 33 mosuseclick

by rhovel on Sunday, August 2nd 2009 at 10:09 AM

i got this error when im trying to run this program




Error 1 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?) C:\sandbox\mouseclick\mosuseclick\mosuseclick\Program.cs 18 33 mosuseclick

by user on Wednesday, September 30th 2009 at 02:24 PM

i got this error when im trying to run this program




Error 1 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?) C:\sandbox\mouseclick\mosuseclick\mosuseclick\Program.cs 18 33 mosuseclick

I can see you have no clue about OO programing,

"public partial class Form1 : Form" Form1 is the name of the class, you can name it whatever you want

by == ANDREI == on Friday, March 5th 2010 at 03:33 AM

Something is wrong in this part:

while (className.ToString() != "Internet Explorer_Server") // The class control for the browser
{
handle = GetWindow(handle, 5); // Get a handle to the child window
GetClassName(handle, className, className.Capacity);
}

Reason of the problem: it stays in that loop forever :( I removed it but couldn't obtain a handle of the browser. I managed to get a handle of a button i added to the Form1. For that button the code with the mouse click works ! Good job !

Questions: How do i optain a handle to the Browser ? Why the second parameter of the fuction GetWindow is 5 and not 10 or 29 ? I used 3 to obtain a handle to the button but why is 3 and not 20 ?

Tnx !

by Pradeep1210 on Sunday, May 23rd 2010 at 03:32 PM

Easy way to add Keyboard and Mouse events to WebBrowser control:
http://pradeep1210.spaces.live.com/blog/cns!2E84C99518D46BB4!263.entry

by Mohsen on Tuesday, November 23rd 2010 at 03:40 AM

begir

by free dating sites on Sunday, November 20th 2011 at 07:00 AM

carefully produced that they are identical as a team on the subject of material plus style - but not only do genuine real sports jerseys glance good luckily they are up to taking up the weather condition; letting your sensitive skin breath plus keeping you actually comfortable. Find serial statistics and/or holograms which may usually be used as indicators the fact that sports jerseys will be valid.

by link building services on Monday, December 12th 2011 at 05:52 AM


Hi! Great article, it helped me alot!

You could also add an example how to repeat an exception instead of letting the program just end. For example: we need an integer input, but an string was entered... how to repeat the try - catch block?

by Andrei Pociu on Monday, November 21st 2005 at 09:37 AM

Put everything in a method, and in the catch block you can recall the method (you can say the method calls itself).

by GenF20 Plus on Monday, December 19th 2011 at 05:25 PM

This is a great trick and I didn’t know you can use C# to obtain the info of your computer

by Human Growth Hormone on Monday, December 19th 2011 at 05:28 PM

This is a great trick and I didn’t know you can use C# to obtain the info of your computer

by Phen375 on Monday, December 19th 2011 at 05:34 PM

Does this trick work for Mac too?

by GenFX on Monday, December 19th 2011 at 05:36 PM

Does this trick work for Mac too?

by Teeth Whitening Reviews on Monday, December 19th 2011 at 05:42 PM

I needed to create you this tiny word so as to give thanks once again for your pleasant techniques you have provided on this site. This has been shockingly open-handed with people like you to supply openly what exactly a number of people would've offered for an e book to help make some profit for themselves, precisely now that you could possibly have done it in case you desired.

by Nuratrim on Monday, December 19th 2011 at 05:43 PM

I needed to create you this tiny word so as to give thanks once again for your pleasant techniques you have provided on this site. This has been shockingly open-handed with people like you to supply openly what exactly a number of people would've offered for an e book to help make some profit for themselves, precisely now that you could possibly have done it in case you desired.

by Ultimate Maqui Berry on Monday, December 19th 2011 at 05:47 PM

Those concepts additionally worked as a good way to recognize that other people have the same desire really like my own to see significantly more in regard to this issue. Certainly there are numerous more pleasurable opportunities up front for people who looked over your blog.

by Best Diet Pills on Monday, December 19th 2011 at 05:48 PM

Those concepts additionally worked as a good way to recognize that other people have the same desire really like my own to see significantly more in regard to this issue. Certainly there are numerous more pleasurable opportunities up front for people who looked over your blog.

by Dental White on Monday, December 19th 2011 at 05:51 PM

I have to express my thanks to the writer just for rescuing me from such a difficulty. Because of browsing through the online world and seeing views which were not helpful, I believed my life was done. Existing without the presence of solutions to the problems you've fixed as a result of your main write-up is a crucial case, and the kind which could have in a wrong way affected my entire career if I hadn't discovered your website.

by Pure Acai Berry Max on Monday, December 19th 2011 at 05:53 PM

I have to express my thanks to the writer just for rescuing me from such a difficulty. Because of browsing through the online world and seeing views which were not helpful, I believed my life was done. Existing without the presence of solutions to the problems you've fixed as a result of your main write-up is a crucial case, and the kind which could have in a wrong way affected my entire career if I hadn't discovered your website.

by on Tuesday, January 17th 2012 at 12:52 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by pret auto on Tuesday, May 8th 2012 at 05:30 AM

Error 1 The type or namespace name 'Form1' could not be found (are you missing a using directive or an assembly reference?) C:\sandbox\mouseclick\mosuseclick\mosuseclick\Program.cs 18 33 mosuseclick

by Alston18ISABELLA on Wednesday, May 16th 2012 at 11:24 AM

According to my own exploration, billions of people in the world get the credit loans at good creditors. Thus, there is a good possibility to get a car loan in every country.

by on Monday, June 11th 2012 at 06:27 PM

asdads


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources