首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么"FindWindowEx“找不到RichTextBox组件

为什么"FindWindowEx“找不到RichTextBox组件
EN

Stack Overflow用户
提问于 2018-06-01 16:07:46
回答 1查看 223关注 0票数 2

我正在做一个自动程序(C#,不是C++),我需要在表单中获得一个RichTextBox。我使用Spy++来获取标题和类名,但是FindWindowEx总是找不到RichTextBox,而GetLastError获取的是单词0。这是一个简单的例子。

代码语言:javascript
复制
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");
}

但是结果是richtextbox can't find。帮帮我。

EN

回答 1

Stack Overflow用户

发布于 2018-06-01 16:42:57

我真的不认为这是最好的方法,但它是有意义的。

对于这种特定情况,您可以搜索表单中的所有处理程序,然后更改所需的处理程序。

代码语言:javascript
复制
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!");

我已经测试过了,allItems1将始终是相同的项目,我认为这是项目在winForm中从上到下排序的方式。

我为Win方法使用了第二个类:

代码语言:javascript
复制
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/

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50638850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档