The following features are implemented:
- Find form
- Find Control ( also called subform )
- Get content
- Get location
- Set up
- Mouse click
Demonstration 1. Find form
Take the calculator that comes with the operating system as an example
string clWindow = "CalcFrame";
The class name of the entire window
string tlWindow = "计算器";
Window title
IntPtr ParenthWnd = FindWindow(clWindow, tlWindow);
This gets the window handle parenthwnd , if the ParenthWnd==IntPtr.Zero
description does not find the form;
[DllImport("User32.dll", EntryPoint = "FindWindow")]public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
Parameter lpclassname,lpwindowname can only fill one, but the other needs to be filled in: null instead of:"" (that is, string.) Empty).
If you open several of the same windows at the same time, such as starting up 2 calculators, which one is the return handle? The answer is: the top-level one.
1, if the Calculator 2 interface, in front of the Calculator 1 interface, then will return the handle of Calculator 2. 2, if the two calculator interface is side-by, that is, there is no overlap (including partial overlap), First switch the calculator 1, and then switch the calculator
2, then switch to another program, will return the handle of the calculator 2, and vice versa return the handle of the calculator 1.
This is probably the concept of a noun in the Windows system interface, such as the top-level window, z-order, and the active window. But I do not particularly understand, let me give an example to illustrate the actual situation.
2. Find the control, get the content, get the location
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
This function is cumbersome to use,hwndparent refers to the direct ancestor of the target control handle, sometimes you know that the window handle is not enough, but also need to know the hierarchical relationship between layers to find the line. Here are some examples of how to use:
FindWindowEx (Parenthwnd, IntPtr.Zero, "Tpanel", "Print Preview");
What I want to say is: Parameter hwndchildafter can write IntPtr.Zero,lpszclass and Lpszwindow can be null.
I usually use the enumeration method: Enumerate the list of all the handles of this window, and then filter the list to get the desired handle. On the code:
[DllImport ("user32.dll", ExactSpelling = True)]public static extern bool Enumchildwindows (IntPtr hwndparent, Enumwindowsproc lpenumfunc, int lParam); [DllImport ("User32.dll")]public static extern int getwindowtextw (IntPtr hWnd, [MarshalAs (UNMANAGEDTYPE.LPWSTR)] StringBuilder lpstring, int nmaxcount); [DllImport ("User32.dll")]public static extern int Getclassnamew (IntPtr hWnd, [MarshalAs (UNMANAGEDTYPE.LPWSTR)] StringBuilder lpstring, int nmaxcount); [DllImport ("User32.dll")]public static extern bool GetWindowRect (IntPtr hWnd, ref RECT LpRect);p ublic delegate bool ENUMW Indowsproc (IntPtr hWnd, int lParam);p ublic struct windowinfo{public IntPtr hwnd;//handle public string szwindowname;//window Port name public string szclassname;//class name public System.Drawing.Rectangle rect;//location size information}list<windowinfo> Enumchildwind Owscallback (IntPtr handle) {//For saving handle list list<windowinfo> wndlist = new list<windowinfo> (); Win32api.enumchildwindows (handle, delegate (IntPtr hWnd, int lParam) { Windowinfo wnd = new Windowinfo (); StringBuilder sb = new StringBuilder (256); Get hwnd Wnd.hwnd = hwnd; Get window name GETWINDOWTEXTW (hWnd, SB, SB.) capacity); Wnd.szwindowname = sb. ToString (); Get window class Getclassnamew (HWnd, SB, SB.) capacity); Wnd.szclassname = sb. ToString (); Rect rect = new rect (); if (GetWindowRect (hWnd, ref rect) = = True) wnd. Rect = rect. Torectangle (); Wndlist.add (WND); return true; }, 0); Here you get the handle list "Wndlist" to filter the wndlist. return wndlist;}
C # calls the Win32 API to manipulate other Windows