使用BitBlt控制位圖移動

1、加載位圖到內存,經過BitBlt函數拷貝到DC上緩存

int dx;
int dy;
const int width = 252;
const int height = 304;
void Paint(HWND hwnd){
PAINTSTRUCT ps;
HDC hdc;
HDC mdc;
HBITMAP hbmp; //位圖繪製對象句柄
hdc = BeginPaint(hwnd,&ps); //當前窗口DC
mdc = CreateCompatibleDC(hdc); //建立緩存DC(當前窗口DC的兼容DC)
hbmp = (HBITMAP)LoadImage(NULL,TEXT(".\lxy.bmp"),IMAGE_BITMAP,width,height,LR_LOADFROMFILE); //加載位圖
SelectObject(mdc,hbmp); //緩存DC選擇位圖繪製對象
printf("dx=%d,dy=%d\n",dx,dy);
BitBlt(hdc,dx,dy,width,height,mdc,0,0,SRCCOPY); //拷貝緩存DC數據到窗口DC上
DeleteObject(hbmp);
EndPaint(hwnd,&ps);
}ide

2、添加按鍵消息處理
const int KEY_UP = 38;
const int KEY_DOWN = 40;
const int KEY_LEFT = 37;
const int KEY_RIGHT = 39;函數

void Invalid(HWND hwnd){
RECT rect;
GetClientRect(hwnd,&rect);
InvalidateRect(hwnd,&rect,true);
}code

case WM_KEYDOWN:{
int code = LOWORD(wParam);
//printf("code:%d\n",code);
switch(code){
case KEY_UP:{
dy -= 5;
Invalid(hwnd);
break;
}
case KEY_DOWN:{
dy += 5;
Invalid(hwnd);
break;
}
case KEY_LEFT:{
dx -= 5;
Invalid(hwnd);
break;
}
case KEY_RIGHT:{
dx += 15;
Invalid(hwnd);
break;
}
default:
break;
}
break;
} 對象

相關文章
相關標籤/搜索