1. 程式人生 > >MFC CStatic控制元件在DrawItem中自繪

MFC CStatic控制元件在DrawItem中自繪

1.實現PreSubclassWindow

void CStaticHref::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class


CStatic::PreSubclassWindow();


DWORD dwStyle = GetStyle();  
SetWindowLong(GetSafeHwnd(),GWL_STYLE,dwStyle | SS_OWNERDRAW);
//ModifyStyle(NULL, SS_OWNERDRAW);
}

2.在virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);中完成相關繪製

3.確保訊息對映中不能有:ON_WM_PAINT(也可以直接在OnPaint中繪製)、ON_WM_DRAWITEM 訊息,否則DrawItem函式不會被呼叫

注:第二步的是DrawItem而非OnDrawItem。OnDrawItem是ON_WM_DRAWITEM訊息的處理函式,是處理子控制元件傳送過來的自繪訊息的