一、画笔步骤及相关代码
void 绘图(HWND hWnd) { PAINTSTRUCT p = {}; HDC hdc = BeginPaint(hWnd, &p); //1、创建笔 HPEN hPen = CreatePen(PS_SOLID, 10, RGB(255, 0, 0)); //2、将画笔应用到DC中 HGDIOBJ jdcp = SelectObject(hdc, hPen); //3、绘图 //画个点(hdc); //画线(hdc); //画矩形(hdc); 画扇形(hdc); //4、从dc中取出画笔 SelectObject(hdc, jdcp); //5、释放画笔 DeleteObject(hPen); EndPaint(hWnd, &p); }
二、画刷步骤及相关代码
//创建画刷 //HBRUSH hBrush = CreateSolidBrush(RGB(0, 255, 0));//实心 HBRUSH hBrush = CreateHatchBrush(HS_DIAGCROSS,RGB(0, 200, 0));//纹理 //HGDIOBJ hBrush = GetStockObject(NULL_BRUSH);创建透明画刷,背景图不会改变,不需要DeleteObject(hBrush); //将画刷应用到dc中 HGDIOBJ hob = SelectObject(hdc, hBrush); 画扇形(hdc); //从dc中取出画刷 SelectObject(hdc, hob); //释放画刷 DeleteObject(hBrush);
三、位图
1、资源里面添加位图
2、相关代码
void 绘图(HWND hWnd) { PAINTSTRUCT p = {}; HDC hdc = BeginPaint(hWnd, &p); //1、添加位图资源 //2、加载位图资源 HBITMAP hBmp = LoadBitmap(g_hInstance, (wchar_t*)IDB_BITMAP1); //3、创建虚拟区域接收上面的dc HDC hmdc = CreateCompatibleDC(hdc); //4、在虚拟区域绘画 HGDIOBJ hobmp= SelectObject(hmdc, hBmp); //5、从虚拟区copy到原dc //原dc,开始地址,要copy的开始地址,虚拟dc,要copy的范围,copy方式 BitBlt(hdc, 100, 100, 48, 48, hmdc, 0, 0, SRCCOPY); //原dc,开始地址,放大原图,虚拟dc,要copy开始地址,实际大小 StretchBlt(hdc, 200, 100, 96, 96, hmdc, 0, 0, 48, 48, NOTSRCCOPY); //6、从虚拟dc取出位图 SelectObject(hmdc, hobmp); //7、释放位图 DeleteObject(hBmp); //8、释放内存dc DeleteDC(hmdc); EndPaint(hWnd, &p); }
3、 GetObject方法,获取位图的相关信息
BITMAP bmpInfo = { 0 }; GetObject(hBmp, sizeof(bmpInfo), &bmpInfo); BitBlt(hdc, 100, 100, bmpInfo.bmWidth, bmpInfo.bmWidth, hmdc, 0, 0, SRCCOPY);