C# uses FindWindow and FindWindowEx to find forms and controls, and how to find controls with underlined titles

C# needs to import win32 functions

using System.Runtime.InteropServices;

/// <summary>
        /// Get window handle function
        /// </summary>
        /// <param name="lpClassName">window class name</param>
        /// <param name="lpWindowName" >Window title name</param>
        /// <returns>Return handle</returns>
        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

Function introduction:

Function function: This function obtains the handle of a top-level form whose class name and form name match the given string. This function does not look for subforms. Do not distinguish between uppercase and lowercase when searching.

    Function type: HWND FindWindow (LPCTSTR IpClassName, LPCTSTR IpWindowName);

    Parameters:

    IpClassName: Point to a null-terminated string specifying the class name, or a pointer to a member identifying the class name string. Assuming that the parameter is a member, it must be the global member generated by calling theGlobafAddAtom function last time. This member is 16 bits, must be located in the lower 16 bits of IpClassName, and the higher bit must be 0.

    IpWindowName: Points to a null-terminated string specifying the window name (window title). If the parameter is empty, all forms will be matched.

    Return value: If the function succeeds, the return value is a form handle with the specified class name and form name; if the function fails, the return value is NULL

[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]
       

public static extern IntPtr FindWindowEx(IntPtr hwndParent, uint hwndChildAfter, string lpszClass, string lpszWindow);

Function introduction:

函数原型:HWND FindWindowEx(HWND hwndParent,HWND hwndChildAfter,LPCTSTR lpszClass,LPCTSTR lpszWindow);

 Parameters:

    hwndParent: The handle of the parent window to find the child window.

    If hwnjParent is NULL, the function uses the desktop window as the parent window to find all child windows of the desktop window.

    Windows NT5.0 and later: If hwndParent is HWND_MESSAGE, the function only finds all message windows.

    hwndChildAfter : child window handle. The search starts with the next child window in the Z order. The child window must be a direct child window of the hwndPareRt window rather than a descendant window. If HwndChildAfter is NULL, the search starts from the first child window of hwndParent. If both hwndParent and hwndChildAfter are NULL, the function searches all top-level windows and message windows.

    lpszClass: Pointer to a null-terminated string specifying the class name, or a pointer to a member identifying the class name string. If the parameter is a member, it must be the global member generated by the previous call to theGlobaIAddAtom function. This member is 16 bits, must be located in the low 16 bits of lpClassName, and the high bit must be 0.

    lpszWindow: Pointer to a null-terminated string specifying the window name (window title). If this parameter is NULL, all windows will be fully matched. Return Value: If the function succeeds, the return value is a window handle with the specified class name and window name. If the function fails, the return value is NULL.

usage:

 const int BM_CLICK = 0xF5; 
 IntPtr maindHwnd = FindWindow(null, "QQ user login"); //Get the handle of the QQ login box 
 if (maindHwnd != IntPtr.Zero) 
 { 
     IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null , "Login"); //Get the handle of the button 
     if (childHwnd != IntPtr.Zero) 
     { 
         SendMessage(childHwnd, BM_CLICK, 0, 0); //Send the message of clicking the button 
     } 
     else 
     { 
         MessageBox.Show("Not found child window"); 
     } 
 } 
 else 
 { 
     MessageBox.Show("No window found"); 
 }

However, the title of the control may be underlined during use, so we need to add the "&" symbol when searching, as follows:

IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null, "&N"); //Get the handle of the button

Guess you like

Origin blog.csdn.net/yunxiaobaobei/article/details/90436906#comments_25621233