Download this article code (VS2010 development): http://download.csdn.net/source/2796362
Summary:
1: A simple example
1.1: Introduction to EnumChildWindows
1.2: main source code
2: Difficulty: how to obtain the specified control handle
2.1: use SPY ++
2.2: Get the widget location
2.3: Get the Control ID
1: A simple example
In the daily coding process, we often perform automated tests. The automated test here is not a unit test, but a simulation of manual input for fast and high concurrency testing. The available automatic chemical industry has LOADRUNNER, and currently has a powerful testing platform in VS2010 (recording operation steps, automatic generation of code ). However, mastering these tools also requires a certain amount of time, and most importantly, it is not flexible enough for a programmer. Therefore, a more efficient method is to call the windows api and manually write the code.
The following is a simple demonstration. For simplicity, assume that an application exists:
1: provides a WINFORM with a TextBox and a Button;
2: click the Button. A prompt box is displayed. The prompt box contains the TextBox value;
Now, the test requirements are as follows:
1: run the above program on 300 machines;
2: Go to the 300 machines and click the Button to see if function 2 has been implemented;
Obviously, there is not such a simple program in the actual situation. The actual situation may be that you click the Button to download a file in a unified manner, and the test requirements may become the server load assessment. Now, the test department obviously does not have 300 people sitting on the client to verify the test results. In this case, we need to provide an automated test tool to complete the necessary test tasks.
The testing tool is also a C # program. Its main purpose is:
1: Get the window handle of the above application, and then get the TextBox handle and Button handle;
2: Enter some characters randomly for TextBox;
3: click a Button;
1.1: Introduction to EnumChildWindows
Here we need to introduce EnumChildWindows,
EnumChildWindows is a good thing. You can enumerate all the child windows of a parent window:
BOOL EnumChildWindows (
HWNDHWndParent, // Handle to parent window // parent window handle
WNDENUMPROCLpEnumFunc, // Callback function address
LPARAMLParam// Application-defined value // The parameter you have defined
);
In this simple way, Let's define another callback function, as shown below:
Bool callback EnumChildProc (
HWNDHwnd, // Handle to child window
LPARAMLParam// Application-defined value
);
When you call the EnumChildWindows function, the function is always enumerated until the maximum subwindow is called or the callback function returns a false value. Otherwise, the function is always enumerated.
1.2: main source code of a simple example
The main code of the test tool is as follows:
Private void button#click (object sender, EventArgs e) {// obtain the form handle of the test program IntPtr mainWnd = FindWindow (null, "FormLogin "); list <IntPtr> listWnd = new List <IntPtr> (); // obtain the handle of the OK button on the form IntPtr hwnd_button = find1_wex (mainWnd, new IntPtr (0), null, "OK"); // obtain the handle EnumChildWindows (mainWnd, new CallBack (delegate (IntPtr hwnd, int lParam) {listWnd. add (hwnd); return true;}), 0); foreach (IntPtr item in ListWnd) {if (item! = Hwnd_button) {char [] UserChar = "luminji ". toCharArray (); foreach (char ch in UserChar) {SendChar (item, ch, 100) ;}} SendMessage (hwnd_button, WM_CLICK, mainWnd, "0 ");} public void SendChar (IntPtr hand, char ch, int SleepTime) {PostMessage (hand, WM_CHAR, ch, 0); System. threading. thread. sleep (SleepTime);} public static int WM_CHAR = 0x102; public static int WM_CLICK = 0x00F5; [DllImport ("User32.dll", EntryPoint = "SendMessage")] public static extern int SendMessage (IntPtr hWnd, int Msg, IntPtr wParam, string lParam); [DllImport ("user32.dll")] public static extern IntPtr find1_wex (IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport ("user32.dll", SetLastError = true)] public static extern IntPtr FindWindow (string lpClassName, string lpWindowName); [DllImport ("user32.dll") public static extern int AnyPopup (); [DllImport ("user32.dll", CharSet = CharSet. auto, SetLastError = true)] public static extern int GetWindowText (IntPtr hWnd,