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 type
      37         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     }
时间: 2024-10-07 09:50:54

user32的使用的相关文章

转整理分享C#通过user32.dll模拟物理按键操作的代码

对系统模拟按键方面的知识和按键映射代码做了一下梳理,在这里分享出来,适用于开发自动操作工具和游戏外挂. 主代码: public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key click flag public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag [DllImport("user32.dll")] private static extern void keybd_event(

Python 调用 user32.dll

import ctypes h = ctypes.windll.LoadLibrary("C:\\Windows\\System32\\user32.dll") h.MessageBoxW(0, u'内容', u'标题', 0)

C#:使用Window自带函数(如:user32.dll)

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 [DllImport("user32.dll", EntryPoint = "GetScrollInfo", CallingConvention = CallingConvention.StdCall)] public static extern bool GetScrollInfo(IntPtr hwnd, int f

User32.dll详细介绍

RegisterServiceProcess(ProcessID:Long,Type:Long) 该函数存在于Kernal32.dll中. Process指向进程的ID,Type表示是否向系统注册该进程,是1,否0.= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

【整理】c# 调用windows API(user32.dll)

User32.dll提供了很多可供调用的接口,大致如下(转自http://blog.csdn.net/zhang399401/article/details/6978803) using System;    using System.Collections.Generic;    using System.Linq;    using System.Text;    using System.Runtime.InteropServices;       namespace WindowsAPI

整理分享C#通过user32.dll模拟物理按键操作的代码

对系统模拟按键方面的知识和按键映射代码做了一下梳理,在这里分享出来,适用于开发自动操作工具和游戏外挂. 主代码: public const int KEYEVENTF_EXTENDEDKEY = 0x0001; //Key click flag public const int KEYEVENTF_KEYUP = 0x0002; //Key up flag [DllImport("user32.dll")] private static extern void keybd_event(

user32.dll

1 user32.dll中的所有函数 2 3 4 5 6 using System; 7 using System.Collections.Generic; 8 using System.Linq; 9 using System.Text; 10 using System.Runtime.InteropServices; 11 12 namespace WindowsAPI 13 { 14 class CSharp_Win32Api 15 { 16 #region User32.dll 函数 1

准确看看 user32.dll 里有哪些导出函数(win7-32)

看看 user32.dll里有哪些导出函数,大家都会,但准确性???以MS dumpbin为标准,要做出来结果一模一样,才表示代码完全正确. 直接上代码: 1 // ListExport.cpp : Defines the entry point for the console application. 2 // 3 #include "stdafx.h" 4 #include <windows.h> 5 #include <winnt.h> 6 7 exter

【转】c# 调用windows API(user32.dll)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace WindowsAPI { class CSharp_Win32Api { #region User32.dll 函数 /// <summary> /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以