MFC组合控件即CComboBox Class,这个控件的Style有三种形式:
Style | When is list box visible? | Static or edit control? |
---|---|---|
Simple | Always | Edit |
Drop-down | When dropped down | Edit |
Drop-down list | When dropped down | Static |
其中第一种为最简单的Edit控件,第二种是可选择可编辑下拉控件,第三种是只可选择控件。这个控件常用的一些控件消息有:
-
ON_CBN_CLOSEUP (Windows 3.1 and later.) The list box of a combo box has closed. This notification message is not sent for a combo box that has the CBS_SIMPLE style.
-
ON_CBN_DBLCLK The user double-clicks a string in the list box of a combo box. This notification message is only sent for a combo box with the CBS_SIMPLE style. For a combo box with the CBS_DROPDOWN or CBS_DROPDOWNLIST style, a double-click cannot occur because a single click hides the list box.
-
ON_CBN_EDITCHANGE The user has taken an action that may have altered the text in the edit-control portion of a combo box. Unlike the CBN_EDITUPDATE message, this message is sent after Windows updates the screen. It is not sent if the combo box has the CBS_DROPDOWNLIST style.
-
ON_CBN_EDITUPDATE The edit-control portion of a combo box is about to display altered text. This notification message is sent after the control has formatted the text but before it displays the text. It is not sent if the combo box has the CBS_DROPDOWNLIST style.
-
ON_CBN_KILLFOCUS The combo box is losing the input focus.
-
ON_CBN_SELCHANGE The selection in the list box of a combo box is about to be changed as a result of the user either clicking in the list box or changing the selection by using the arrow keys. When processing this message, the text in the edit control of the combo box can only be retrieved via GetLBText or another similar function. GetWindowText cannot be used.
-
ON_CBN_SETFOCUS The combo box receives the input focus.
-
在使用的过程中发现了Drop-down和Drop-down list的另一个区别,浪费了我好半天时间。
-
当控件的风格为Drop-down时,消息ON_CBN_SELCHANGE、ON_CBN_CLOSEUP被响应时,控件的当前值还是原来的值,没有被改变。比如控件的选项值是一个int值,你用GetDlgItemInt(IDC)时,得到的值是旧值。只有在响应ON_CBN_KILLFOCUS消息,即控件失去焦点时,控件的值才被刷新。
-
当控件的风格为Drop-down list时,消息ON_CBN_SELCHANGE、ON_CBN_CLOSEUP、ON_CBN_KILLFOCUS消息被响应时,控件里的值已经被刷新。比如控件的选项值是一个int值,你用GetDlgItemInt(IDC)时,得到的值是新值。
-
可能是因为当控件的是可编辑的时候,只有当控件失去焦点时才算真正输入完毕,所以那时的值才是最新的值。