• 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

  • 相关阅读:
    Form表单提交数据的几种方式
    前端基础-HTML
    python入门函数详解
    Python作业编写
    Python入门数据类型详解
    Jquery选择器
    做外链接和有外链接区别
    三层架构
    drop,delete,truncate区别
    run()和star()区别
  • 原文地址:https://www.cnblogs.com/2SBC/p/12891808.html
Copyright © 2020-2023  润新知