简体   繁体   中英

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.

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.

I need to do this because I need to determine if there is any native win32 ListBox under a certain screen point. 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.

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. 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...

Well I have had the idea of sending some special message to the ListBox to test it. I started trying with the message LB_GETCOUNT , however it actually cannot help in this case.

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. And yes it can help, for the ListBox the returned value is -1 while for other controls the returned value is just 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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