开发者

how to mouse click on an image on a running IE instance programmatically from windows form app

I want to create a windows app that does the following. When a button is clicked,

  1. Find a running instance of IE (which I was able to get a handle using FindWindow api (user32.dll))
  2. Send message to windows OS to mouse click on the image in IE. I already know that there is an image on the page. -- this is where I need help!! thanks.

How do I get a image object in html from windows app when I have a handle? I've tried user32.dll (mouse_e开发者_运维百科vent(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo)) but I can't find correct x and y for the image in the page.


please, check the code below; it should be iterating through all opened IE windows using SHDocVw.ShellWindows collection; for each window it iterates through collection of images of the mshtml.HTMLDocument object associated with the window. For each image it dumps its href into the console and tries to click on it using the mshtml.HTMLImg click method. You can also check HTMLImg defenition for the object's position in case you would want to go with sending mouse click through mouse_event procedure. I also added OnClick event handler to image objects; it should be executed every time you click on the image either in code or using mouse.

private void button2_Click(object sender, EventArgs e)
{
    SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
    foreach (SHDocVw.WebBrowser ie in shellWindows)
    {
        mshtml.HTMLDocument doc = ie.Document as mshtml.HTMLDocument;
        if (doc != null)
        {
            foreach (mshtml.HTMLImg imgElement in doc.images)
            {
                ((HTMLImgEvents_Event)imgElement).onclick += new mshtml. HTMLImgEvents_onclickEventHandler(Form1_onclick); 
                Console.WriteLine(imgElement.href);
                imgElement.click();
            }
        }
    }
}

private bool Form1_onclick()
{
    Console.WriteLine("click !!");            
    return true;
}

hope this helps, regards

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜