//常用设置CClientDC dc(this);CPen pen(PS_SOLID, 1, RGB(255,0,0));//选择画笔颜色CPen *pOldpen = dc.SelectObject(&pen);dc.SelectObject(CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH)));//选择画刷颜色为透明GetDlgItem(IDC_TEST11)->GetWindowRect(&rect);ScreenToClient(&rect); //屏幕坐标转换为客户区坐标pDC->SetBkMode(TRANSPARENT);//设置文字背景为透明
//demo1 (改变对话框、组框和编辑栏的背景和字体颜色)CWnd::OnCtlColor//会被多次调用afx_msg HBRUSH OnCtlColor( CDC* pDC, CWnd* pWnd, UINT nCtlColor );Return ValueOnCtlColor must return a handle to the brush that is to be used for painting the control background.//改变对话框、组框和编辑栏的背景和字体颜色//在对话框类中捕获WM_CTLCOLOR消息,并添加消息处理函数OnCtlColorHBRUSH CSetDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);// TODO: Change any attributes of the DC hereif(pWnd->GetDlgCtrlID() == IDC_TEST11){pDC->SetTextColor(RGB(220,0,0));pDC->SetBkMode(TRANSPARENT);return m_brush;}if(pWnd->GetDlgCtrlID() == IDC_EDIT1){pDC->SetTextColor(RGB(220,0,0));//设置文字颜色pDC->SetBkMode(TRANSPARENT);//设置文字背景颜色为透明return m_brush;//返回画刷颜色,m_brush为CBrush类型的对话框的成员变量}return hbr;// TODO: Return a different brush if the default is not desired}
//demo2 (改变按钮的字体颜色和背景)CButton::DrawItem virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );//改变按钮的字体颜色和背景//添加新的类CTestButton,继承自CButton//在CTestButton类中重写虚函数DrawButton()//在按钮的属性style页中勾选owner draw//将按钮通过类向导关联变量,类型为CTestButton void CTestButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {// TODO: Add your code to draw the specified item 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);}
//demo3 (在窗口中贴图)CDC::StretchBlt BOOL StretchBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, int nSrcWidth, int nSrcHeight, DWORD dwRop );//函数从源矩形中复制一个位图到目标矩形,必要时按目前目标设备设置的模式进行图像的拉伸或压缩。//捕获WM_ERASEBKGND或者WM_PAINT消息,并添加响应函数//WM_PAINT消息会有闪烁BOOL CGraphicView::OnEraseBkgnd(CDC* pDC) {// TODO: Add your message handler code here and/or call default//加载位图CBitmap bitmap;bitmap.LoadBitmap(IDB_BITMAP2);BITMAP bitmapinfo;bitmap.GetBitmap(&bitmapinfo);//创建兼容DCCDC dcCompatible;dcCompatible.CreateCompatibleDC(pDC);//与当前DC兼容 //将位图选进兼容DCdcCompatible.SelectObject(&bitmap);//将兼容DC的位图拷贝到目的DCCRect rect;GetClientRect(&rect);//pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,SRCCOPY);pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,bitmapinfo.bmWidth,bitmapinfo.bmHeight,SRCCOPY);return TRUE;//return CView::OnEraseBkgnd(pDC);}