当有多个文本框时,使用FindWindowEx()? [英] FindWindowEx() when there are more than one textbox?

查看:103
本文介绍了当有多个文本框时,使用FindWindowEx()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想问个问题.我正在使用以下功能来学习Windows窗体中文本框的句柄.但是有3个不同的文本框,并且它们都有相同的类名(WindowsForms10.EDIT.app.0.378734a).我怎么知道我找到合适的人了?

在此先感谢

礼来

childhandle = FindWindowEx(handle,IntPtr.Zero,"WindowsForms10.EDIT.app.0.378734a",null);

Hello Guys,

I want to ask a question. I am using the following function to learn the handle of a textbox in a windows form. But there are 3 different text boxes and they all have same class name(WindowsForms10.EDIT.app.0.378734a). How Can I know that I find the right one?

Thanks in advance

Lilly

childhandle = FindWindowEx(handle, IntPtr.Zero, "WindowsForms10.EDIT.app.0.378734a", null);

推荐答案

这不是问题.类是相同的,但是句柄在不同的实例中总是不同的.在您的示例中,如果它返回成功找到的句柄,则其值将与文本框实例之一的TextBox.Handle(从Control继承)的值相同.这样,您进行了一次往返.参见:
http://msdn.microsoft.com/en-us/library/system. windows.forms.textbox.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.control.aspx [ ^ ].

实际上,永远不需要此操作,因为您始终可以通过此属性拥有此句柄.

我不知道您为什么会尝试使用Windows API查找它,也许是因为您不知道此属性.

一个警告:在正确的.NET UI开发中不需要使用句柄和Windows API(通过P/Invoke).使用纯.NET库可以解决大多数问题.而且,使用句柄会破坏与其他平台的兼容性.您是否知道编写准确的Forms应用程序可以在无需重新编译的许多平台上运行,而不仅仅是Windows?可以使用Mono( http://en.wikipedia.org/wiki/Mono_%28software%29 [ ^ ]).使用句柄(HWND)或P/Invoke将破坏这种兼容性.

太糟糕了,您没有分享自己的最终目标.如果可以,我们将为您提供有关如何使用纯.NET库做事的好建议.

—SA
This is not a problem. The class is the same but the handles are always different in different instances. In your example, if it returns a successfully found handle, its value will the same as the value of TextBox.Handle (inherited from Control) of one of the instances of your text boxes. In this way, you made a round trip. See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

In practice, this is never needed as you always have this handle through this property.

I don''t know why would you try to find it using Windows API, perhaps because you did not know about this property.

A word of warning: the use of handles and Windows API (via P/Invoke) is not needed in correct .NET UI development; most of the problems can be solved using pure .NET libraries. Moreover, using handles would break compatibility with other platforms. Did you know that accurately written Forms application can run on many platforms without recompilation, not just Windows? This can be done using Mono (http://en.wikipedia.org/wiki/Mono_%28software%29[^]). Using handles (HWND) or P/Invoke will break such compatibility.

Too bad you did not share your ultimate goal. If you did, chances are, we would give you a good advice on how to do things using pure .NET libraries.

—SA


这篇关于当有多个文本框时,使用FindWindowEx()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆