如何使用Control.FromHandle?
我看到一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
仅当您传入的句柄实际上是应用程序中的
Control
时,此方法才有效。This method only works if the handle you pass in actually is a
Control
in your application.在您的特定情况下,由于您只想设置文本,因此请调用
In your particular case, since you just want to set the text, call SetWindowText from user32.dll
对于任何其他通过谷歌搜索找到这个答案并想知道为什么会出现这种行为的人,这篇文章 http ://www.codeguru.com/forum/showthread.php?t=443191 特别具有启发性,特别是“MadHatter”的最后一篇文章:
进一步阅读您的问题,您似乎正在尝试进行一些自动化操作,或者至少以某种方式操纵窗口。我是否建议您查看 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':
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.