gyhanonline

二呆——我在Microsoft当Vendor :)

Simulate click event using widows API.

This article illuminates how to simulate click event using widows API. It is easily to add a click event in our WinForm/webForm application. But, if I want an external program to involve their click event, it’s a little bit tricky. It’s known that to protect an application we can’t access the application directly. If we have to control external app we could use Window API send message to tell the app what we want. I’ll use IE download dialog as a sample to explain How to click “OK” button automatically.

First of all, we need create a winForm project and drag a button onto the form. Then add click event.

Then we can add namespace “ System.Runtime.InteropServices; to import windows API.

using System.Runtime.InteropServices;

Following is the Windows API what we needed.

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

 

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

 

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern int PostMessage(IntPtr hwnd, int msg, uint wparam, uint lparam);

 

[DllImport("user32.dll", CharSet = CharSet.Auto)]

private extern static IntPtr SetActiveWindow(IntPtr hWnd);

 

[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

public static extern bool SetForegroundWindow(IntPtr hwnd);

 

FindWindowEx and FindWindow are used to get the handle of a dialog. FindWindow use to get the top level window. If you wanna find the child element, FindWindowEx supply a good practice.

 Before send click message, it’s necessary that active the button’s owner dialog. This is very important. If the “OK” button work at background, whatever we send the click message it would not respond. On the other hand, we have to make sure the Download Dialog has the focus before it is clicked.

So Let’s use function SetForegroundWindow to top the Download Dialog then active it by function SetActiveWindow.

From now all the prepare work has finished. And we can send the message to “OK” button thought by method PostMessage.

I’d like you using BM_Click as the click message. You can find all windows messages form MSDN site. Furthermore, spy++ is the good tool to find the value of a specified message.

Declare a message like this:

const int BM_CLICK = 0x00F5;

Completed! Using above approach, our program can click external application like a hackerJ

Sounds good, isn’t it! Following is the whole code segment:

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

 

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

 

[DllImport("user32.dll", CharSet = CharSet.Auto)]

public static extern int PostMessage(IntPtr hwnd, int msg, uint wparam, uint lparam);

 

[DllImport("user32.dll", CharSet = CharSet.Auto)]

private extern static IntPtr SetActiveWindow(IntPtr hWnd);

 

const int WM_CLICK = 0x00F5;

private void button2_Click(object sender, EventArgs e)

{

        IntPtr dialog= FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "");

        IntPtr child = FindWindowEx(dialog, IntPtr.Zero, "Button", "OK");

        SetForegroundWindow(dialog);

        SetActiveWindow(dialog);

        PostMessage(child, WM_CLICK, 0, 0);

}

 

Open a browser,Try!

 

 

posted on 2010-05-01 15:26  gyhanonline  阅读(570)  评论(0编辑  收藏  举报

导航