我想在一个窗口客户区画出来车道,用矩形代表车辆,在车道入口随机出现一定数量的矩形(模拟车流),沿着车道运动。请问该怎么办????
------解决思路----------------------
仅供参考:
#pragma comment(lib,"user32")
#pragma comment(lib,"gdi32")
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>
HWND WINAPI GetConsoleWindow();
void HideTheCursor() {
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (GetConsoleCursorInfo(hStdOut, &cciCursor)) {
cciCursor.bVisible = FALSE;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}
void ShowTheCursor() {
CONSOLE_CURSOR_INFO cciCursor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (GetConsoleCursorInfo(hStdOut, &cciCursor)) {
cciCursor.bVisible = TRUE;
SetConsoleCursorInfo(hStdOut, &cciCursor);
}
}
int main() {
HWND hwnd;
HDC hdc;
HFONT hfont;
RECT rect,wsize;
HBRUSH hbrush;
int y,x,d,g;
system("color F0");
system("cls");
HideTheCursor();
hwnd =GetConsoleWindow();
GetClientRect(hwnd,&wsize);
hdc =GetDC(hwnd);
hfont =CreateFont(48,0,0,0,0,0,0,0,0,0,0,0,0,"华文楷体");
hbrush=CreateSolidBrush((COLORREF)0x00FFFFFF);
SelectObject(hdc,hfont);
y=10;x=30;d=4;g=3;
while (1) {
rect.left=x;
rect.top=y;
rect.right=x+300+d+1;
rect.bottom=y+60+d+1;
FillRect(hdc, &rect, hbrush);
TextOut(hdc,x+10,y+10,"地球人都知道!",14);
MoveToEx(hdc,x+5,y+5,NULL);
LineTo(hdc,x+300,y+ 5);
LineTo(hdc,x+300,y+ 60);
LineTo(hdc,x+ 5,y+ 60);
LineTo(hdc,x+ 5,y+ 5);
Sleep(15);
if (_kbhit()) {getch();break;}
switch (g) {
case 0:if (y> d) y-=d; else g=2;if (x> d) x-=d; else g=1;break;// ↖
case 1:if (y> d) y-=d; else g=3;if (x<wsize.right-300-d) x+=d; else g=0;break;// ↗
case 2:if (y<wsize.bottom-60-d) y+=d; else g=0;if (x> d) x-=d; else g=3;break;// ↙
case 3:if (y<wsize.bottom-60-d) y+=d; else g=1;if (x<wsize.right-300-d) x+=d; else g=2;break;// ↘
}
}
DeleteObject(hbrush);
DeleteObject(hfont);
ReleaseDC(hwnd,hdc);
system("color 07");
system("cls");
ShowTheCursor();
return 0;
}
------解决思路----------------------
http://blog.csdn.net/muzi9_17/article/details/6376922