首页 > 代码库 > user32的使用

user32的使用

  1. 通过代码查询特定的窗口,并在文本框中输入文字然后单击"OK"按钮
    • 需要查找的Dialog

    • 使用Spy++查看窗口信息

    • 通过代码实现功能
       1     class Program 2     { 3         //define method 4  5         /// <summary> 6         /// 查找顶级窗口,如果有指定的类名和窗口的名字则表示成功返回一个窗口的句柄。否则返回零。 7         /// </summary> 8         /// <param name="lpClassName">lpClassName参数指向类名</param> 9         /// <param name="lpWindowName">lpWindowName指向窗口名</param>10         /// <returns></returns>11         [DllImport("User32.dll", EntryPoint = "FindWindow")]12         private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);13 14         /// <summary>15         /// 在窗口列表中寻找与指定条件相符的第一个子窗口 16         /// </summary>17         /// <param name="hwndParent">父窗口句柄</param>18         /// <param name="hwndChildAfter">子窗口句柄</param>19         /// <param name="lpszClass">窗口类名</param>20         /// <param name="lpszWindow">窗口名</param>21         /// <returns></returns>22         [DllImport("user32.dll", EntryPoint = "FindWindowEx")]23         private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);24 25         /// <summary>26         /// 该函数将指定的消息发送到一个或多个窗口27         /// </summary>28         /// <param name="hWnd">接收消息的窗口句柄</param>29         /// <param name="Msg">指定被发送的消息类型</param>30         /// <param name="wParam"></param>31         /// <param name="lParam">发送的消息</param>32         /// <returns></returns>33         [DllImport("User32.dll", EntryPoint = "SendMessage")]34         private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);35 36         //define message type37         private const int WM_GETTEXT = 0x000D;38         private const int WM_SETTEXT = 0x000C;39         private const int WM_CLICK = 0x00F5;40 41         public static void Main() 42         {43             SearchWindow();44         }45 46         private static void SearchWindow()47         {48             //主窗口类型名及窗口名49             string lpszParentClassName = ""; 50             string lpszParentWindowName = "Print To File";51             //主窗口句柄52             IntPtr ParenthWnd = new IntPtr(0);53             //子窗口句柄54             IntPtr EdithWnd = new IntPtr(0);55 56             //查到主窗体,得到整个窗体 57             ParenthWnd = FindWindow(null, lpszParentWindowName);58             //判断这个窗体是否有效 59             if (!ParenthWnd.Equals(IntPtr.Zero))60             {61                 //得到FileName这个子窗体,并设置其内容 62                 EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, "Edit", "");63                 if (!EdithWnd.Equals(IntPtr.Zero))64                 {65                     //调用SendMessage方法设置其内容 66                     SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, "你需要输入的文本");67                 }68                 //得到OK这个子窗体,并设置其内容 69                 EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, "Button", "OK");70                 if (!EdithWnd.Equals(IntPtr.Zero))71                 {72                     SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "");73                 }74             }75         } 76     }

       

user32的使用