簡體   English   中英

為什么“ FindWindowEx”找不到RichTextBox組件

[英]Why “FindWindowEx” can't find RichTextBox component

我正在做一個自動程序(C#,不是C ++),我需要以表格的形式獲取RichTextBox。 我已經使用Spy++來獲取標題和類名,但是FindWindowEx始終找不到RichTextBox ,而GetLastError得到單詞0 然后,這是一個簡單的示例。

IntPtr parent = FindWindow(null, "Form1");
if (parent!=IntPtr.Zero) {
    //find test1 textbox
    IntPtr child = FindWindowEx(parent, 0,null,  "test1");
    if (child!=IntPtr.Zero) {
        SendMessage(child, 0x000c, 0, lParam:  "test");
    } else {
        Console.WriteLine("textbox can't be found");
    }
    //find test2 richtextbox
    IntPtr childRich = FindWindowEx(parent, 0, null, "test2");
    if (childRich != IntPtr.Zero) {
        SendMessage(child, 0x000c, 0, lParam: "test");
    } else {
        Console.WriteLine("richtextbox can't be found");
    }
} else {
    Console.WriteLine("Form1 can't be found");
}

https://i.stack.imgur.com/7eWil.png

但是結果是richtextbox can't find 幫我。

我真的不認為這是最好的方法,但確實如此。

對於這種特定情況,您可以在表單中搜索所有處理程序,然后更改所需的處理程序。

var iHandle = Win32.FindWindow(null, "Form1");
var allItems = Win32.GetAllChildrenWindowHandles((IntPtr)iHandle, int.MaxValue);
Win32.SendMessage(allItems[1], 0x000c, 0, lParam: "Now you can change the text!");

我已經測試過,allItems [1]將始終是同一項目,我認為這是在winForm中從上至下對項目進行排序的方式。

我正在為Win方法使用第二類:

public class Win32
{
    public const int WM_SETTEXT = 0X000C;

    public static List<IntPtr> GetAllChildrenWindowHandles(IntPtr hParent, int maxCount)
    {
        var result = new List<IntPtr>();
        int ct = 0;
        IntPtr prevChild = IntPtr.Zero;
        IntPtr currChild = IntPtr.Zero;
        while (true && ct < maxCount)
        {
            currChild = FindWindowEx(hParent, prevChild, null, null);
            if (currChild == IntPtr.Zero) break;
            result.Add(currChild);
            prevChild = currChild;
            ++ct;
        }
        return result;
    }

    [DllImport("user32.dll")]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [DllImport("User32.dll")]
    public static extern int FindWindow(string strClassName, string strWindowName);
}

編輯:從以下網址獲取所有子窗口句柄的方法: https//jamesmccaffrey.wordpress.com/2013/02/03/getting-all-child-window-handles-using-c-pinvoke-findwindowex/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM