按钮比如radio、check按钮状态的获得和设置的问题

VC++Button控件

按钮窗口(控件)在MFC中使用CButton表示,CButton包含了三种样式的按钮,Push ButtonCheck BoxRadio Box。所以在利用CButton对象生成按钮窗口时需要指明按钮的风格。

创建按钮:BOOL CButton::Create( LPCTSTR lpszCaption, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );其中lpszCaption是按钮上显示的文字,dwStyle为按钮风格,除了Windows风格可以使用外(如WS_CHILD|WS_VISUBLE|WS_BORDER)还有按钮专用的一些风格。

BS_AUTOCHECKBOX 检查框,按钮的状态会自动改变 Same as a check box, except that a check mark appears in the check box when the user selects the box; the check mark disappears the next time the user selects the box.

BS_AUTORADIOBUTTON 圆形选择按钮,按钮的状态会自动改变 Same as a radio button, except that when the user selects it, the button automatically highlights itself and removes the selection from any other radio buttons with the same style in the same group.

BS_AUTO3STATE 允许按钮有三种状态即:选中,未选中,未定 Same as a three-state check box, except that the box changes its state when the user selects it.

BS_CHECKBOX 检查框 Creates a small square that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style).

BS_DEFPUSHBUTTON 默认普通按钮 Creates a button that has a heavy black border. The user can select this button by pressing the ENTER key. This style enables the user to quickly select the most likely option (the default option).

BS_LEFTTEXT 左对齐文字 When combined with a radio-button or check-box style, the text appears on the left side of the radio button or check box.

BS_OWNERDRAW 自绘按钮 Creates an owner-drawn button. The framework calls the DrawItem member function when a visual aspect of the button has changed. This style must be set when using the CBitmapButton class.

BS_PUSHBUTTON 普通按钮 Creates a pushbutton that posts a WM_COMMAND message to the owner window when the user selects the button.

BS_RADIOBUTTON 圆形选择按钮 Creates a small circle that has text displayed to its right (unless this style is combined with the BS_LEFTTEXT style). Radio buttons are usually used in groups of related but mutually exclusive choices.

BS_3STATE 允许按钮有三种状态即:选中,未选中,未定 Same as a check box, except that the box can be dimmed as well as checked. The dimmed state typically is used to show that a check box has been disabled.

rect为窗口所占据的矩形区域,pParentWnd为父窗口指针,nID为该窗口的ID值。

获取/改变按钮状态:对于检查按钮和圆形按钮可能有两种状态,选中和未选中,如果设置了BS_3STATEBS_AUTO3STATE风格就可能出现第三种状态:未定,这时按钮显示灰色。通过调用int CButton::GetCheck( ) 得到当前是否被选中,返回0:未选中,1:选中,2:未定。调用void CButton::SetCheck( int nCheck );设置当前选中状态。

处理按钮消息:要处理按钮消息需要在父窗口中进行消息映射,映射宏为ON_BN_CLICKED( id, memberFxn )id为按钮的ID值,就是创建时指定的nID值。处理函数原型为afx_msg void memberFxn( )

在处理点击按钮消息函数时,一般使用int CButton::GetCheck( )来获取按钮,比如复选框的状态,如果我们使用了wizzard创建了一个对话框,在对话框里面再创建一个复选框,就可以很清楚的了解到了。

按钮比如radio、check按钮状态的获得和设置的问题

<shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 229.5pt; HEIGHT: 346.5pt" type="#_x0000_t75"><imagedata o:title="QQ截图未命名" src="file:///C:%5CDOCUME~1%5CBTazuo%5CLOCALS~1%5CTemp%5Cmsohtml1%5C01%5Cclip_image001.png"></imagedata></shape>

如下函数:

void ClistcontrolDlg::OnBnClickedCheck1()

{

// TODO: Add your control notification handler code here

CButton *pButton = (CButton *)GetDlgItem(IDC_CHECK1);

BOOL checked = pButton->GetCheck();

//CString a;

//a.Format(_T("%d"), checked);

//MessageBox(a);

if (!checked) {

SetDlgItemText(IDC_STATIC2, _T("NO!"));

pButton->SetCheck(0);

return;

}

else

if (checked) {

SetDlgItemText(IDC_STATIC2, _T("YES!"));

pButton->SetCheck(1);

return;

}

/*

checked = checked % 2;

if (checked) {

SetDlgItemText(IDC_STATIC2, _T("NO!"));

pButton->SetCheck(0);

checked++;

}

else if (!checked) {

SetDlgItemText(IDC_STATIC2, _T("YES!"));

pButton->SetCheck(1);

checked++;

}

*/

}

这里用了两种方案,一种是计数法,就是不获取复选按钮的状态,直接来根据以前点击的次数来,以前选了这次就取消选择,没有的话 就选择,这种方法很“无敌”,就不说了。

没有被注释的方法就是用GetCheck()方法来的:但是有一点要注意,就是GetCheck()是发生在点击鼠标之后的,而不是在鼠标点击之前,而点击鼠标之后,复选框状态已经发生变化,GetCheck()这个函数得到的就是变化之后的状态,比如之前是选择了(得到1)的,那么点击之后就自动的变化了,就是非选择的了。那么这个时候得到的就是0(非选择),那么下面的程序就要相应的用0的状态来编写。

如果这样写是什么结果?!!!

CButton *pButton = (CButton *)GetDlgItem(IDC_CHECK1);

BOOL checked = pButton->GetCheck();

if (checked) {

SetDlgItemText(IDC_STATIC2, _T("NO!"));

pButton->SetCheck(0);

return;

}

else

if (!checked) {

SetDlgItemText(IDC_STATIC2, _T("YES!"));

pButton->SetCheck(1);

return;

}

不管之前是什么状态,结果都是yes,也勾选。

假设之前是勾选,那么点击鼠标时候是非勾选,checked = pButton->GetCheck()这么一来就是0 了,执行else里的内容,勾选又被设置成1了,pButton->SetCheck(1);一瞬间有两个动作,先成0,再成1,等你下次点击的之前又是1了,点击成0,又变成1,……

其实SetCheck()函数也可以不要,系统自动的帮你变了哦。呵呵。

你可能感兴趣的:(qq,F#,ext,mfc,vc++)