简体   繁体   English

如何确定控件句柄是否属于本机(或基于本机)win32 ListBox?

[英]How to determine if a control handle is of a native (or based on native one) win32 ListBox?

The problem can be expressed in a simple way like this: Given a control handle (IntPtr), how do we know if it is of a ListBox or in general it would be better if we know what kind of control it is.问题可以用这样的简单方式表达:给定一个控件句柄(IntPtr),我们如何知道它是一个ListBox还是一般来说,如果我们知道它是什么类型的控件会更好。

I suppose that the ListBox creator does not put any info (such as the plain text "ListBox") inside the class name of the ListBox (the class name can be obtained using GetClassName function.我想 ListBox 创建者不会在 ListBox 的类名中放置任何信息(例如纯文本“ListBox”)(可以使用GetClassName函数获取类名。

I need to do this because I need to determine if there is any native win32 ListBox under a certain screen point.我需要这样做,因为我需要确定某个屏幕点下是否有任何本机 win32 ListBox。 The first step is to find the control handle using WindowFromPoint function, but next we need to check if the found handle is of a ListBox or not.第一步是使用WindowFromPoint函数找到控件句柄,但接下来我们需要检查找到的句柄是否属于 ListBox。

Update更新

When I say native listbox I mean the control can be created using CreateWindow and then the class name can be specified with any string.当我说原生列表框时,我的意思是可以使用CreateWindow创建控件,然后可以使用任何字符串指定class name I suppose that class name does not contain any fixed phrase like LISTBOX , as for the ListBox in WindowsForms, the class name of it has a format like WindowsForms10.LISTBOX.app.0.bf771...我想类名不包含任何像LISTBOX这样的固定短语,对于 WindowsForms 中的 ListBox,它的类名的格式类似于WindowsForms10.LISTBOX.app.0.bf771...

Well I have had the idea of sending some special message to the ListBox to test it.好吧,我有向 ListBox 发送一些特殊消息来测试它的想法。 I started trying with the message LB_GETCOUNT , however it actually cannot help in this case.我开始尝试使用消息LB_GETCOUNT ,但在这种情况下它实际上无济于事。

So I've tried another message LB_GETTEXTLEN in which I passed in an invalid item index (such as -1 ) and see if the returned result is totally different and distinct to help recognize a ListBox from other controls.因此,我尝试了另一条消息LB_GETTEXTLEN ,其中我传入了一个无效的项目索引(例如-1 ),并查看返回的结果是否完全不同且不同,以帮助识别来自其​​他控件的 ListBox。 And yes it can help, for the ListBox the returned value is -1 while for other controls the returned value is just 0 .是的,它可以提供帮助,对于 ListBox,返回值是-1而对于其他控件,返回值只是0

Here is the code:这是代码:

//LB is a defined enum for ListBox messages, which can be found in pinvoke
var isListBox = User32.SendMessage(handle, (uint)LB.LB_GETTEXTLEN, -1, 0).ToInt32() != 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM