您好,登錄后才能下訂單哦!
構造一個矩形位圖,位圖內有一個帶有陰影和紫色間隙的小球,程序使用定時器來控制小球的動作,實際上是每當接收到定時器消息時將位圖通過BitBlt函數復制到客戶區,每當小球碰撞到客戶區上下左右四邊時就反彈回來。下面代碼有本人的理解注釋可供參考,本人才疏學淺,不妥請見諒。效果圖如下:
剛開始(初始其實是在客戶區中心開始的,難截圖就隨便找張了,但理解就OK了,不礙事):
移動后:
#include<windows.h>
#define ID_TIMER 1
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdline, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("Bounce");
HWND hWnd;//窗口句柄
MSG mSg;//消息結構體
//創建窗口類
WNDCLASSEX wndClass;
//設置窗口類各類屬性
wndClass.cbSize = sizeof(WNDCLASSEX);//設置窗口類結構體大小
wndClass.cbClsExtra = 0;//窗口類尾部的一部分額外的空間
wndClass.cbWndExtra = 0;
wndClass.hInstance = hInstance;//應用程序當前的實例句柄
wndClass.hCursor = LoadCursor(NULL, IDC_HELP);
wndClass.hIcon = NULL;
wndClass.hIconSm = NULL;
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpfnWndProc = WndProc;//回調函數的地址(窗口消息處理程序)
wndClass.lpszClassName = szAppName;//窗口類的名字,也就是窗口的標識,后面用于創建窗口函數的參數。
wndClass.lpszMenuName = NULL;//菜單的名字,沒有為NULL。
wndClass.style = CS_HREDRAW | CS_VREDRAW;//窗口類的樣式,它的值可以是窗口樣式值的任意組合。CS_HREDRAW CS_VREDRAW,這個是垂直刷新和水平刷新,窗口尺寸改變,重畫活動區域。
//注冊對話框類
if (!RegisterClassEx(&wndClass))
{
DWORD error_code = GetLastError();
MessageBox(NULL, TEXT("This program requires Windows NT!"), TEXT("NumRain"), MB_ICONERROR | MB_OKCANCEL);
return 0;
}
hWnd = CreateWindow(szAppName, TEXT("The Hello Program"), WS_OVERLAPPEDWINDOW, 200, 200, 800, 500, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&mSg, NULL, 0, 0))
{
TranslateMessage(&mSg);
DispatchMessage(&mSg);
}
return (int)mSg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc,hdcMem;
HBRUSH hBrush;
int iScale;
static HBITMAP hBitmap;
static int cxClient, cyClient,xCenter,yCenter,cxTotal,cyTotal,cxRadius,cyRadius,cxMove,cyMove,xPixel,yPixel;
switch (message)
{
case WM_CREATE:
//初始化數據
hdc = GetDC(hwnd);
xPixel = GetDeviceCaps(hdc, ASPECTX);//36
yPixel = GetDeviceCaps(hdc, ASPECTY);//36
ReleaseDC(hwnd, hdc);
SetTimer(hwnd, ID_TIMER, 50, NULL);
return 0;
case WM_SIZE:
//客戶區中心的坐標
xCenter = (cxClient = LOWORD(lParam)) / 2;//392
yCenter = (cyClient = HIWORD(lParam)) / 2;//230
iScale = min(cxClient*xPixel, cyClient*yPixel) / 16;//1037
//球水平的半徑和垂直的半徑
cxRadius = iScale / xPixel;//28
cyRadius = iScale / yPixel;//28
//球水平的半徑的一半和垂直的半徑的一半
cxMove = max(1, cxRadius / 2);//14
cyMove = max(1, cyRadius / 2);//14
//位圖寬和高
cxTotal = 2 * (cxRadius + cxMove);//84
cyTotal = 2 * (cyRadius + cyMove);//84
if (hBitmap)
{
DeleteObject(hBitmap);
}
hdc = GetDC(hwnd);
//創建內存設備環境
hdcMem = CreateCompatibleDC(hdc);
//創建與客戶區兼容的GDI位圖對象,該位圖寬為cxTotal,高為cyTotal
hBitmap = CreateCompatibleBitmap(hdc, cxTotal, cyTotal);
ReleaseDC(hwnd, hdc);
//將GDI位圖對象選入內存設備環境,擴大了顯示表面
SelectObject(hdcMem, hBitmap);
//矩形的邊框被繪制在位圖之外
Rectangle(hdcMem, -1, -1, cxTotal + 1, cyTotal + 1);
//創建陰影畫刷
hBrush = CreateHatchBrush(HS_DIAGCROSS, 0L);
SelectObject(hdcMem, hBrush);
//設置陰影畫刷間隔的空白處為紫色
SetBkColor(hdcMem, RGB(255, 0, 255));
/*
將小球繪制在位圖的中心
值得注意的是,陰影畫刷和陰影間隙的紫色效果都是在Rectangle函數之后,說明只有球才有陰影畫刷和紫色間隙,而球外面的邊空還是白色的。
*/
Ellipse(hdcMem, cxMove, cyMove, cxTotal - cxMove, cyTotal - cyMove);
DeleteDC(hdcMem);
DeleteObject(hBrush);
return 0;
case WM_TIMER:
if (!hBitmap)
{
break;
}
hdc = GetDC(hwnd);
//創建內存設備環境
hdcMem = CreateCompatibleDC(hdc);
//將繪制好的位圖對象選入內存設備環境
SelectObject(hdcMem, hBitmap);
//如果畫圖細心分析,會發現位圖中心,水平和垂直每移動cxMove個像素,都會有新的位圖覆蓋上一個位圖的球,這就不會出現球的殘影了
BitBlt(hdc, xCenter - cxTotal / 2, yCenter - cyTotal / 2, cxTotal, cyTotal, hdcMem, 0, 0, SRCCOPY);
ReleaseDC(hwnd, hdc);
DeleteDC(hdcMem);
xCenter += cxMove;
yCenter += cyMove;
//客戶區左右兩邊的碰撞檢測
if ((xCenter + cxRadius >= cxClient) || (xCenter - cxRadius <= 0))
{
//將小球反彈回去
cxMove = -cxMove;
}
if ((yCenter + cyRadius >= cyClient) || (yCenter - cyRadius <= 0))
{
//將小球反彈回去
cyMove = -cyMove;
}
return 0;
case WM_CLOSE:
if (IDOK == MessageBox(hwnd, TEXT("是否退出?"), TEXT("對話框"), MB_OKCANCEL | MB_DEFBUTTON1 | MB_ICONQUESTION))
{
DestroyWindow(hwnd);
}
else
{
return 0;
}
case WM_DESTROY:
if (hBitmap)
{
DeleteObject(hBitmap);
}
KillTimer(hwnd, ID_TIMER);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。