Get handles from the API control other forms

Source: Internet
Author: User

Many times, writing a program to simulate mouse and keyboard operations can easily implement the functions you need, without the need for the other program to open the interface for you. For example, the operation of fetion timed to send SMS and so on. I have developed fetion mice, using the Fetion protocol to grab packets, and then analyze the protocol, and then simulate the implementation of the Protocol, developed a client, and mobile server communication, but there are some shortcomings. If the mobile server changes the interface, the client I write will also upgrade accordingly. If the server's protocol is changed, even a personal writing of such third-party clients needs to be rewritten. And I personally do not have the time and energy, or not enough to support me to continue to reconstruct fetion rats. Therefore, this is a good software, now on the shelf, I also feel sorry. Last week, a project acceptance, need to modify the interface, but zero can not find the source. I have to solve this problem in two or three hours, the time is tight. It occurred to me that my roommate had previously done a small program that simulated the mouse keyboard to send fetion messages. So I hastened the telephone consultation. Then mastered this skill, solved the problem on time. I think this technique is still very useful and is summarized as follows:
First, the following three API interfaces are introduced:

[DllImport("user32.dll")]publicstaticexternIntPtr FindWindow(string lpClassName, stringlpWindowName);[DllImport("User32.dll", EntryPoint = "SendMessage")]privatestaticexternintSendMessage(IntPtr hWnd, int Msg, IntPtr wParam, stringlParam);[DllImport("User32.dll ")]publicstaticexternIntPtr FindWindowEx(IntPtr parent, IntPtr childe, stringstrclass, stringFrmText);

The first and third are used to find the window handle, and any window running on Windows has a handle. A text box on a window, such as a button, also has its handle (which can be considered a child window handle). The types of these handles can be

Spy + + to query. For example, the C language Program, the text box handle type is generally "EDIT", C # written program is not, can be specific to check. The second interface is used to send various messages to the window, such as sending a text box

String, or sends a pressed and bounced message to the button, and so on. Detailed explanations are as follows:

IntPtr hwnd = FindWindow(null, "无标题 - 记事本");

This is the window that is used to find the open window in the operating system titled Untitled-Notepad. The first parameter is the type of this window. These two parameters know a

One, and the other can be null. However, if you are looking for a window type, you may only get one of these windows. Therefore, it is very convenient to search by the title.

IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);

This function is used to obtain a handle to the window's child window, which refers to the various controls in the window. The first argument is the handle to the parent window, and the second parameter indicates that the number of child windows in the same type is obtained. Fill

IntPtr.Zero is the first child window that is obtained. The third parameter represents the type of child window you need to find, and the fourth parameter is generally null. If you have two text boxes in a window, you can get the second text box by doing the following

Handle.

IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, "EDIT", null);IntPtr htextbox2 = FindWindowEx(hwnd, htextbox, "EDIT", null);//填上次获得的句柄,可以得到下一个的句柄。

This is just to fill in the second parameter as IntPtr.Zero, get the first edit type of text box, and then the second call, then fill the second parameter as the first text box handle, then the execution returned is the handle of the next text box

The So what Htextbox2 get is the handle to the second text box.
After you have free access to the handles of various windows and their controls, we can send them a variety of messages for mouse and keyboard simulations. Like what:

SendMessage(htextbox, WM_SETTEXT, IntPtr.Zero, name);

This sentence is for the text box to fill in the corresponding string name.

IntPtr hbutton = FindWindowEx(hwnd, IntPtr.Zero, "BUTTON", null);SendMessage(hbutton, WM_LBUTTONDOWN, IntPtr.Zero, null);SendMessage(hbutton, WM_LBUTTONUP, IntPtr.Zero, null);

These three sentences are obtained by a button in the window, then send Press, pop up the message to it, simulate the mouse click action.
The first argument to the SendMessage function is the window handle, or the handle to the control in the window, and the second parameter is the type flag of the message, which is defined in some header files of the API. If you're in C #, define them yourself, like

Constint Wm_settext =0x000c;
Constint Wm_lbuttondown =0x0201;
Constint Wm_lbuttonup =0x0202;
Constint Wm_close =0x0010;


There are other types of flag, you can refer to the previous blog query, you can also check MSDN. The third parameter and the fourth parameter are the exact contents of the message. In general, we use the last parameter. The third parameter is filled with IntPtr.Zero.

Of course, if it is the mouse action, then the last parameter is null.

SendMessage(htextbox, WM_SETTEXT, IntPtr.Zero, name);//填写文本框。SendMessage(hbutton, WM_LBUTTONDOWN, IntPtr.Zero, null);//鼠标按下按钮。

Get handles from API control other forms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.