在贪吃蛇的代码中,我们在定时器中将pa函数注释掉,可以解决其不动的问题,我们用此背景可以进行画图,我们首先可以画一条直线,此时我们不能用HDC去画,应该创建一个临时的HDC和位图,进行画,之后将临时的加载到原HDC中,因为如果不创建临时的,在画完起点后就被release掉了,最后结果就是终点和(0,0)的直线(默认0,0)
1 #include<Windows.h> 2 #include "resource.h" 3 #include <time.h> 4 typedef char boolean; 5 #define TRUE 1 6 #define FALSE 0 7 8 9 10 11 LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM); 12 13 typedef struct E{ 14 int h,l; 15 struct E *pnext; 16 struct E *plast; 17 }Apple,Snake,node; 18 19 Apple apple={5,6}; 20 Snake *top=NULL; 21 Snake *end=NULL; 22 23 24 25 26 27 HBITMAP h_back; 28 HBITMAP h_apple; 29 HBITMAP h_body; 30 HBITMAP h_l; 31 HBITMAP h_r; 32 HBITMAP h_u; 33 HBITMAP h_d; 34 int g_fx=1; 35 void add(int h,int l); 36 void initSnake(); 37 void show(HDC hdc); 38 void showApple(HDC hdc); 39 void showSnake(HDC hdc); 40 41 void pa(); 42 boolean iseat(); 43 void Newapple(); 44 boolean isziyao(); 45 boolean iszhuang(); 46 47 48 49 50 51 52 int WINAPI WinMain(HINSTANCE hlnstance,HINSTANCE hPrelnstance, 53 LPSTR lpCmdLine,int nCmdShow){ //callback 54 55 56 //设计 57 HWND hwnd; 58 MSG msg; 59 LPCSTR classname="牟柏旭"; 60 WNDCLASSEX ex; 61 62 ex.style=CS_HREDRAW | CS_VREDRAW; 63 ex.cbSize=sizeof(WNDCLASSEX); 64 ex.cbClsExtra=0; 65 ex.cbWndExtra=0; 66 ex.hbrBackground=CreateSolidBrush(RGB(171,171,226)); 67 ex.hCursor=NULL; 68 ex.hIcon=NULL; 69 ex.hIconSm=NULL; 70 ex.hInstance=hlnstance; 71 ex.lpszMenuName=0; 72 ex.lpszClassName=classname; 73 ex.lpfnWndProc=MyWndProc; //消息过程函数 消息回调函数 74 75 //注册 76 RegisterClassEx(&ex); 77 //创建 78 hwnd= CreateWindow(classname,"mubaixu",WS_OVERLAPPEDWINDOW,50,50,620,630,NULL,NULL, 79 hlnstance,NULL); 80 SetTimer (hwnd, 1, 500, NULL) ; 81 82 //最后一个为是否创建多文档,比如code可以打开多个程序 83 84 85 ShowWindow(hwnd,nCmdShow); 86 87 88 89 h_back=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP1)); 90 h_apple=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP2)); 91 h_body=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP3)); 92 h_l=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP6)); 93 h_r=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP4)); 94 h_u=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP5)); 95 h_d=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP7)); 96 initSnake(); 97 srand((unsigned int )time(0)); 98 while(GetMessage(&msg,NULL,0,0)){ 99 100 TranslateMessage(&msg); 101 102 DispatchMessage(&msg); 103 //调用MyWndProc,系统调用 104 105 //指定接收哪个窗口的消息,如果为NULL用于接收所有窗口 106 // 后两个为过滤 107 } 108 109 110 111 return 0; 112 } 113 114 int xx=200; 115 HDC tmp_dc; 116 HBITMAP tmp_bitmap; 117 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){ 118 int x,y; 119 120 121 // char tx[10]; 122 //char ty[10]; 123 HDC hdc; 124 //temp_hdc; 125 //HBRUSH newbrush; 126 //HBRUSH oldbrush; 127 PAINTSTRUCT ps; 128 129 // HBITMAP hbitmap ; 130 131 switch (Msg) 132 { 133 134 case WM_KEYDOWN: 135 switch (wParam) 136 { 137 case VK_UP: 138 g_fx=1; 139 break; 140 case VK_DOWN: 141 g_fx=3; 142 break; 143 case VK_RIGHT: 144 g_fx=0; 145 break; 146 case VK_LEFT: 147 g_fx=2; 148 break; 149 150 default: 151 break; 152 } 153 break; 154 155 /*case WM_MOUSEMOVE: 156 hdc = GetDC (hWnd) ; 157 158 x = LOWORD (lParam) ; 159 y = HIWORD (lParam) ; 160 161 LineTo (tmp_dc, x, y) ; 162 BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 163 ReleaseDC (hWnd, hdc) ; 164 165 break;*/ 166 case WM_LBUTTONDOWN: 167 168 169 hdc = GetDC (hWnd) ; 170 171 172 // Rectangle(hdc, 150,150,200,200); 173 174 //创建临时的hdc和位图,并且把原图传过去 175 tmp_dc=CreateCompatibleDC(hdc); 176 tmp_bitmap=CreateCompatibleBitmap(hdc,620,630); 177 SelectObject(tmp_dc,tmp_bitmap); 178 BitBlt(tmp_dc,0,0,620,630,hdc,0,0,SRCCOPY); 179 180 x = LOWORD (lParam) ; 181 y = HIWORD (lParam) ; 182 //MessageBox (hWnd,itoa(x,tx,10),itoa(y,ty,10),MB_OKCANCEL);*/ 183 184 MoveToEx (tmp_dc, x, y, NULL) ; 185 186 187 ReleaseDC (hWnd, hdc) ; 188 break; 189 190 case WM_LBUTTONUP: 191 hdc = GetDC (hWnd) ; 192 193 x = LOWORD (lParam) ; 194 y = HIWORD (lParam) ; 195 196 LineTo (tmp_dc, x, y) ; 197 BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 198 199 DeleteObject(tmp_dc); 200 DeleteObject(tmp_bitmap); 201 ReleaseDC (hWnd, hdc) ; 202 203 break; 204 case WM_CLOSE: 205 PostQuitMessage(0); 206 207 break; 208 209 case WM_PAINT: 210 hdc = BeginPaint (hWnd, &ps) ; 211 212 show(hdc); 213 214 //Rectangle(hdc, 50,50,100,100); 215 //hbitmap = CreateCompatibleBitmap (hdc, 100, 200) ; 216 //temp_hdc = CreateCompatibleDC (hdc) ; 217 //SelectObject(temp_hdc,h_back); 218 ////Rectangle(temp_hdc, 10,10,50,50); 219 220 //BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ; 221 222 //xx+=20; 223 //Ellipse( hdc, xx, 300, xx+70, 340); 224 225 EndPaint (hWnd, &ps) ; 226 break; 227 228 case WM_TIMER: 229 230 231 232 //pa(); 233 if(iseat()){ 234 Newapple(); 235 add(-20,-20); 236 } 237 if(isziyao()||iszhuang()){ 238 PostQuitMessage(0); 239 } 240 241 hdc = GetDC (hWnd) ; 242 //show(hdc);重绘 243 /*newbrush= CreateSolidBrush(RGB(171,171,226));//椭圆更新移动 244 oldbrush=(HBRUSH)SelectObject(hdc,newbrush); 245 Rectangle( hdc, 0, 0,600,600); 246 247 DeleteObject(newbrush); 248 SelectObject(hdc,oldbrush); 249 xx+=20; 250 Ellipse( hdc, xx, 300, xx+70, 340); 251 Rectangle(hdc, 50,50,100,100);*/ 252 ReleaseDC (hWnd, hdc) ; 253 break; 254 default: 255 break; 256 } 257 //系统默认的消息处理函数 258 return DefWindowProc(hWnd,Msg,wParam,lParam); 259 } 260 261 262 263 void add(int h,int l){ 264 node * pnode=(node *)malloc(sizeof(node)); 265 pnode->h=h; 266 pnode->l=l; 267 pnode->plast=NULL; 268 pnode->pnext=NULL; 269 if(top==NULL){ 270 top=pnode; 271 } 272 else{ 273 pnode->plast=end; 274 end->pnext=pnode; 275 } 276 277 end=pnode; 278 } 279 280 281 void initSnake(){ 282 int i; 283 for( i=8;i<=10;i++){ 284 add(i,10); 285 } 286 287 } 288 289 void show(HDC hdc){ 290 HDC temp_hdc; 291 temp_hdc = CreateCompatibleDC (hdc) ; 292 SelectObject(temp_hdc,h_back); 293 294 BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ; 295 DeleteObject(temp_hdc); 296 showApple( hdc); 297 showSnake( hdc); 298 } 299 void showApple(HDC hdc){ 300 HDC temp_hdc; 301 temp_hdc = CreateCompatibleDC (hdc) ; 302 SelectObject(temp_hdc,h_apple); 303 304 BitBlt (hdc, apple.l*30+30,apple.h*30+30, 30, 30, temp_hdc, 0, 0, SRCCOPY) ; 305 DeleteObject(temp_hdc); 306 } 307 void showSnake(HDC hdc){ 308 HDC temp_hdc; 309 Snake *ptop=top; 310 temp_hdc = CreateCompatibleDC (hdc) ; 311 switch (g_fx) 312 { 313 314 case 0: 315 SelectObject(temp_hdc,h_r); 316 break; 317 case 1: 318 SelectObject(temp_hdc,h_u); 319 break; 320 case 2: 321 SelectObject(temp_hdc,h_l); 322 break; 323 case 3: 324 SelectObject(temp_hdc,h_d); 325 break; 326 default: 327 break; 328 } 329 330 331 BitBlt (hdc, top->l*30+30,top->h*30+30 , 30, 30, temp_hdc, 0, 0, SRCCOPY) ; 332 DeleteObject(temp_hdc); 333 334 335 temp_hdc = CreateCompatibleDC (hdc) ; 336 SelectObject(temp_hdc,h_body); 337 ptop=ptop->pnext; 338 while(ptop){ 339 340 BitBlt (hdc, ptop->l*30+30,ptop->h*30+30 , 30, 30, temp_hdc, 0, 0, SRCCOPY) ; 341 ptop=ptop->pnext; 342 } 343 DeleteObject(temp_hdc); 344 345 } 346 347 void pa(){ 348 Snake *jend=end; 349 while(jend->plast!=NULL) 350 { 351 jend->h=jend->plast->h; 352 jend->l=jend->plast->l; 353 354 jend=jend->plast; 355 } 356 switch(g_fx) 357 { 358 case 0: 359 top->l++; 360 break; 361 case 1: 362 top->h--; 363 break; 364 case 2: 365 top->l--; 366 break; 367 case 3: 368 top->h++; 369 break; 370 371 } 372 } 373 boolean iseat(){ 374 if(top->h==apple.h &&top->l==apple.l ) 375 return TRUE; 376 return FALSE; 377 } 378 void Newapple(){ 379 apple.l=rand()%18; 380 apple.h=rand()%18; 381 } 382 boolean isziyao(){ 383 Snake *pbody=top->pnext; 384 while(pbody!=NULL){ 385 if(top->h==pbody->h &&top->l==pbody->l ) 386 return TRUE; 387 pbody=pbody->pnext; 388 } 389 return FALSE; 390 } 391 boolean iszhuang(){ 392 if(top->h>17||top->l<0||top->l>17||top->h<0) 393 { 394 return TRUE; 395 } 396 return FALSE; 397 }
此时如果加上WM_MOUSEMOVE 就会变成了一纸笔
1 case WM_MOUSEMOVE: 2 hdc = GetDC (hWnd) ; 3 4 x = LOWORD (lParam) ; 5 y = HIWORD (lParam) ; 6 7 LineTo (tmp_dc, x, y) ; 8 BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 9 10 ReleaseDC (hWnd, hdc) ; 11 12 break;
在第一张图中,我们可以多次画很多条线,我们怎么才能在图面上显示一条线?
我们可以再次创建一个src_hdc和map,来存储原来的图片,每次画新的线的时候,我们都将其src给hdc,在通过hdc给tmp_hcd,这样就能保证每次画新的线的时候旧的线就没了
1 case WM_LBUTTONDOWN: 2 3 4 hdc = GetDC (hWnd) ; 5 x = LOWORD (lParam) ; 6 y = HIWORD (lParam) ; 7 if(type==0){ 8 begin_x=x; 9 begin_y=y; 10 src_dc=CreateCompatibleDC(hdc); 11 src_bitmap=CreateCompatibleBitmap(hdc,620,630); 12 SelectObject(src_dc,src_bitmap); 13 BitBlt(src_dc,0,0,620,630,hdc,0,0,SRCCOPY); 14 type=1; 15 } 16 17 18 // Rectangle(hdc, 150,150,200,200); 19 20 if(1==type){ 21 //创建临时的hdc和位图,并且把原图传过去 22 BitBlt(hdc,0,0,620,630,src_dc,0,0,SRCCOPY);//把原hdc弄干净 23 tmp_dc=CreateCompatibleDC(hdc); 24 tmp_bitmap=CreateCompatibleBitmap(hdc,620,630); 25 SelectObject(tmp_dc,tmp_bitmap); 26 BitBlt(tmp_dc,0,0,620,630,hdc,0,0,SRCCOPY); 27 28 29 //MessageBox (hWnd,itoa(x,tx,10),itoa(y,ty,10),MB_OKCANCEL);*/ 30 31 MoveToEx (tmp_dc, x, y, NULL) ; 32 33 } 34 ReleaseDC (hWnd, hdc) ; 35 break; 36 37 case WM_LBUTTONUP: 38 hdc = GetDC (hWnd) ; 39 40 x = LOWORD (lParam) ; 41 y = HIWORD (lParam) ; 42 43 44 LineTo (tmp_dc, x, y) ; 45 BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 46 47 48 49 DeleteObject(tmp_dc); 50 DeleteObject(tmp_bitmap); 51 ReleaseDC (hWnd, hdc) ; 52 53 break;
加上下面的每次只能画一条随意弯曲的线
1 //case WM_MOUSEMOVE: 2 // hdc = GetDC (hWnd) ; 3 4 // x = LOWORD (lParam) ; 5 // y = HIWORD (lParam) ; 6 7 // LineTo (tmp_dc, x, y) ; 8 // BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 9 10 // ReleaseDC (hWnd, hdc) ; 11 12 // break;
我们可以设置begin_x,begin_y来标定我们起始点,之后我们在moucemove里面画4根线
就画出了一个矩形
case WM_MOUSEMOVE: hdc = GetDC (hWnd) ; x = LOWORD (lParam) ; y = HIWORD (lParam) ; BitBlt(tmp_dc,0,0,620,630,src_dc,0,0,SRCCOPY); //1根线 MoveToEx (tmp_dc, begin_x, begin_y, NULL) ; LineTo (tmp_dc,begin_x, y) ; //2根 MoveToEx (tmp_dc, begin_x, begin_y, NULL) ; LineTo (tmp_dc, x, begin_y) ; //3根 MoveToEx (tmp_dc, x, begin_y, NULL) ; LineTo (tmp_dc, x, y) ; //4根 MoveToEx (tmp_dc, begin_x,y, NULL) ; LineTo (tmp_dc, x, y) ; BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); ReleaseDC (hWnd, hdc) ; break;
下面就是画线不重复的代码
1 #include<Windows.h> 2 #include "resource.h" 3 #include <time.h> 4 typedef char boolean; 5 #define TRUE 1 6 #define FALSE 0 7 8 9 10 11 LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM); 12 13 typedef struct E{ 14 int h,l; 15 struct E *pnext; 16 struct E *plast; 17 }Apple,Snake,node; 18 19 Apple apple={5,6}; 20 Snake *top=NULL; 21 Snake *end=NULL; 22 23 24 25 26 27 HBITMAP h_back; 28 HBITMAP h_apple; 29 HBITMAP h_body; 30 HBITMAP h_l; 31 HBITMAP h_r; 32 HBITMAP h_u; 33 HBITMAP h_d; 34 int g_fx=1; 35 void add(int h,int l); 36 void initSnake(); 37 void show(HDC hdc); 38 void showApple(HDC hdc); 39 void showSnake(HDC hdc); 40 41 void pa(); 42 boolean iseat(); 43 void Newapple(); 44 boolean isziyao(); 45 boolean iszhuang(); 46 47 48 49 50 51 52 int WINAPI WinMain(HINSTANCE hlnstance,HINSTANCE hPrelnstance, 53 LPSTR lpCmdLine,int nCmdShow){ //callback 54 55 56 //设计 57 HWND hwnd; 58 MSG msg; 59 LPCSTR classname="牟柏旭"; 60 WNDCLASSEX ex; 61 62 ex.style=CS_HREDRAW | CS_VREDRAW; 63 ex.cbSize=sizeof(WNDCLASSEX); 64 ex.cbClsExtra=0; 65 ex.cbWndExtra=0; 66 ex.hbrBackground=CreateSolidBrush(RGB(171,171,226)); 67 ex.hCursor=NULL; 68 ex.hIcon=NULL; 69 ex.hIconSm=NULL; 70 ex.hInstance=hlnstance; 71 ex.lpszMenuName=0; 72 ex.lpszClassName=classname; 73 ex.lpfnWndProc=MyWndProc; //消息过程函数 消息回调函数 74 75 //注册 76 RegisterClassEx(&ex); 77 //创建 78 hwnd= CreateWindow(classname,"mubaixu",WS_OVERLAPPEDWINDOW,50,50,620,630,NULL,NULL, 79 hlnstance,NULL); 80 SetTimer (hwnd, 1, 500, NULL) ; 81 82 //最后一个为是否创建多文档,比如code可以打开多个程序 83 84 85 ShowWindow(hwnd,nCmdShow); 86 87 88 89 h_back=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP1)); 90 h_apple=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP2)); 91 h_body=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP3)); 92 h_l=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP6)); 93 h_r=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP4)); 94 h_u=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP5)); 95 h_d=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP7)); 96 initSnake(); 97 srand((unsigned int )time(0)); 98 while(GetMessage(&msg,NULL,0,0)){ 99 100 TranslateMessage(&msg); 101 102 DispatchMessage(&msg); 103 //调用MyWndProc,系统调用 104 105 //指定接收哪个窗口的消息,如果为NULL用于接收所有窗口 106 // 后两个为过滤 107 } 108 109 110 111 return 0; 112 } 113 114 int xx=200; 115 HDC tmp_dc; 116 HBITMAP tmp_bitmap; 117 HDC src_dc; 118 HBITMAP src_bitmap; 119 int type=0; 120 int begin_x=0; 121 int begin_y=0; 122 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){ 123 int x,y; 124 125 126 // char tx[10]; 127 //char ty[10]; 128 HDC hdc; 129 //temp_hdc; 130 //HBRUSH newbrush; 131 //HBRUSH oldbrush; 132 PAINTSTRUCT ps; 133 134 // HBITMAP hbitmap ; 135 136 switch (Msg) 137 { 138 139 case WM_KEYDOWN: 140 switch (wParam) 141 { 142 case VK_UP: 143 g_fx=1; 144 break; 145 case VK_DOWN: 146 g_fx=3; 147 break; 148 case VK_RIGHT: 149 g_fx=0; 150 break; 151 case VK_LEFT: 152 g_fx=2; 153 break; 154 155 default: 156 break; 157 } 158 break; 159 160 //case WM_MOUSEMOVE: 161 // hdc = GetDC (hWnd) ; 162 163 // x = LOWORD (lParam) ; 164 // y = HIWORD (lParam) ; 165 166 // if(type==1){ 167 168 // BitBlt(tmp_dc,0,0,620,630,src_dc,0,0,SRCCOPY); 169 // //1根线 170 // MoveToEx (tmp_dc, begin_x, begin_y, NULL) ; 171 // LineTo (tmp_dc,begin_x, y) ; 172 173 // //2根 174 // MoveToEx (tmp_dc, begin_x, begin_y, NULL) ; 175 // LineTo (tmp_dc, x, begin_y) ; 176 // //3根 177 // MoveToEx (tmp_dc, x, begin_y, NULL) ; 178 // LineTo (tmp_dc, x, y) ; 179 // //4根 180 // MoveToEx (tmp_dc, begin_x,y, NULL) ; 181 // LineTo (tmp_dc, x, y) ; 182 183 184 // } 185 // BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 186 187 // ReleaseDC (hWnd, hdc) ; 188 189 // break; 190 case WM_LBUTTONDOWN: 191 192 193 hdc = GetDC (hWnd) ; 194 x = LOWORD (lParam) ; 195 y = HIWORD (lParam) ; 196 if(type==0){ 197 begin_x=x; 198 begin_y=y; 199 src_dc=CreateCompatibleDC(hdc); 200 src_bitmap=CreateCompatibleBitmap(hdc,620,630); 201 SelectObject(src_dc,src_bitmap); 202 BitBlt(src_dc,0,0,620,630,hdc,0,0,SRCCOPY); 203 204 type=1; 205 } 206 207 208 // Rectangle(hdc, 150,150,200,200); 209 210 if(1==type){ 211 //创建临时的hdc和位图,并且把原图传过去 212 BitBlt(hdc,0,0,620,630,src_dc,0,0,SRCCOPY);//把原hdc弄干净 213 tmp_dc=CreateCompatibleDC(hdc); 214 tmp_bitmap=CreateCompatibleBitmap(hdc,620,630); 215 SelectObject(tmp_dc,tmp_bitmap); 216 BitBlt(tmp_dc,0,0,620,630,hdc,0,0,SRCCOPY); 217 218 219 //MessageBox (hWnd,itoa(x,tx,10),itoa(y,ty,10),MB_OKCANCEL);*/ 220 221 MoveToEx (tmp_dc, x, y, NULL) ; 222 223 } 224 ReleaseDC (hWnd, hdc) ; 225 break; 226 227 case WM_LBUTTONUP: 228 hdc = GetDC (hWnd) ; 229 230 x = LOWORD (lParam) ; 231 y = HIWORD (lParam) ; 232 233 if(type==1){ 234 //画线结束 235 LineTo (tmp_dc, x, y) ; 236 BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 237 DeleteObject(tmp_dc); 238 DeleteObject(tmp_bitmap); 239 //抠图 240 } 241 242 ReleaseDC (hWnd, hdc) ; 243 244 break; 245 case WM_CLOSE: 246 PostQuitMessage(0); 247 248 break; 249 250 case WM_PAINT: 251 hdc = BeginPaint (hWnd, &ps) ; 252 253 show(hdc); 254 255 //Rectangle(hdc, 50,50,100,100); 256 //hbitmap = CreateCompatibleBitmap (hdc, 100, 200) ; 257 //temp_hdc = CreateCompatibleDC (hdc) ; 258 //SelectObject(temp_hdc,h_back); 259 ////Rectangle(temp_hdc, 10,10,50,50); 260 261 //BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ; 262 263 //xx+=20; 264 //Ellipse( hdc, xx, 300, xx+70, 340); 265 266 EndPaint (hWnd, &ps) ; 267 break; 268 269 case WM_TIMER: 270 271 272 273 //pa(); 274 if(iseat()){ 275 Newapple(); 276 add(-20,-20); 277 } 278 if(isziyao()||iszhuang()){ 279 PostQuitMessage(0); 280 } 281 282 hdc = GetDC (hWnd) ; 283 //show(hdc);重绘 284 /*newbrush= CreateSolidBrush(RGB(171,171,226));//椭圆更新移动 285 oldbrush=(HBRUSH)SelectObject(hdc,newbrush); 286 Rectangle( hdc, 0, 0,600,600); 287 288 DeleteObject(newbrush); 289 SelectObject(hdc,oldbrush); 290 xx+=20; 291 Ellipse( hdc, xx, 300, xx+70, 340); 292 Rectangle(hdc, 50,50,100,100);*/ 293 ReleaseDC (hWnd, hdc) ; 294 break; 295 default: 296 break; 297 } 298 //系统默认的消息处理函数 299 return DefWindowProc(hWnd,Msg,wParam,lParam); 300 } 301 302 303 304 void add(int h,int l){ 305 node * pnode=(node *)malloc(sizeof(node)); 306 pnode->h=h; 307 pnode->l=l; 308 pnode->plast=NULL; 309 pnode->pnext=NULL; 310 if(top==NULL){ 311 top=pnode; 312 } 313 else{ 314 pnode->plast=end; 315 end->pnext=pnode; 316 } 317 318 end=pnode; 319 } 320 321 322 void initSnake(){ 323 int i; 324 for( i=8;i<=10;i++){ 325 add(i,10); 326 } 327 328 } 329 330 void show(HDC hdc){ 331 HDC temp_hdc; 332 temp_hdc = CreateCompatibleDC (hdc) ; 333 SelectObject(temp_hdc,h_back); 334 335 BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ; 336 DeleteObject(temp_hdc); 337 showApple( hdc); 338 showSnake( hdc); 339 } 340 void showApple(HDC hdc){ 341 HDC temp_hdc; 342 temp_hdc = CreateCompatibleDC (hdc) ; 343 SelectObject(temp_hdc,h_apple); 344 345 BitBlt (hdc, apple.l*30+30,apple.h*30+30, 30, 30, temp_hdc, 0, 0, SRCCOPY) ; 346 DeleteObject(temp_hdc); 347 } 348 void showSnake(HDC hdc){ 349 HDC temp_hdc; 350 Snake *ptop=top; 351 temp_hdc = CreateCompatibleDC (hdc) ; 352 switch (g_fx) 353 { 354 355 case 0: 356 SelectObject(temp_hdc,h_r); 357 break; 358 case 1: 359 SelectObject(temp_hdc,h_u); 360 break; 361 case 2: 362 SelectObject(temp_hdc,h_l); 363 break; 364 case 3: 365 SelectObject(temp_hdc,h_d); 366 break; 367 default: 368 break; 369 } 370 371 372 BitBlt (hdc, top->l*30+30,top->h*30+30 , 30, 30, temp_hdc, 0, 0, SRCCOPY) ; 373 DeleteObject(temp_hdc); 374 375 376 temp_hdc = CreateCompatibleDC (hdc) ; 377 SelectObject(temp_hdc,h_body); 378 ptop=ptop->pnext; 379 while(ptop){ 380 381 BitBlt (hdc, ptop->l*30+30,ptop->h*30+30 , 30, 30, temp_hdc, 0, 0, SRCCOPY) ; 382 ptop=ptop->pnext; 383 } 384 DeleteObject(temp_hdc); 385 386 } 387 388 void pa(){ 389 Snake *jend=end; 390 while(jend->plast!=NULL) 391 { 392 jend->h=jend->plast->h; 393 jend->l=jend->plast->l; 394 395 jend=jend->plast; 396 } 397 switch(g_fx) 398 { 399 case 0: 400 top->l++; 401 break; 402 case 1: 403 top->h--; 404 break; 405 case 2: 406 top->l--; 407 break; 408 case 3: 409 top->h++; 410 break; 411 412 } 413 } 414 boolean iseat(){ 415 if(top->h==apple.h &&top->l==apple.l ) 416 return TRUE; 417 return FALSE; 418 } 419 void Newapple(){ 420 apple.l=rand()%18; 421 apple.h=rand()%18; 422 } 423 boolean isziyao(){ 424 Snake *pbody=top->pnext; 425 while(pbody!=NULL){ 426 if(top->h==pbody->h &&top->l==pbody->l ) 427 return TRUE; 428 pbody=pbody->pnext; 429 } 430 return FALSE; 431 } 432 boolean iszhuang(){ 433 if(top->h>17||top->l<0||top->l>17||top->h<0) 434 { 435 return TRUE; 436 } 437 return FALSE; 438 }
上面我们画了矩形,我们需要把这个矩形扣下来,放在t_dc里面,之后将这个矩形变成白色
1 #include<Windows.h> 2 #include "resource.h" 3 #include <time.h> 4 typedef char boolean; 5 #define TRUE 1 6 #define FALSE 0 7 8 9 10 11 LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM); 12 13 typedef struct E{ 14 int h,l; 15 struct E *pnext; 16 struct E *plast; 17 }Apple,Snake,node; 18 19 Apple apple={5,6}; 20 Snake *top=NULL; 21 Snake *end=NULL; 22 23 24 25 26 27 HBITMAP h_back; 28 HBITMAP h_apple; 29 HBITMAP h_body; 30 HBITMAP h_l; 31 HBITMAP h_r; 32 HBITMAP h_u; 33 HBITMAP h_d; 34 int g_fx=1; 35 void add(int h,int l); 36 void initSnake(); 37 void show(HDC hdc); 38 void showApple(HDC hdc); 39 void showSnake(HDC hdc); 40 41 void pa(); 42 boolean iseat(); 43 void Newapple(); 44 boolean isziyao(); 45 boolean iszhuang(); 46 47 48 49 50 51 52 int WINAPI WinMain(HINSTANCE hlnstance,HINSTANCE hPrelnstance, 53 LPSTR lpCmdLine,int nCmdShow){ //callback 54 55 56 //设计 57 HWND hwnd; 58 MSG msg; 59 LPCSTR classname="牟柏旭"; 60 WNDCLASSEX ex; 61 62 ex.style=CS_HREDRAW | CS_VREDRAW; 63 ex.cbSize=sizeof(WNDCLASSEX); 64 ex.cbClsExtra=0; 65 ex.cbWndExtra=0; 66 ex.hbrBackground=CreateSolidBrush(RGB(171,171,226)); 67 ex.hCursor=NULL; 68 ex.hIcon=NULL; 69 ex.hIconSm=NULL; 70 ex.hInstance=hlnstance; 71 ex.lpszMenuName=0; 72 ex.lpszClassName=classname; 73 ex.lpfnWndProc=MyWndProc; //消息过程函数 消息回调函数 74 75 //注册 76 RegisterClassEx(&ex); 77 //创建 78 hwnd= CreateWindow(classname,"mubaixu",WS_OVERLAPPEDWINDOW,50,50,620,630,NULL,NULL, 79 hlnstance,NULL); 80 SetTimer (hwnd, 1, 500, NULL) ; 81 82 //最后一个为是否创建多文档,比如code可以打开多个程序 83 84 85 ShowWindow(hwnd,nCmdShow); 86 87 88 89 h_back=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP1)); 90 h_apple=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP2)); 91 h_body=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP3)); 92 h_l=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP6)); 93 h_r=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP4)); 94 h_u=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP5)); 95 h_d=LoadBitmap(hlnstance,MAKEINTRESOURCE(IDB_BITMAP7)); 96 initSnake(); 97 srand((unsigned int )time(0)); 98 while(GetMessage(&msg,NULL,0,0)){ 99 100 TranslateMessage(&msg); 101 102 DispatchMessage(&msg); 103 //调用MyWndProc,系统调用 104 105 //指定接收哪个窗口的消息,如果为NULL用于接收所有窗口 106 // 后两个为过滤 107 } 108 109 110 111 return 0; 112 } 113 114 int xx=200; 115 HDC tmp_dc; 116 HBITMAP tmp_bitmap; 117 HDC src_dc; 118 HBITMAP src_bitmap; 119 120 HDC t_dc; 121 HBITMAP t_bitmap; 122 int type=0; 123 int begin_x=0; 124 int begin_y=0; 125 int end_x; 126 int end_y; 127 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){ 128 int x,y; 129 130 131 // char tx[10]; 132 //char ty[10]; 133 HDC hdc; 134 //temp_hdc; 135 //HBRUSH newbrush; 136 //HBRUSH oldbrush; 137 PAINTSTRUCT ps; 138 139 // HBITMAP hbitmap ; 140 141 switch (Msg) 142 { 143 144 case WM_KEYDOWN: 145 switch (wParam) 146 { 147 case VK_UP: 148 g_fx=1; 149 break; 150 case VK_DOWN: 151 g_fx=3; 152 break; 153 case VK_RIGHT: 154 g_fx=0; 155 break; 156 case VK_LEFT: 157 g_fx=2; 158 break; 159 160 default: 161 break; 162 } 163 break; 164 165 case WM_MOUSEMOVE: 166 hdc = GetDC (hWnd) ; 167 168 x = LOWORD (lParam) ; 169 y = HIWORD (lParam) ; 170 171 if(type==1){ 172 173 BitBlt(tmp_dc,0,0,620,630,src_dc,0,0,SRCCOPY); 174 //1根线 175 MoveToEx (tmp_dc, begin_x, begin_y, NULL) ; 176 LineTo (tmp_dc,begin_x, y) ; 177 178 //2根 179 MoveToEx (tmp_dc, begin_x, begin_y, NULL) ; 180 LineTo (tmp_dc, x, begin_y) ; 181 //3根 182 MoveToEx (tmp_dc, x, begin_y, NULL) ; 183 LineTo (tmp_dc, x, y) ; 184 //4根 185 MoveToEx (tmp_dc, begin_x,y, NULL) ; 186 LineTo (tmp_dc, x, y) ; 187 188 189 } 190 BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 191 192 ReleaseDC (hWnd, hdc) ; 193 194 break; 195 case WM_LBUTTONDOWN: 196 197 198 hdc = GetDC (hWnd) ; 199 x = LOWORD (lParam) ; 200 y = HIWORD (lParam) ; 201 if(type==0){ 202 begin_x=x; 203 begin_y=y; 204 src_dc=CreateCompatibleDC(hdc); 205 src_bitmap=CreateCompatibleBitmap(hdc,620,630); 206 SelectObject(src_dc,src_bitmap); 207 BitBlt(src_dc,0,0,620,630,hdc,0,0,SRCCOPY); 208 209 type=1; 210 } 211 212 213 // Rectangle(hdc, 150,150,200,200); 214 215 if(1==type){ 216 //创建临时的hdc和位图,并且把原图传过去 217 //BitBlt(hdc,0,0,620,630,src_dc,0,0,SRCCOPY);//把原hdc弄干净 218 tmp_dc=CreateCompatibleDC(hdc); 219 tmp_bitmap=CreateCompatibleBitmap(hdc,620,630); 220 SelectObject(tmp_dc,tmp_bitmap); 221 BitBlt(tmp_dc,0,0,620,630,hdc,0,0,SRCCOPY); 222 223 224 //MessageBox (hWnd,itoa(x,tx,10),itoa(y,ty,10),MB_OKCANCEL);*/ 225 226 MoveToEx (tmp_dc, x, y, NULL) ; 227 228 } 229 ReleaseDC (hWnd, hdc) ; 230 break; 231 232 case WM_LBUTTONUP: 233 hdc = GetDC (hWnd) ; 234 235 x = LOWORD (lParam) ; 236 y = HIWORD (lParam) ; 237 238 if(type==1){ 239 //画线结束 240 LineTo (tmp_dc, x, y) ; 241 BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 242 //抠图 243 type=2; 244 t_dc=CreateCompatibleDC(hdc); 245 t_bitmap=CreateCompatibleBitmap(hdc,620,630); 246 SelectObject(t_dc,t_bitmap); 247 BitBlt(t_dc,0,0,abs(begin_x-x),abs(begin_y-y),tmp_dc, begin_x<x?begin_x:x ,begin_y<y?begin_y:y,SRCCOPY); 248 249 DeleteObject(tmp_dc); 250 DeleteObject(tmp_bitmap); 251 end_x=x; 252 end_y=y; 253 Rectangle(hdc, begin_x,begin_y,end_x,end_y); 254 255 //BitBlt(hdc,x,y,abs(begin_x-x),abs(begin_y-y),t_dc, 0,0,SRCCOPY); 256 257 258 } 259 260 ReleaseDC (hWnd, hdc) ; 261 262 break; 263 case WM_CLOSE: 264 PostQuitMessage(0); 265 266 break; 267 268 case WM_PAINT: 269 hdc = BeginPaint (hWnd, &ps) ; 270 271 show(hdc); 272 273 //Rectangle(hdc, 50,50,100,100); 274 //hbitmap = CreateCompatibleBitmap (hdc, 100, 200) ; 275 //temp_hdc = CreateCompatibleDC (hdc) ; 276 //SelectObject(temp_hdc,h_back); 277 ////Rectangle(temp_hdc, 10,10,50,50); 278 279 //BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ; 280 281 //xx+=20; 282 //Ellipse( hdc, xx, 300, xx+70, 340); 283 284 EndPaint (hWnd, &ps) ; 285 break; 286 287 case WM_TIMER: 288 289 290 291 //pa(); 292 if(iseat()){ 293 Newapple(); 294 add(-20,-20); 295 } 296 if(isziyao()||iszhuang()){ 297 PostQuitMessage(0); 298 } 299 300 hdc = GetDC (hWnd) ; 301 //show(hdc);重绘 302 /*newbrush= CreateSolidBrush(RGB(171,171,226));//椭圆更新移动 303 oldbrush=(HBRUSH)SelectObject(hdc,newbrush); 304 Rectangle( hdc, 0, 0,600,600); 305 306 DeleteObject(newbrush); 307 SelectObject(hdc,oldbrush); 308 xx+=20; 309 Ellipse( hdc, xx, 300, xx+70, 340); 310 Rectangle(hdc, 50,50,100,100);*/ 311 ReleaseDC (hWnd, hdc) ; 312 break; 313 default: 314 break; 315 } 316 //系统默认的消息处理函数 317 return DefWindowProc(hWnd,Msg,wParam,lParam); 318 } 319 320 321 322 void add(int h,int l){ 323 node * pnode=(node *)malloc(sizeof(node)); 324 pnode->h=h; 325 pnode->l=l; 326 pnode->plast=NULL; 327 pnode->pnext=NULL; 328 if(top==NULL){ 329 top=pnode; 330 } 331 else{ 332 pnode->plast=end; 333 end->pnext=pnode; 334 } 335 336 end=pnode; 337 } 338 339 340 void initSnake(){ 341 int i; 342 for( i=8;i<=10;i++){ 343 add(i,10); 344 } 345 346 } 347 348 void show(HDC hdc){ 349 HDC temp_hdc; 350 temp_hdc = CreateCompatibleDC (hdc) ; 351 SelectObject(temp_hdc,h_back); 352 353 BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ; 354 DeleteObject(temp_hdc); 355 showApple( hdc); 356 showSnake( hdc); 357 } 358 void showApple(HDC hdc){ 359 HDC temp_hdc; 360 temp_hdc = CreateCompatibleDC (hdc) ; 361 SelectObject(temp_hdc,h_apple); 362 363 BitBlt (hdc, apple.l*30+30,apple.h*30+30, 30, 30, temp_hdc, 0, 0, SRCCOPY) ; 364 DeleteObject(temp_hdc); 365 } 366 void showSnake(HDC hdc){ 367 HDC temp_hdc; 368 Snake *ptop=top; 369 temp_hdc = CreateCompatibleDC (hdc) ; 370 switch (g_fx) 371 { 372 373 case 0: 374 SelectObject(temp_hdc,h_r); 375 break; 376 case 1: 377 SelectObject(temp_hdc,h_u); 378 break; 379 case 2: 380 SelectObject(temp_hdc,h_l); 381 break; 382 case 3: 383 SelectObject(temp_hdc,h_d); 384 break; 385 default: 386 break; 387 } 388 389 390 BitBlt (hdc, top->l*30+30,top->h*30+30 , 30, 30, temp_hdc, 0, 0, SRCCOPY) ; 391 DeleteObject(temp_hdc); 392 393 394 temp_hdc = CreateCompatibleDC (hdc) ; 395 SelectObject(temp_hdc,h_body); 396 ptop=ptop->pnext; 397 while(ptop){ 398 399 BitBlt (hdc, ptop->l*30+30,ptop->h*30+30 , 30, 30, temp_hdc, 0, 0, SRCCOPY) ; 400 ptop=ptop->pnext; 401 } 402 DeleteObject(temp_hdc); 403 404 } 405 406 void pa(){ 407 Snake *jend=end; 408 while(jend->plast!=NULL) 409 { 410 jend->h=jend->plast->h; 411 jend->l=jend->plast->l; 412 413 jend=jend->plast; 414 } 415 switch(g_fx) 416 { 417 case 0: 418 top->l++; 419 break; 420 case 1: 421 top->h--; 422 break; 423 case 2: 424 top->l--; 425 break; 426 case 3: 427 top->h++; 428 break; 429 430 } 431 } 432 boolean iseat(){ 433 if(top->h==apple.h &&top->l==apple.l ) 434 return TRUE; 435 return FALSE; 436 } 437 void Newapple(){ 438 apple.l=rand()%18; 439 apple.h=rand()%18; 440 } 441 boolean isziyao(){ 442 Snake *pbody=top->pnext; 443 while(pbody!=NULL){ 444 if(top->h==pbody->h &&top->l==pbody->l ) 445 return TRUE; 446 pbody=pbody->pnext; 447 } 448 return FALSE; 449 } 450 boolean iszhuang(){ 451 if(top->h>17||top->l<0||top->l>17||top->h<0) 452 { 453 return TRUE; 454 } 455 return FALSE; 456 }
之后我们便可以,把扣除去的图放在hdc中,我们可以放在WM_BUTTONUP里面,表示我们只需要放在指定的位置上,我们也可以放在WM_MOUSEMOVE里面,表示可以不同的拖拽
1 int xx=200; 2 HDC tmp_dc; 3 HBITMAP tmp_bitmap; 4 HDC src_dc; 5 HBITMAP src_bitmap; 6 7 HDC t_dc; 8 HBITMAP t_bitmap; 9 int type=0; 10 int begin_x=0; 11 int begin_y=0; 12 int end_x; 13 int end_y; 14 LRESULT CALLBACK MyWndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){ 15 int x,y; 16 17 18 // char tx[10]; 19 //char ty[10]; 20 HDC hdc; 21 //temp_hdc; 22 //HBRUSH newbrush; 23 //HBRUSH oldbrush; 24 PAINTSTRUCT ps; 25 26 // HBITMAP hbitmap ; 27 28 switch (Msg) 29 { 30 31 case WM_KEYDOWN: 32 switch (wParam) 33 { 34 case VK_UP: 35 g_fx=1; 36 break; 37 case VK_DOWN: 38 g_fx=3; 39 break; 40 case VK_RIGHT: 41 g_fx=0; 42 break; 43 case VK_LEFT: 44 g_fx=2; 45 break; 46 47 default: 48 break; 49 } 50 break; 51 52 case WM_MOUSEMOVE: 53 hdc = GetDC (hWnd) ; 54 55 x = LOWORD (lParam) ; 56 y = HIWORD (lParam) ; 57 58 if(type==1){ 59 60 BitBlt(tmp_dc,0,0,620,630,src_dc,0,0,SRCCOPY); 61 //1根线 62 MoveToEx (tmp_dc, begin_x, begin_y, NULL) ; 63 LineTo (tmp_dc,begin_x, y) ; 64 65 //2根 66 MoveToEx (tmp_dc, begin_x, begin_y, NULL) ; 67 LineTo (tmp_dc, x, begin_y) ; 68 //3根 69 MoveToEx (tmp_dc, x, begin_y, NULL) ; 70 LineTo (tmp_dc, x, y) ; 71 //4根 72 MoveToEx (tmp_dc, begin_x,y, NULL) ; 73 LineTo (tmp_dc, x, y) ; 74 75 BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 76 } 77 78 79 if(type==2){ 80 81 BitBlt(hdc,0,0,620,630,src_dc, 0,0,SRCCOPY); 82 Rectangle(hdc, begin_x,begin_y,end_x,end_y); 83 BitBlt(hdc,x,y,abs(begin_x-end_x),abs(begin_y-end_y),t_dc, 0,0,SRCCOPY); 84 } 85 86 ReleaseDC (hWnd, hdc) ; 87 88 break; 89 case WM_LBUTTONDOWN: 90 91 92 hdc = GetDC (hWnd) ; 93 x = LOWORD (lParam) ; 94 y = HIWORD (lParam) ; 95 if(type==0){ 96 begin_x=x; 97 begin_y=y; 98 src_dc=CreateCompatibleDC(hdc); 99 src_bitmap=CreateCompatibleBitmap(hdc,620,630); 100 SelectObject(src_dc,src_bitmap); 101 BitBlt(src_dc,0,0,620,630,hdc,0,0,SRCCOPY); 102 103 type=1; 104 } 105 106 107 // Rectangle(hdc, 150,150,200,200); 108 109 if(1==type){ 110 //创建临时的hdc和位图,并且把原图传过去 111 //BitBlt(hdc,0,0,620,630,src_dc,0,0,SRCCOPY);//把原hdc弄干净 112 tmp_dc=CreateCompatibleDC(hdc); 113 tmp_bitmap=CreateCompatibleBitmap(hdc,620,630); 114 SelectObject(tmp_dc,tmp_bitmap); 115 BitBlt(tmp_dc,0,0,620,630,hdc,0,0,SRCCOPY); 116 117 118 //MessageBox (hWnd,itoa(x,tx,10),itoa(y,ty,10),MB_OKCANCEL);*/ 119 120 MoveToEx (tmp_dc, x, y, NULL) ; 121 122 } 123 ReleaseDC (hWnd, hdc) ; 124 break; 125 126 case WM_LBUTTONUP: 127 hdc = GetDC (hWnd) ; 128 129 x = LOWORD (lParam) ; 130 y = HIWORD (lParam) ; 131 132 if(type==1){ 133 //画线结束 134 LineTo (tmp_dc, x, y) ; 135 BitBlt(hdc,0,0,620,630,tmp_dc,0,0,SRCCOPY); 136 //抠图 137 type=2; 138 t_dc=CreateCompatibleDC(hdc); 139 t_bitmap=CreateCompatibleBitmap(hdc,620,630); 140 SelectObject(t_dc,t_bitmap); 141 BitBlt(t_dc,0,0,abs(begin_x-x),abs(begin_y-y),tmp_dc, begin_x<x?begin_x:x ,begin_y<y?begin_y:y,SRCCOPY); 142 143 DeleteObject(tmp_dc); 144 DeleteObject(tmp_bitmap); 145 end_x=x; 146 end_y=y; 147 Rectangle(hdc, begin_x,begin_y,end_x,end_y); 148 149 BitBlt(hdc,x,y,abs(begin_x-x),abs(begin_y-y),t_dc, 0,0,SRCCOPY); 150 151 152 } 153 154 ReleaseDC (hWnd, hdc) ; 155 156 break; 157 case WM_CLOSE: 158 PostQuitMessage(0); 159 160 break; 161 162 case WM_PAINT: 163 hdc = BeginPaint (hWnd, &ps) ; 164 165 show(hdc); 166 167 //Rectangle(hdc, 50,50,100,100); 168 //hbitmap = CreateCompatibleBitmap (hdc, 100, 200) ; 169 //temp_hdc = CreateCompatibleDC (hdc) ; 170 //SelectObject(temp_hdc,h_back); 171 ////Rectangle(temp_hdc, 10,10,50,50); 172 173 //BitBlt (hdc, 0,0, 600, 600, temp_hdc, 0, 0, SRCCOPY) ; 174 175 //xx+=20; 176 //Ellipse( hdc, xx, 300, xx+70, 340); 177 178 EndPaint (hWnd, &ps) ; 179 break; 180 181 case WM_TIMER: 182 183 184 185 //pa(); 186 if(iseat()){ 187 Newapple(); 188 add(-20,-20); 189 } 190 if(isziyao()||iszhuang()){ 191 PostQuitMessage(0); 192 } 193 194 hdc = GetDC (hWnd) ; 195 //show(hdc);重绘 196 /*newbrush= CreateSolidBrush(RGB(171,171,226));//椭圆更新移动 197 oldbrush=(HBRUSH)SelectObject(hdc,newbrush); 198 Rectangle( hdc, 0, 0,600,600); 199 200 DeleteObject(newbrush); 201 SelectObject(hdc,oldbrush); 202 xx+=20; 203 Ellipse( hdc, xx, 300, xx+70, 340); 204 Rectangle(hdc, 50,50,100,100);*/ 205 ReleaseDC (hWnd, hdc) ; 206 break; 207 default: 208 break; 209 } 210 //系统默认的消息处理函数 211 return DefWindowProc(hWnd,Msg,wParam,lParam); 212 }