先讲一下LoadBitmap的用法,代码如下:
PAINTSTRUCT ps; HDC hdc = BeginPaint(hwnd,&ps); HDC hMemDC = CreateCompatibleDC(hdc); HBITMAP hBitmap = LoadBitmap(g_hIntance,MAKEINTRESOURCE(IDB_BITMAP1)); HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC,hBitmap); //把HBITMAP转换成BITMAP,从而获取位图的宽高。 BITMAP bmp; GetObject(hBitmap, sizeof(BITMAP), &bmp); BitBlt(hdc,0,0,bmp.bmWidth,bmp.bmHeight,hMemDC,0,0,SRCCOPY); SelectObject(hMemDC,hOldBitmap); DeleteObject(hBitmap); DeleteDC(hMemDC); EndPaint(hwnd,&ps);
BitBlt的作用是把hMemDC上的内容拷贝到hdc上,不进行缩放,但是可以位移。
如果想要缩放,可以使用StretchBlt,以下这个例子比较有趣,是把整个屏幕绘制到窗口中。
PAINTSTRUCT ps; HDC hdcClient = BeginPaint(hwnd, &ps); HDC hdcScreen = CreateDC(TEXT("DISPLAY"), NULL, NULL, NULL); RECT rtClient; int scrWidth = GetSystemMetrics(SM_CXSCREEN); int scrHeight = GetSystemMetrics(SM_CYSCREEN); GetClientRect(hwnd,&rtClient); StretchBlt(hdcClient,0,0,rtClient.right,rtClient.bottom, hdcScreen,0,0,scrWidth,scrHeight,SRCCOPY); DeleteDC(hdcScreen); EndPaint(hwnd, &ps);