win32之取畫刷的方法

weixin_34198583發表於2015-04-01
取畫刷(HBRUSH) 的六種方法2009-07-23 15:00HBRUSH hbr;
第一種: hbr= CreateSolidBrush(RGB(255,0,0)); //單色的畫刷
第二種: hbr= (HBRUSH)GetStockObject(BLACK_BRUSH); //只能取特定顏色的畫刷,如BLACK_BRUSH,GRAY_BRUSH等刷
第三種: hbr= CreatePatternBrush(HBITMAP hbmp); //得到點陣圖畫刷
第四種: hbr = CreateHatchBrush(int fnStyle, COLORREF clrref) //建立一種帶陰影的畫刷
第五種: hbr= CreateBrushIndirect(LOGBRUSH); //通過LOGBRUSH結構體來取畫刷
typedef struct tagLOGBRUSH { 
UINT lbStyle; //畫刷型別
COLORREF lbColor; //顏色
LONG lbHatch; //陰影
} LOGBRUSH, *PLOGBRUSH;
第六種: hbr= HBRUSH CreateDIBPatternBrush( //通過與裝置無關點陣圖建立一個畫刷
HGLOBAL hglbDIBPacked, // handle to DIB
UINT fuColorSpec // color table data
);

例如:
HBRUSH CAfdView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

// TODO: Change any attributes of the DC here
if (pWnd->GetDlgCtrlID()==IDC_STATIC1)
{
pDC->SetTextColor(RGB(200,0,0));
pDC->SetBkColor(RGB(0,0,255));
static HBRUSH hBrush = CreateSolidBrush(RGB(222,0,255));
return hBrush;
}
// TODO: Return a different brush if the default is not desired
else 
return hbr;
}
改變對話方塊背景色
HBRUSH CDqfDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
if(nCtlColor == CTLCOLOR_DLG)
{
CBrush *brush;
brush = new CBrush(RGB(221,221,221));
return (HBRUSH)(brush->m_hObject);
}
return hbr;
}

第一種方法和第二種方法自己測試了可以使用,後面的還沒有測試。

相關文章