怎样在PropertySheet中添加按钮

时间:2022-05-03 10:32:17

 1、用ClassWizard从CPropertySheet中派出出一个类CMyPropertySheet;
      2、在CMyPropertySheet.h中添加一个成员变量CButton m_ButtonCopy;在资源文件resource.h中添加一个资源ID:#define IDC_BUTTON_COPY 0x2000,这个ID就是我们将要用到的Copy按钮的ID;
  3、重载CPropertySheet::OnInitDialog函数,并插入以下代码:

CRect rect, tabrect;int width;

//Get button sizes and positions

GetDlgItem(IDOK)->GetWindowRect(rect);

GetTabControl()->GetWindowRect(tabrect);

ScreenToClient(rect);

ScreenToClient(tabrect);

//New button -> width, height and Y-coordiate of IDOK

// -> X-coordinate of tab control

width = rect.Width();rect.left = tabrect.left;

rect.right = tabrect.left + width;

//Create new "Add" button and set standard fontm_

ButtonCopy.Create("Copy",BS_PUSHBUTTON|WS_CHILD|WS_VISIBLE|WS_TABSTOP, rect, this, IDC_BUTTON_COPY);

m_ButtonCopy.SetFont(GetFont());

运行程序,可以看到Copy按钮了,但此时点按钮并不会有什么动作。为了处理该按钮的鼠标单击事件,需要在消息映射表中添加如下一行代码:

最后,添加一个函数OnButtonCopy(),在里面添加自己的代码就行了。

BEGIN_MESSAGE_MAP(CMyPropertySheet, CPropertySheet)

ON_BN_CLICKED(IDC_BUTTON_COPY, OnButtonCopy)

END_MESSAGE_MAP()