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

如果使用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!");
            }
        }
    }
}


vs2022,找到相关引用啊。。好像在.net3中才有的。。。可是vs2022是.net6啊。。。怎么引用。。。百度也没找到