//1.设置窗口大小 MoveWindow(hWnd, 300, 200, 700 + 6, 600 + 28, FALSE); //放在InitInstance用于设置位置与窗口大小 //传递句柄就可以绘图 //2.双缓冲绘图模式 void cDefense::DrawAll() { HDC hDc = GetDC(m_hWnd); HDC dcMem = CreateCompatibleDC(hDc); //创建与hDc兼容的位图 即一片内存 HBITMAP hBitmap = CreateCompatibleBitmap(hDc, 700, 600); //把这个位图与窗口dcMem绑定起来 即把这片内存与窗口dcMem绑定起来 对dcMem操作则为对这片内存的操作 SelectObject(dcMem, hBitmap); //把所有的图像绘制到dcMem上 DrawMap(dcMem); //把dcMem绘制到hDc上 Select后才能BitBlt BitBlt(hDc, 0, 0, 700, 600, dcMem, 0, 0, SRCCOPY); ReleaseDC(m_hWnd, hDc); DeleteObject(dcMem); DeleteObject(hBitmap); } void cDefense::DrawMap(HDC dcMem) { HDC dcTemp; dcTemp = CreateCompatibleDC(NULL); HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, L"images\\maps\\bg.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE); SelectObject(dcTemp, hBitmap); BITMAP bitInfo; GetObject(hBitmap, sizeof(BITMAP), &bitInfo); BitBlt(dcMem, 0, 0, bitInfo.bmWidth, bitInfo.bmHeight, dcTemp, 0, 0, SRCCOPY); DeleteObject(dcTemp); DeleteObject(hBitmap); }//总结:创建窗口 创建位图 选定位图 获取位图信息 绑定位图
时间: 2024-10-19 08:21:34