如果在按钮风格中没有勾上owner draw(自绘)风格,那么响应DrawItem函数void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)必须要在类似
PreSubclassWindow函数功能中设置BS_OWNERDRAW风格
void CMyButton::PreSubclassWindow()
{
CButton::PreSubclassWindow();
UINT nBS;
nBS = GetButtonStyle();
m_nTypeStyle = nBS & SS_TYPEMASK;
if (m_nTypeStyle == BS_DEFPUSHBUTTON)
{
m_bIsDefault = TRUE;
m_nTypeStyle = BS_PUSHBUTTON;
}
ModifyStyle(SS_TYPEMASK, BS_OWNERDRAW, SWP_FRAMECHANGED);
CRect rect;
GetClientRect(rect);
m_rgn.DeleteObject();
SetWindowRgn(NULL, FALSE);
if(intButtonFace==1)
m_rgn.CreateRoundRectRgn(rect.left,rect.top,rect.right,rect.bottom,10,10);
if(intButtonFace==0)
m_rgn.CreateRectRgn(rect.left,rect.top,rect.right,rect.bottom);
if(intButtonFace==2)
m_rgn.CreateEllipticRgn(rect.left,rect.top,rect.right,rect.bottom);
SetWindowRgn(m_rgn, TRUE);
ClientToScreen(rect);
CWnd* pParent = GetParent();
if (pParent) pParent->ScreenToClient(rect);
MoveWindow(rect.left, rect.top, rect.Width(), rect.Height(), TRUE);
}
或者在对话框初始化中设置:
BOOL CTest4Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_ImageList.Create(16,16,ILC_COLOR24|ILC_MASK,1,0);
m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));
m_mybutton.SetImageList(&m_ImageList);
m_mybutton.SetButtonStyle(BS_OWNERDRAW,TRUE);//m_mybutton是自定义按钮CMyButton的一个实例对象
return TRUE; // return TRUE unless you set the focus to a control
}