function function: This function obtains a handle to a top-level form that matches the given string with its class name and form name. This function does not look for subforms. Do not distinguish between uppercase and lowercase when searching.
Function type: HWND FindWindow (LPCTSTR ipclassname,lpctstr ipwindowname);
Number of references:
Ipclassname: A pointer to an empty end string that specifies the class name, or a member that identifies the class name string. Assuming that the parameter is a member, it must be the global member produced by the previous call to the Theglobafaddatom function. The member is 16 bits and must be in the low 16 bits of the ipclassname and the high must be 0.
Ipwindowname: Points to an empty end string that specifies the form name (form title). If the number of participants is NULL, all forms are fully matched.
Return value: Assuming the function succeeds, the return value is a form handle with the specified class name and form name, assuming the function fails with a return value of NULL.
Use this function in C # to first import namespaces:
using System.Runtime.InteropServices;
Then write the API reference part of the code into the class inside
[DllImport ("user32.dll", EntryPoint = "FindWindow")]private extern static IntPtr FindWindow (String lpclassname, String lpwindowname);
This function has two parameters, the first is the class of the form to find, and the second is the title of the form to find. You don't necessarily know both when searching, but at least you need to know one of them. Some forms have titles that are easier to get, such as "calculators", so search should be done using headings. But the title of some software is not fixed, such as "Notepad", it is assumed that the opening of different files, the form title is also different, when using the form class search is more convenient. Suppose a form is found that satisfies the condition, and the function returns a handle to the form, otherwise 0 is returned. Like what:
IntPtr hwnd = FindWindow (NULL, "Calculator"), if (hwnd! = IntPtr.Zero) {MessageBox.Show ("Find Calculator form");} else{MessageBox.Show ("No calculator form found");} hwnd = FindWindow ("Notepad", null), if (hwnd! = IntPtr.Zero) {MessageBox.Show ("Find Notepad form");} else{MessageBox.Show ("No Notepad form found");}
As can be seen from the discussion above, the problem is simpler if the form title of the external program you are searching for is easier to get. If the title of the form is not fixed or there is no title at all, how can I get the class of the form? At this point we can use Microsoft Spy + +, execute Spy + +, press ALT+F3, drag the target icon to the program in the popup interface.
FindWindow How to use