我们常常看到许多软件如QQ, 360, 金山词霸等都非常的漂亮,看不到我们通常看到的Window窗体和控件。 本节我们学习一下,当然技术上也是老话重谈,有点点技术就是双重缓冲的解决切换闪烁的问题.
一、关键代码
1. 载入一个背影到内存(确保窗体与背影大小一致)
CREATESTRUCT cs = *(CREATESTRUCT*)lParam;
hBmpBK = LoadBitmap(cs.hInstance, MAKEINTRESOURCE(IDB_BACKGROUND));
BITMAP bmp;
GetObject(hBmpBK, sizeof(BITMAP), (LPVOID)&bmp);
MoveWindow(hWnd, 0, 0, bmp.bmWidth, bmp.bmHeight, FALSE);
hBmpBK = LoadBitmap(cs.hInstance, MAKEINTRESOURCE(IDB_BACKGROUND));
BITMAP bmp;
GetObject(hBmpBK, sizeof(BITMAP), (LPVOID)&bmp);
MoveWindow(hWnd, 0, 0, bmp.bmWidth, bmp.bmHeight, FALSE);
2. 双缓冲绘图(一个作图缓冲, 作完图再绘到显示缓冲上)
case WM_PAINT:
{
RECT rtClient;
GetClientRect(hWnd, &rtClient);
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rtClient.right - rtClient.left, rtClient.bottom - rtClient.top);
SelectObject(hMemDC, hBitmap);
BitBlt(hDC, 0, 0, rtClient.right - rtClient.left, rtClient.bottom - rtClient.top, hMemDC, 0, 0, SRCCOPY);
DeleteObject(hMemDC);
DeleteObject(hBitmap);
EndPaint(hWnd, &ps);
return 0;
}
{
RECT rtClient;
GetClientRect(hWnd, &rtClient);
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, rtClient.right - rtClient.left, rtClient.bottom - rtClient.top);
SelectObject(hMemDC, hBitmap);
BitBlt(hDC, 0, 0, rtClient.right - rtClient.left, rtClient.bottom - rtClient.top, hMemDC, 0, 0, SRCCOPY);
DeleteObject(hMemDC);
DeleteObject(hBitmap);
EndPaint(hWnd, &ps);
return 0;
}
二、程序效果