如何使用Control.FromHandle?

发布于 2024-08-22 03:38:35 字数 987 浏览 11 评论 0原文

我看到一个名为 Control.FromHandle 的方法,它(应该)允许您访问它。 现在,我想使用这段代码尝试一下,

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.

    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

    [DllImport("user32.dll")]
    private static extern IntPtr GetDC(IntPtr hwnd);
    [DllImport("user32.dll")]
    private static extern bool ReleaseDC(IntPtr hwnd, IntPtr hdc);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        IntPtr ptr = FindWindowByCaption(IntPtr.Zero, "Download");
        Control f = Control.FromHandle(ptr);
        f.Text = "Something";
    }

但显然它不起作用。 我亲自检查了句柄是正确的......但该方法返回一个空控件。 有解释一下吗?

I saw a method called Control.FromHandle which (should) give you the access to it.
Now, I wanted to try it using this code

    [DllImport("user32.dll", SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    // Find window by Caption only. Note you must pass IntPtr.Zero as the first parameter.

    [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

    [DllImport("user32.dll")]
    private static extern IntPtr GetDC(IntPtr hwnd);
    [DllImport("user32.dll")]
    private static extern bool ReleaseDC(IntPtr hwnd, IntPtr hdc);

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        IntPtr ptr = FindWindowByCaption(IntPtr.Zero, "Download");
        Control f = Control.FromHandle(ptr);
        f.Text = "Something";
    }

but it won't, obviously, work.
I checked personally that the handle is correct... but the method returns a null control.
Any explaining?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

徒留西风 2024-08-29 03:38:35

仅当您传入的句柄实际上是应用程序中的 Control 时,此方法才有效。

This method only works if the handle you pass in actually is a Control in your application.

苄①跕圉湢 2024-08-29 03:38:35

在您的特定情况下,由于您只想设置文本,因此请调用

In your particular case, since you just want to set the text, call SetWindowText from user32.dll

向日葵 2024-08-29 03:38:35

对于任何其他通过谷歌搜索找到这个答案并想知道为什么会出现这种行为的人,这篇文章 http ://www.codeguru.com/forum/showthread.php?t=443191 特别具有启发性,特别是“MadHatter”的最后一篇文章:

从反射器中 Control.FromHandle 中发生的情况来看,看起来就像将窗口添加到 .net 世界中一样,它存储了一个已加载的句柄表,当您传入一个未在其表中列出的句柄。可能有一些 hack 允许您通过从 .net 应用程序内创建窗口时使用的所有子系统来注册窗口,但直接通过 windows api 包装您需要的任何功能可能会更好/更一致然后尝试破解 Control.FromHandle 以允许您访问/操作其他进程的窗口。

进一步阅读您的问题,您似乎正在尝试进行一些自动化操作,或者至少以某种方式操纵窗口。我是否建议您查看 SourceForge 上的托管 Windows API 项目。它写得很好,我们已将其用于您所描述的目的。

For anyone else googling that found this answer and wondered why this behavior is the case this post http://www.codeguru.com/forum/showthread.php?t=443191 is particularly enlightening, specifically the last post from 'MadHatter':

well just from looking over what goes on in Control.FromHandle in reflector, it looks like as windows are added to the .net world, it stores a table of what handles it has loaded, the problem comes when you pass in a handle that is not listed in its tables. There may be some hack that would allow you to register the window through all the subsystems that are used when creating windows from within the .net app, but it would probably be better / more consistent to wrap whatever functionality you need directly through the windows api then try to hack in Control.FromHandle to allow you to access / manipulate the window of some other process.

Reading more into your question it seems like you are trying to do some automation or at very least manipulate the window in some way. Might I recommend looking at the Managed Windows API project on SourceForge. It is pretty well written and we've used it for the purposes you are describing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文