1. 在Create() 時的Style要加上 BS_OWNERDRAW
2. 在Class中加上 virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
3. 在cpp 檔中繪製元件 <--- 從網路上找的
void CTvButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
dc.Attach(lpDrawItemStruct->hDC); //Get device context object
CRect rt;
rt = lpDrawItemStruct->rcItem; //Get button rect
dc.FillSolidRect(rt, RGB(0, 0, 255)); //Fill button with blue color
UINT state = lpDrawItemStruct->itemState; //Get state of the button
if ( (state & ODS_SELECTED) ) // If it is pressed
{
dc.DrawEdge(rt,EDGE_SUNKEN,BF_RECT); // Draw a sunken face
}
else
{
dc.DrawEdge(rt,EDGE_RAISED,BF_RECT); // Draw a raised face
}
dc.SetTextColor(RGB(255,255,120));
// Set the color of the caption to be yellow
CString strTemp;
GetWindowText(strTemp);
// Get the caption which have been set
dc.DrawText(strTemp,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE);
// Draw out the caption
if ( (state & ODS_FOCUS ) ) // If the button is focused
{
// Draw a focus rect which indicates the user
// that the button is focused
int iChange = 3;
rt.top += iChange;
rt.left += iChange;
rt.right -= iChange;
rt.bottom -= iChange;
dc.DrawFocusRect(rt);
}
dc.Detach();
}