Windows Develop Bookmark and Share   
 index > Windows Forms General > How can I catch the Mouse Click?
 

How can I catch the Mouse Click?

Hello everyone,

I have created a BHO using C#. The code works fine as I get hook to many things. I also would like to be able todetect mouse click on a particular button.Here is a section of my code:

public class BHO : IObjectWithSite

{

private SHDocVw.WebBrowser webBrowser;

private mshtml.HTMLDocument document;

private mshtml.IHTMLDocument2 document2;

private mshtml.HTMLElementEvents_Event documentEvents;

private mshtml.HTMLElementEvents_Event elementEvents;

.

.

.

public bool OnElementMouseClick()

{

MessageBox.Show("HelloWorld...OnElementMouseClick");

return true;

}

.

.

.

public int SetSite(object site)

{

if (site != null)

{

webBrowser = (SHDocVw.WebBrowser)site;

webBrowser.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(this.OnDocumentComplete);

webBrowser.BeforeNavigate2 += new SHDocVw.DWebBrowserEvents2_BeforeNavigate2EventHandler(this.OnBeforeNavigate2);

webBrowser.OnQuit += new SHDocVw.DWebBrowserEvents2_OnQuitEventHandler(this.OnQuit);

webBrowser.NavigateComplete2 += new SHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.NavigateComplete2);

elementEvents.onclick += new mshtml.HTMLElementEvents_onclickEventHandler(this.OnElementMouseClick);



for the OnElementMouseClick function I have the following.

public bool OnElementMouseClick()

{

MessageBox.Show("HelloWorld...OnElementMouseClick");

return true;

}


.
.
.
.


But nothing really happens! Am I wrong to think that the above code should give me HelloWorld message once a button is click?

Any information on this would be appriciated. Thank you very much.

Khoramdin

Khoramdin  Wednesday, November 21, 2007 8:36 PM

Hi, Khoramdin

Yes, it possible to handle the click eventof a html button with managed code.

Code Block

private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com");
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Find the "Search" button.
HtmlElement element = webBrowser1.Document.GetElementById("btnG");

//Attach to the hole page is also OK.
//HtmlElement element = webBrowser1.Document.Body;

//Add an event handler.
element.AttachEventHandler("onclick", delegate(object o, EventArgs arg)
{
MessageBox.Show("Hello!");
});
}

Here I was using the WebBrowser control however using SHDocVw should be the same. When you click the "Search" button in the web page a message box will pop up saying "Hello!".

Best Regards

Chunsheng Tang

Chunsheng Tang  Friday, November 23, 2007 10:56 AM

Hi, Khoramdin

Yes, it possible to handle the click eventof a html button with managed code.

Code Block

private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.google.com");
}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Find the "Search" button.
HtmlElement element = webBrowser1.Document.GetElementById("btnG");

//Attach to the hole page is also OK.
//HtmlElement element = webBrowser1.Document.Body;

//Add an event handler.
element.AttachEventHandler("onclick", delegate(object o, EventArgs arg)
{
MessageBox.Show("Hello!");
});
}

Here I was using the WebBrowser control however using SHDocVw should be the same. When you click the "Search" button in the web page a message box will pop up saying "Hello!".

Best Regards

Chunsheng Tang

Chunsheng Tang  Friday, November 23, 2007 10:56 AM

You can use google to search for other answers

Custom Search

More Threads

• help how to display
• Socket Freezes in WinForm within IE
• Compare 2 dlls using reflection
• passing data between 2 datagridviews
• Strong name assembly DataGridViewAutoFilter.dll
• ComboBox - Add a string to display and associated data
• Sort columns in datagridview
• GridView Image Problem...
• Listbox Problem after displaying dialog box
• How to actual page source for XML pages with ShDocVw.InternetExplorer