Win32学习 (十二)位图

位图相关
光栅图形 - 记录图像中没一点的颜色等信息
矢量图形 - 记录图像算法、绘图指令等
HBITMAP - 位图句柄
位图的使用
1 在资源中添加位图资源
2 从资源中加载位图LoadBitmap
3 创建一个与当前DC 相匹配的DC(内存DC)
HDC CreateCompatibleDC(
HDC hdc //当前DC句柄,可以为NULL(使用屏幕DC)
);返回创建好的DC句柄

	4 将位图放入匹配的DC中 SelectObject
	5成像(1:1)
	BOOL BitBlt(
				HDC hdcDest,//目的DC
				int nXDest,//目的左上X坐标
				int nYDest,  //目的左上Y坐标
				int nWidth,//目的宽度
				int nHeight,//目的高度
				HDC hdcSrc,//源DC
				int nXSrc,//源左上X坐标
				int nYSrc,,//源左上Y坐标
				DWORD dwRop //成像方法 SRCCOPY
	);
	缩放成像
	BOOL StretchBlt(
				HDC hdcDest, //handle to destination DC
				int nXoriginDest,//x-coord of destination upper-left corner
				int nYOriginDest,//y-coord of destination upper-left corner
				int nWidthDest,//width of destination rectangle
				int nHeightDest,//height of destination rectangle
				HDC hdcSrc, //handle to  source DC
				int nXOriginSrc, //x-coord of source upper-left corner
				int nYOriginSrc,//y-coord of source upper-left corner
				int nWidthSrc, //源DC宽
				int nHeightSrc, //源DC高
				DWORD dwRop  //raster operation code
		);
		6.取出位图
		SelectObject
		7释放位图
		DeleteObject
		8释放匹配的DC
		DeleteDC
#include 
#include "resource.h"
HINSTANCE g_hInstance = 0;
void DrawPit(HDC hdc) {
	SetPixel(hdc, 100, 100, RGB(255, 0, 0));
}
void DrawLine(HDC hdc) {
	MoveToEx(hdc, 100, 100,NULL);//指明窗口当前点
	LineTo(hdc, 300, 300);//画直线
	LineTo(hdc, 300, 0);//画直线
}
void DrawRect(HDC hdc) {
	Rectangle(hdc, 100, 100, 300, 300);
}
void DrawEll(HDC hdc) {
	Ellipse(hdc, 100, 100, 300, 300);
}
void DrawBmp(HDC hdc) {
	//添加位图资源(不需要代码)
	HBITMAP hBmp = LoadBitmap(g_hInstance, (CHAR*)IDB_BITMAP1);
	HDC hMemdc = CreateCompatibleDC(hdc);
	//创建一个内存DC,并构建一个虚拟区域,并且内存DC在虚拟区域中绘图
	HGDIOBJ nOldBmp = SelectObject(hMemdc, hBmp);
	//将位图数据送给内存DC,内存DC在虚拟区域中将位图绘制出来。
	BitBlt(hdc, 100, 100, 48, 48, hMemdc, 0, 0, SRCCOPY);
	//将虚拟区域中绘制好的图形成像到窗口
	StretchBlt(hdc, 200, 200, 24, 24, hMemdc, 0, 0, 48, 48, SRCCOPY);
	//缩放成像 
	SelectObject(hMemdc, nOldBmp);
	DeleteObject(hBmp);
	DeleteDC(hMemdc);


}



void OnPaint(HWND hWnd) {
	PAINTSTRUCT ps = { 0 };
	HDC hdc = BeginPaint(hWnd, &ps);

	HPEN hPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0)); //创建画笔 PS__DASH虚像画笔
	HGDIOBJ nOldPen = SelectObject(hdc, hPen);//将画笔应用到DC中

	//HBRUSH hBrush = CreateSolidBrush(RGB(0, 255, 0));
	//HBRUSH hBrush = CreateHatchBrush(HS_CROSS, RGB(0, 255, 0));
	HGDIOBJ hBursh = GetStockObject(NULL_BRUSH);
	HGDIOBJ nOldBrush = SelectObject(hdc, hBursh);

	//DrawPit( hdc );
	//DrawLine( hdc );
	//DrawRect(hdc);
	//DrawEll(hdc);
	DrawBmp(hdc);

	SelectObject(hdc, nOldBrush);
	//DeleteObject(hBrush);


	SelectObject(hdc, nOldPen);//收回画笔
	DeleteObject(hPen);//销毁画笔


	EndPaint(hWnd, &ps);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM IParam)
{
	switch (msgID) {
	case WM_PAINT:
		OnPaint(hWnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}
	return DefWindowProc(hWnd, msgID, wParam, IParam);
}

int CALLBACK WinMain(HINSTANCE hIns, HINSTANCE hPreIns, LPSTR lpCmdLine, int nCmdShow) {
	g_hInstance = hIns;
	WNDCLASS wc = { 0 };
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 3);
	wc.hCursor = NULL;
	wc.hIcon = NULL;
	wc.hInstance = hIns;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "Main";
	wc.lpszMenuName = NULL;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wc); //将以上所有赋值全部写入操作系统中
	//在内存创建窗口
	HWND hWnd = CreateWindowEx(0, "Main", "window", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, NULL
		, hIns
		, NULL);

	//显示窗口
	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);
	//消息循环
	MSG nMsg = { 0 };
	while (GetMessage(&nMsg, NULL, 0, 0)) { //抓消息
		TranslateMessage(&nMsg);//翻译消息
		DispatchMessage(&nMsg);//派发消息:将消息交给窗口处理函数来处理。
	}

	return 0;
}



你可能感兴趣的:(学习,c++)