C++中用hdc画bmp图片

//在这里是一个正方形,就用一个参数宽度
lgWidth =64;
PAINTSTRUCT ps;
HDC hdc,hMemDC;
HBITMAP hbmp1;
HLOCAL pBits;
hbmp1 = (HBITMAP)LoadImage(hInst, TEXT("D:\\pic\\logo.bmp"), IMAGE_BITMAP, lgWidth, lgWidth, LR_LOADFROMFILE);
...
case WM_PAINT:
	{
		hdc = BeginPaint(hWnd, &ps);
		hdc = GetDC(hWnd);//或者hdc=GetWindowDC(hwnd);两者有一点差别
		hMemDC = CreateCompatibleDC(hdc);
		//创建一个64*64的位图
		pBits = LocalAlloc(LMEM_FIXED, lgWidth * lgWidth * 4);
		SelectObject(hMemDC, hbmp1);
		BitBlt(hdc, herx = herx + 10, 0, lgWidth, lgWidth, hMemDC, 0, 0, SRCCOPY);
		//释放
		LocalFree(pBits);

		ReleaseDC(hWnd, hMemDC);
		//DeleteDC(hMemDC);
		EndPaint(hWnd, &ps);
		break;
	}

以上代码,添加在WndPrc主函数的CALLBACK的WM_PAINT中,在初始化窗口的时候就会在窗口呃逆对应位置生成一个图像:
C++中用hdc画bmp图片_第1张图片

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