15,979
社区成员
发帖
与我相关
我的任务
分享
// NOTE: CMyButton is a class derived from CButton. The CMyButton
// object was created as follows:
//
// CMyButton myButton;
// myButton.Create(_T("My button"),
// WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_OWNERDRAW,
// CRect(10,10,100,30), pParentWnd, 1);
//
// This example implements the DrawItem method for a CButton-derived
// class that draws the button's text using the color red.
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
UINT uStyle = DFCS_BUTTONPUSH;
// This code only works with buttons.
ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);
// If drawing selected, add the pushed style to DrawFrameControl.
if (lpDrawItemStruct->itemState & ODS_SELECTED)
uStyle |= DFCS_PUSHED;
// Draw the button frame.
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem,
DFC_BUTTON, uStyle);
// Get the button's text.
CString strText;
GetWindowText(strText);
// Draw the button text using the text color red.
COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
::DrawText(lpDrawItemStruct->hDC, strText, strText.GetLength(),
&lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}
if (lpDrawItemStruct->CtlID == IDC_BTN_READ)
{
//先求出按钮的状态
UINT uStyle = DFCS_BUTTONPUSH;
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
uStyle |= DFCS_PUSHED;
}
//以按钮的状态,画出默认的框框
::DrawFrameControl(lpDrawItemStruct->hDC,
&lpDrawItemStruct->rcItem,
DFC_BUTTON,
uStyle);
//求按钮的文字
TCHAR strBtn[200];
::GetDlgItemText(m_hWnd,lpDrawItemStruct->CtlID,strBtn,200);
//设颜色
COLORREF crOld = ::SetTextColor(lpDrawItemStruct->hDC,
RGB(0,0,255)); //设蓝色
//画字
::DrawText(lpDrawItemStruct->hDC,strBtn,_tcslen(strBtn),
&lpDrawItemStruct->rcItem,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
//设回原来的颜色
::SetTextColor(lpDrawItemStruct->hDC,crOld);
}
void CBmpConvertDlg::OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your message handler code here and/or call default
if (lpDrawItemStruct->CtlID == IDC_BTN_READ)
{
//先求出按钮的状态
UINT uStyle = DFCS_BUTTONPUSH;
if (lpDrawItemStruct->itemState & ODS_SELECTED)
{
uStyle |= DFCS_PUSHED;
}
//以按钮的状态,画出默认的框框
::DrawFrameControl(lpDrawItemStruct->hDC,
&lpDrawItemStruct->rcItem,
DFC_BUTTON,
uStyle);
//求按钮的文字
CString strBtn;
GetDlgItemText(lpDrawItemStruct->CtlID,strBtn);
//设颜色
COLORREF crOld = ::SetTextColor(lpDrawItemStruct->hDC,
RGB(0,0,255)); //设蓝色
//画字
::DrawText(lpDrawItemStruct->hDC,strBtn,strBtn.GetLength(),
&lpDrawItemStruct->rcItem,DT_SINGLELINE|DT_VCENTER|DT_CENTER);
//设回原来的颜色
::SetTextColor(lpDrawItemStruct->hDC,crOld);
}
CDialog::OnDrawItem(nIDCtl, lpDrawItemStruct);
}