初始化位图

1
2
3
4
hMemDC = CreateCompatibleDC(hdc);
hBitmap = LoadBitmap(hInst, "mybmp");
SelectObject(hMemDC, hBitmap);
GetObject(hBitmap, sizeof(BITMAP), &bitmap);

绘制位图

1
BitBlt(hdc, bmx, bmy, bitmap.bmWidth, bitmap.bmHeight, hMemDC, 0, 0, SRCCOPY);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
RECT rect;
static HDC hMemDC;
static HBITMAP hBitmap;
static BITMAP bitmap;

switch(message)
{
case WM_CREATE : // 初始化
hdc = GetDC(hWnd);
hMemDC = CreateCompatibleDC(hdc);
hBitmap = LoadBitmap(hInst, "mybmp");
SelectObject(hMemDC, hBitmap);
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
ReleaseDC(hWnd, hdc);
break;
case WM_PAINT : // 重绘
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rect);
BitBlt(hdc, bmx, bmy, bitmap.bmWidth, bitmap.bmHeight, hMemDC, 0, 0, SRCCOPY);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY : // 析构
DeleteObject(hBitmap);
ReleaseDC(hWnd, hMemDC);
PostQuitMessage(0);
break;
}
}

两张/多张位图

1
2
3
4
5
hBitmap = (HBITMAP)LoadImage(NULL,"mybmp.bmp",IMAGE_BITMAP,800,800,LR_LOADFROMFILE);
hBitmap2 = (HBITMAP)LoadImage(NULL,"mybmp2.bmp",IMAGE_BITMAP,50,50,LR_LOADFROMFILE);
SelectObject(hMemDC, hBitmap);
GetObject(hBitmap, sizeof(BITMAP), &bitmap);
GetObject(hBitmap2, sizeof(BITMAP), &bitmap2);