zoukankan      html  css  js  c++  java
  • C# API sendmessage()函数获取文本框文本 (WM_GETTEXT = 0x000D)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApp1
    {
      class Program
        {
    #region 获取另一系统文本框值
          [DllImport("User32.dll", EntryPoint = "FindWindow")]
          public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

          [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
          public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);

          [DllImport("User32.dll", EntryPoint = "FindEx")]
          public static extern IntPtr FindEx(IntPtr hwnd, IntPtr hwndChild, string lpClassName, string lpWindowName);

          [DllImport("user32.dll", EntryPoint = "SendMessageA")]
          private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam);
    #endregion
        public static int WM_GETTEXT = 0x000D;
        static void Main(string[] args)
        {
          IntPtr maindHwnd = FindWindow(null, "Form1"); //获得句柄
          if (maindHwnd != IntPtr.Zero)
          {
            Console.WriteLine("找到了窗体!");
            IntPtr maindHwndp = FindWindowEx(maindHwnd, IntPtr.Zero, null, null);
            if (maindHwndp != IntPtr.Zero)
            {
              Console.WriteLine("找到输入框!");
              const int buffer_size = 1024;
              StringBuilder buffer = new StringBuilder(buffer_size);
              SendMessage(maindHwndp, WM_GETTEXT, buffer_size, buffer);
              Console.WriteLine(string.Format("取到的值是:{0}", buffer.ToString()));//取值一直是空字符串
            }
          }
          else
          {
            Console.WriteLine("没有找到窗口");
          }

        }
      }

    }
    ————————————————
    版权声明:本文为CSDN博主「魔尊X」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/mozunx/article/details/79336919

  • 相关阅读:
    安装部署Python开发环境
    CentOS系统常见优化
    chm文件打开无法显示
    数据库恢复技术
    视图的认识
    存储过程的认识
    error C2471: 无法更新程序数据库 ,fatal error C1083: 无法打开程序数据库文件
    ubuntu下使用aptget install下载安装文件管理
    转:[译文] 程序员的禅修之路
    数据库的两段锁协议
  • 原文地址:https://www.cnblogs.com/2SBC/p/12891808.html
Copyright © 2011-2022 走看看