如何从句柄区分控件

nmg1852951 2010-09-01 03:32:24
windows控件中的checkbox,radiobutton和button的ClassName都是WindowsForms10.BUTTON.
那么我从界面上获得了某个窗体的句柄,然后发现它的ClassName是WindowsForms10.BUTTON,然后我应该如何区别这个控件是个checkbox,还是radiobutton,或者是button呢?
如果是checkbox,或者radiobutton,那么我应该通过句柄去获得什么属性才能知道它是否被checked呢?
静待佳音中。。。。。。
...全文
142 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
wltg2001 2010-09-02
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 nmg1852951 的回复:]
引用 11 楼 jerrywjh 的回复:
没有源码,那用他们的Caption把,也就是每个控件的文字。

很多控件的Caption都是空的,而且即使有Caption也识别不了控件类型,比如:登陆界面上有个登陆按钮,Caption是“登陆”,但是我也不能因为文字是“登陆”就判断为Button啊。
继续等待解答。。。。。。
[/Quote]
不好意思,没仔细看你的问题,别人的代码,有ID也没有用。
我觉得还是从风格上去判断吧,比如按钮的话,一定会有BS_PUSHBUTTON这个风格,你可以用FindWindowEx取得这个控件的HWND,然后用
DWORD style = GetWindowLong(HWND hWnd,GWL_STYLE); 得到窗口风格,
然后:if(style &BS_PUSHBUTTON)就可以判断是不是按钮了。
对于checkbox,一般会有BS_AUTOCHECKBOX风格,radiobutton一般会有BS_AUTORADIOBUTTON风格。
nmg1852951 2010-09-02
  • 打赏
  • 举报
回复
本帖为总结帖,方便以后遇到和我一样问题的人们参考:
首先,感谢paulcxz,j8daxue和wltg2001(红猪)的解答!
使用GetWindowlong(HWND hWnd,GWL_STYLE)可以获得窗体的style,返回值为一个32位的long型值。其中前16位记录了窗体的WindowStyle,主要的WindowsStyle包括如下:
WS_BORDER
Creates a window that has a thin-line border.

WS_CAPTION
Creates a window that has a title bar (includes the WS_BORDER style).

WS_CHILD
Creates a child window. A window with this style cannot have a menu bar. This style cannot be used with the WS_POPUP style.

WS_CHILDWINDOW
Same as the WS_CHILD style.

WS_CLIPCHILDREN
Excludes the area occupied by child windows when drawing occurs within the parent window. This style is used when creating the parent window.

WS_CLIPSIBLINGS
Clips child windows relative to each other; that is, when a particular child window receives a WM_PAINT message, the WS_CLIPSIBLINGS style clips all other overlapping child windows out of the region of the child window to be updated. If WS_CLIPSIBLINGS is not specified and child windows overlap, it is possible, when drawing within the client area of a child window, to draw within the client area of a neighboring child window.

WS_DISABLED
Creates a window that is initially disabled. A disabled window cannot receive input from the user. To change this after a window has been created, use EnableWindow.

WS_DLGFRAME
Creates a window that has a border of a style typically used with dialog boxes. A window with this style cannot have a title bar.

WS_GROUP
Specifies the first control of a group of controls. The group consists of this first control and all controls defined after it, up to the next control with the WS_GROUP style. The first control in each group usually has the WS_TABSTOP style so that the user can move from group to group. The user can subsequently change the keyboard focus from one control in the group to the next control in the group by using the direction keys.
You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use SetWindowLong.


WS_HSCROLL
Creates a window that has a horizontal scroll bar.

WS_ICONIC
Creates a window that is initially minimized. Same as the WS_MINIMIZE style.

WS_MAXIMIZE
Creates a window that is initially maximized.

WS_MAXIMIZEBOX
Creates a window that has a maximize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.

WS_MINIMIZE
Creates a window that is initially minimized. Same as the WS_ICONIC style.

WS_MINIMIZEBOX
Creates a window that has a minimize button. Cannot be combined with the WS_EX_CONTEXTHELP style. The WS_SYSMENU style must also be specified.

WS_OVERLAPPED
Creates an overlapped window. An overlapped window has a title bar and a border. Same as the WS_TILED style.

WS_OVERLAPPEDWINDOW
Creates an overlapped window with the WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, and WS_MAXIMIZEBOX styles. Same as the WS_TILEDWINDOW style.

WS_POPUP
Creates a pop-up window. This style cannot be used with the WS_CHILD style.

WS_POPUPWINDOW
Creates a pop-up window with WS_BORDER, WS_POPUP, and WS_SYSMENU styles. The WS_CAPTION and WS_POPUPWINDOW styles must be combined to make the window menu visible.

WS_SIZEBOX
Creates a window that has a sizing border. Same as the WS_THICKFRAME style.

WS_SYSMENU
Creates a window that has a window menu on its title bar. The WS_CAPTION style must also be specified.

WS_TABSTOP
Specifies a control that can receive the keyboard focus when the user presses the TAB key. Pressing the TAB key changes the keyboard focus to the next control with the WS_TABSTOP style.
You can turn this style on and off to change dialog box navigation. To change this style after a window has been created, use SetWindowLong.


WS_THICKFRAME
Creates a window that has a sizing border. Same as the WS_SIZEBOX style.

WS_TILED
Creates an overlapped window. An overlapped window has a title bar and a border. Same as the WS_OVERLAPPED style.

WS_TILEDWINDOW
Creates an overlapped window with the WS_OVERLAPPED, WS_CAPTION, WS_SYSMENU, WS_THICKFRAME, WS_MINIMIZEBOX, and WS_MAXIMIZEBOX styles. Same as the WS_OVERLAPPEDWINDOW style.

WS_VISIBLE
Creates a window that is initially visible.
This style can be turned on and off by using ShowWindow or SetWindowPos.


WS_VSCROLL
Creates a window that has a vertical scroll bar.

而返回值的下16位,根据窗体class的不同,代表不同的style,class为button,则下16位为button style,class为static,则表示为static style,别的class可以以此类推。
主要的Buttonstyle包括如下:
BS_3STATE

BS_AUTO3STATE

BS_AUTOCHECKBOX

BS_AUTORADIOBUTTON

BS_CHECKBOX

BS_DEFPUSHBUTTON

BS_GROUPBOX

BS_LEFTTEXT

BS_OWNERDRAW

BS_PUSHBUTTON

BS_RADIOBUTTON

BS_USERBUTTON

BS_BITMAP

BS_BOTTOM

BS_CENTER

BS_ICON

BS_FLAT

BS_LEFT

BS_MULTILINE

BS_NOTIFY

BS_PUSHLIKE

BS_RIGHT

BS_RIGHTBUTTON

BS_TEXT

BS_TOP

BS_TYPEMASK

BS_VCENTER

BS_SPLITBUTTON

BS_DEFSPLITBUTTON

BS_COMMANDLINK

BS_DEFCOMMANDLINK

根据Button Style的不同可以区别class为button的窗体,到底是个button,还是checkbox,亦或者radiobutton。不过局限性在于,这种方式仅限于windows的基本控件。很多button style 为BS_OWNERDRAW 的无法区分这个button到底是什么类型。

类似的 使用GetWindowlong(HWND hWnd,GWL_EXSTYLE)可以获得Extended style。

关于获得选中的Index和选中的文本,可以使用LB_GETCURSEL和LB_GETTEXT来获得Listbox中选中的序号和选中的文本,使用CB_GETCURSEL和CB_GETLBTEXT来获得Combox中选中的序号和选中的文本。
OK,总结完毕,结贴,散分~虽然新手能给的分数比较少,希望各位大哥别嫌弃。
nmg1852951 2010-09-02
  • 打赏
  • 举报
回复
饿,写了一整天代码,写昏头了。
本来是想问,如何确定Radiobutton和Checkbox是否被选中的,结果傻乎乎的把Listbox和Combobox的东西写上来了。
判断Radiobutton和Checkbox是否选中,是使用IsDlgButtonChecked来实现的参考:http://baike.baidu.com/view/1079839.htm?fr=ala0_1

nmg1852951 2010-09-02
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wltg2001 的回复:]

引用 14 楼 nmg1852951 的回复:
引用 11 楼 jerrywjh 的回复:
没有源码,那用他们的Caption把,也就是每个控件的文字。

很多控件的Caption都是空的,而且即使有Caption也识别不了控件类型,比如:登陆界面上有个登陆按钮,Caption是“登陆”,但是我也不能因为文字是“登陆”就判断为Button啊。
继续等待解答。。。。。。

不好意思,……
[/Quote]
谢谢红猪的解答,目前所找到的解决办法似乎的确只有那么一种是可行的。不过我现在的疑问是,如何用WindowsAPI或者发送什么消息,能够让我获得ListBox中选中的文本,或者选中的Index呢?类似的Combox应该如何处理?
nmg1852951 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 jerrywjh 的回复:]
没有源码,那用他们的Caption把,也就是每个控件的文字。
[/Quote]
很多控件的Caption都是空的,而且即使有Caption也识别不了控件类型,比如:登陆界面上有个登陆按钮,Caption是“登陆”,但是我也不能因为文字是“登陆”就判断为Button啊。
继续等待解答。。。。。。
nmg1852951 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 wltg2001 的回复:]
引用 7 楼 nmg1852951 的回复:
引用 6 楼 wltg2001 的回复:

区分控件当然应该是用ID了,怎么用窗口类呢

ID是指编码时,控件的名字么?
那样的话,对我来说就难以实现了。因为我是在辨别一个已经发布的Windows Application,我只有exe文件,而没有软件的源码,所以我也就无法获得控件的ID了。

没有源码也有办法取得ID啊,用SPY++看一……
[/Quote]
恩,用Spy++看过了,Control ID 是一组数字,没办法用来识别控件的类型啊。
wltg2001 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 nmg1852951 的回复:]
引用 6 楼 wltg2001 的回复:

区分控件当然应该是用ID了,怎么用窗口类呢

ID是指编码时,控件的名字么?
那样的话,对我来说就难以实现了。因为我是在辨别一个已经发布的Windows Application,我只有exe文件,而没有软件的源码,所以我也就无法获得控件的ID了。
[/Quote]
没有源码也有办法取得ID啊,用SPY++看一下就知道了。
skyfree 2010-09-01
  • 打赏
  • 举报
回复
没有源码,那用他们的Caption把,也就是每个控件的文字。
skyfree 2010-09-01
  • 打赏
  • 举报
回复
每个控件都有ID,用ID来区分,不用句柄
zgsdzhaolanxiang1 2010-09-01
  • 打赏
  • 举报
回复
mark...
Eleven 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用楼主 nmg1852951 的回复:]
windows控件中的checkbox,radiobutton和button的ClassName都是WindowsForms10.BUTTON.
那么我从界面上获得了某个窗体的句柄,然后发现它的ClassName是WindowsForms10.BUTTON,然后我应该如何区别这个控件是个checkbox,还是radiobutton,或者是button呢?
如果是checkbox,或者r……
[/Quote]
都是Button的话,只能从Style上区分了
nmg1852951 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wltg2001 的回复:]

区分控件当然应该是用ID了,怎么用窗口类呢
[/Quote]
ID是指编码时,控件的名字么?
那样的话,对我来说就难以实现了。因为我是在辨别一个已经发布的Windows Application,我只有exe文件,而没有软件的源码,所以我也就无法获得控件的ID了。
wltg2001 2010-09-01
  • 打赏
  • 举报
回复
区分控件当然应该是用ID了,怎么用窗口类呢
j8daxue 2010-09-01
  • 打赏
  • 举报
回复
DWORD style = GetWindowLong(hwnd,GWL_STYLE);
if (style & BS_xxxx)
nmg1852951 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 paulcxz 的回复:]

bool btnstyle = GetWindowLong (hwnd, GWL_STYLE, IDC_BUTTON) & BS_TYPEMASK;
// this will look for only a BS_AUTOCHECKBOX
If (btnstyle == BS_AUTOCHECKBOX)
[/Quote]
这个思路我可以理解,目前也在向这个方向努力中,不过安装MSDN的解释getwindowlong(hwnd,GWL_STYLE)返回的应该是windowstyle,而不是buttonstyle。
而且getwindowlong应该是2个参数的,为什么paulcxz 有三个参数,难道getwindowlong有重载的?
一桶姜山 2010-09-01
  • 打赏
  • 举报
回复
bool btnstyle = GetWindowLong (hwnd, GWL_STYLE, IDC_BUTTON) & BS_TYPEMASK;
// this will look for only a BS_AUTOCHECKBOX
If (btnstyle == BS_AUTOCHECKBOX)
nmg1852951 2010-09-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 aaadddzxc 的回复:]

消息!
[/Quote]
能够请aaadddzxc解释的详细点么?难道向控件发送消息,根据控件对消息的反应来区分控件的类型么?
xengine-qyt 2010-09-01
  • 打赏
  • 举报
回复
消息!

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧