CDC *pDC=GetDC(); pDC->DrawText("hello",CRect(0,0,100,100),DT_LEFT);b.创建内存绘图设备并贴在实际的设备之上:
CDC *pDC=GetDC(); CDC *pMemDC=new CDC; pMemDC->CreateCompatibleDC(pDC); pMemDC->DrawText("hello",CRect(0,0,100,100),DT_LEFT); StretchBlt(pDC->m_hDC,0,0,100,100,pMemDC->m_hDC,0,0,100,100,SRCCOPY); pMemDC->DeleteDC();//释放所以,如果想在内存DC中画文字然后贴到目标DC中,需要内存DC->SelectObject(&cbmp)
CDC *pDC=GetDC(); CDC memdc; memdc.CreateCompatibleDC(pDC); CRect rect; GetClientRect(&rect); CBitmap cbmp; cbmp.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()); memdc.SelectObject(&cbmp);//选择这个空图像 memdc.FillSolidRect(rect,RGB(255,255,255));//白色背景 memdc.DrawText("Hello",CRect(0,0,200,200),DT_LEFT);//在内存DC中输出Hello pDC->BitBlt(0,0,rect.Width(),rect.Height(),&memdc,0,0,SRCCOPY);//贴上 cbmp.DeleteObject(); memdc.DeleteDC(); ReleaseDC(pDC);
CDC *pDC=GetDC(); CFont font; font.CreatePointFont(500,"宋体");//创建字体 pDC->SelectObject(&font); pDC->DrawText("Hello",CRect(0,0,500,500),DT_LEFT);// font.DeleteObject();//需要释放
使用CPen设置线条颜色,类型,宽度
使用CBrush设置形状的填充颜色,花纹等
使用pDC->LineTo,pDC->Ellipse等画线,图形
CDC *pDC=GetDC(); CPen pen; pen.CreatePen(PS_SOLID,2,RGB(255,0,0)); pDC->SelectObject(&pen); CBrush brush; brush.CreateSolidBrush(RGB(255,0,0)); pDC->SelectObject(&brush); pDC->LineTo(100,100); pDC->Ellipse(CRect(0,0,100,100)); pen.DeleteObject();//需要释放 brush.DeleteObject();//需要释放
CDC *pDC=GetDC(); /* CRect r; GetClientRect(&r); pDC->FillRect(&r,NULL);//填充背景 */ CDC memdc; memdc.CreateCompatibleDC(pDC); CBitmap cbmp; cbmp.LoadBitmap(IDB_BITMAP1); memdc.SelectObject(&cbmp); BITMAP bmp; long width,height; cbmp.GetBitmap(&bmp); width = bmp.bmWidth; height = bmp.bmHeight; pDC->StretchBlt(0,0,(int)(width*1.5),(int)(height*1.5),&memdc,0,0, bmp.bmWidth,bmp.bmHeight,SRCCOPY);
COLORREF color; for (int i=0;i<iWidth;i++) { for (int j=0;j<iHeight;j++) { color=pDC->GetPixel(i,j); BYTE r=GetRValue(color); BYTE g=GetGValue(color); BYTE b=GetBValue(color); } } pDC->SetPixel(100,90,color);