VCTEST 介绍VC怎么用按钮控件 联合开发网

VCTEST 介绍VC怎么用按钮控件 联合开发网,第1张

按钮窗口(控件)在MFC中使用CButton表示,CButton包含了三种样式的按钮,Push Button,Check Box,Radio 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_3STATE或BS_AUTO3STATE风格就可能出现第三种状态:未定,这时按钮显示灰色。通过调用int CButton::GetCheck( ) 得到当前是否被选中,返回0:未选中,1:选中,2:未定。调用void CButton::SetCheck( int nCheck );配置当前选中状态。

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

按钮窗口(控件)在MFC中使用CButton表示,CButton包含了三种样式的按钮,Push Button,Check Box,Radio 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_3STATE或BS_AUTO3STATE风格就可能出现第三种状态:未定,这时按钮显示灰色。通过调用int CButton::GetCheck( ) 得到当前是否被选中,返回0:未选中,1:选中,2:未定。调用void CButton::SetCheck( int nCheck );配置当前选中状态。

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

LV_ITEM

lvitem;

LV_COLUMN

lvcolumn;

int

iItem,

iSubItem,

iActualItem;

//定义列表视图的列

for

(i

=

0;

i

<

2;

i++)

{

lvcolumnmask

=

LVCF_FMT

|

LVCF_SUBITEM

|

LVCF_TEXT

|

LVCF_WIDTH;

lvcolumnfmt

=

LVCFMT_LEFT;

lvcolumnpszText

=

rgtsz[i];

lvcolumniSubItem

=

i;

lvcolumncx

=

rectWidth()

(i

+

1)

/

4;

//将第2列宽度设置成第1列宽度3倍

pListCtrl->InsertColumn(i,

&lvcolumn);

//插入列

}

//向列表视图里,添加内容

for

(iItem

=

0;

iItem

<

50;

iItem++)

for

(iSubItem

=

0;

iSubItem

<

2;

iSubItem++)

{

if

(iSubItem

==

0)

iIcon

=

Random()

%

4;

lvitemmask

=

LVIF_TEXT

|

(iSubItem

==

0

LVIF_IMAGE

:

0);

lvitemiItem

=

(iSubItem

==

0)

iItem

:

iActualItem;

lvitemiSubItem

=

iSubItem;

lvitempszText

=

iSubItem

==

0

rgtszIconType[iIcon]

:

rgtszIconDescrip[iIcon];

lvitemiImage

=

iIcon;

//判断,如果是添加第1列,则应该添加一行,否则,只要设置这一行即可。

if

(iSubItem

==

0)

iActualItem

=

pListCtrl->

InsertItem

(&lvitem);

else

pListCtrl->SetItem(&lvitem);

}双击按钮,它会让你决定按钮响应函数的名字,然后它会显示那个函数,你在函数里面添加上面代码

示例代码

std::vector<UINT> buttonIds;

// Get the first child window Use it

HWND hwnd = ::GetWindow( GetSafeHwnd(),

GW_CHILD | GW_HWNDFIRST );

TCHAR className[]=_T("Button");

if (GetClassName(hwnd, className, sizeof(className)/sizeof(TCHAR)) && _tcscmp(className, _T("Button")))

buttonIdspush_back(GetDlgCtrlID(hwnd));

while( hwnd )

{

// Get the next window Use it

hwnd = ::GetWindow( hwnd, GW_HWNDNEXT );

if (GetClassName(hwnd, className, sizeof(className)/sizeof(TCHAR)) && _tcscmp(className, _T("Button")))

buttonIdspush_back(GetDlgCtrlID(hwnd));

}

可以定义一个静态标志变量,初始时设置按钮文字为 开始

代码可以像下面这样

void CTest10Dlg::OnButton1()

{

static bool flag = true;

if (flag)

{

SetDlgItemText(IDC_BUTTON1, "暂停");

flag = false;

}

else

{

SetDlgItemText(IDC_BUTTON1, "开始");

flag = true;

}

}

HWND GetDlgItem(

HWND hDlg, // handle to dialog box

int nIDDlgItem // control identifier

);

使用这个函数就可以了,提供两个参数,第一个是对话框的控件,第二个是控件的ID号。

至于第二个问题,请帖出一个例子。

以上就是关于VCTEST 介绍VC怎么用按钮控件 联合开发网全部的内容,包括:VCTEST 介绍VC怎么用按钮控件 联合开发网、VC++: MFC中怎么实现鼠标单击buttom按钮,在c_list 文本框中显示一串文字、VC 怎么样获取界面上所有Button的ID等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/9751559.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存