雄鹰自由飞翔

导航

 

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

posted on 2020-05-14 22:28  雄鹰自由飞翔  阅读(2258)  评论(0编辑  收藏  举报