徐蕴峰 2023-03-02 15:27 采纳率: 0%
浏览 20

C#获得主窗体的句柄是可以的。但是不能获得edit控件的句柄。

C#获得主窗体的句柄是可以的。但是不能获得edit控件的句柄。

img


准备用句柄调取程序的窗体结构。如图
代码如下:
string THS_Version = "网上股票交易系统5.0";
hwnd_Trade_Form = FindWindow(null, THS_Version) ;
IntPtr hwnd_Buy_Form_StockCodeEdit = FindWindowEx(hwnd_Trade_Form, IntPtr.Zero, "Edit", "");
问题:获得主窗体的句柄是可以的。但是不能获得edit控件的句柄。

追加:inspect可以看到的。

img

  • 写回答

2条回答 默认 最新

  • 赵4老师 2023-03-02 16:16
    关注

    如果使用inspect软件也看不到你所谓edit控件对应的窗口句柄,那就别指望代码能获取了。
    没准人家用的是自绘,根本没有对应的窗口,何来窗口句柄一说?
    你既然提到inspect软件可以,那你有没有注意inspect软件采用的是UI Automation技术?
    百度搜“UI Automation”
    仅供参考:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using System.Windows.Automation;
    
    namespace CStest
    {
        class Program
        {
            static void Main(string[] args)
            {
                //System.Diagnostics.Process.Start("calc");
                //Thread.Sleep(3000); //Sleep 3 sconds to wait calculator launched
    
                try
                {
                    //Get the destkop element
                    AutomationElement elemDesktop = AutomationElement.RootElement;
    
                    //Search the Application main window by title from all children
                    PropertyCondition pCondition = new PropertyCondition(AutomationElement.NameProperty, "计算器");
                    AutomationElement elemApplicationWindow = elemDesktop.FindFirst(TreeScope.Children, pCondition);
    
                    //Search the 1 button
                    AutomationElement btnOne = elemApplicationWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "一"));
                    InvokePattern invokePattern1 = btnOne.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                    invokePattern1.Invoke();
    
                    AutomationElement btnPlus = elemApplicationWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "加"));
                    InvokePattern invokePatternP = btnPlus.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                    invokePatternP.Invoke();
    
                    AutomationElement btnTwo = elemApplicationWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "二"));
                    InvokePattern invokePattern2 = btnTwo.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                    invokePattern2.Invoke();
    
                    AutomationElement btnE = elemApplicationWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "等于"));
                    InvokePattern invokePatternE = btnE.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                    invokePatternE.Invoke();
    
                    //Verify the result by get the Name property
                    AutomationElement labelResult = elemApplicationWindow.FindFirst(TreeScope.Subtree, new PropertyCondition(AutomationElement.NameProperty, "显示为 3"));
                    if (labelResult.Current.Name == "显示为 3")
                    {
                        Console.WriteLine("Test case pass!");
                    }
                    else
                    {
                        Console.WriteLine("Test case fail!");
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("Test case error!");
                }
            }
        }
    }
    
    
    
    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 3月2日

悬赏问题

  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥100 解决数据不连续出现问题
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?
  • ¥15 C#连接不上服务器,