64,685
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <stdlib.h> //rand函数
#include <mmsystem.h> //timeGetTime函数
#pragma comment(lib,"winmm.lib") //timeGetTime函数
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void DrawRectangle(HWND);
long nNextGameTick;
long nTick;
long nNextTick;
int cxClient,cyClient;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nShowCmd)
{
static TCHAR szAppName[]=TEXT("RandRect");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT(""),szAppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,TEXT("Random Rectangles"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
//////////////////////////////////////////////这里开始是我改的
while(true)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) //接收消息
{
if(msg.message==WM_QUIT) //如果是退出消息,则退出循环
break;
TranslateMessage(&msg); //将虚拟键消息转换为字符消息
DispatchMessage(&msg); //处理消息
}
else
{
nNextGameTick=0;
nTick=timeGetTime(); //获取当前游戏时间
//获取下次循环时间
nNextTick=nNextGameTick;
if(nTick>nNextTick) //如果游戏时间到达下次循环时间
{
//计算下次循环的时间
nNextGameTick =nTick+1000;
DrawRectangle(hwnd);
}
}
}
////////////////////////////////////////////////////////////////
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch (message)
{
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
void DrawRectangle (HWND hwnd)
{
HBRUSH hBrush ;
HDC hdc ;
RECT rect ;
if (cxClient == 0 || cyClient == 0)
return ;
SetRect (&rect, rand() % cxClient, rand() % cyClient,
rand() % cxClient, rand() % cyClient) ;
hBrush = CreateSolidBrush (RGB (rand() % 256, rand() % 256, rand() % 256)) ;
hdc = GetDC (hwnd) ;
FillRect (hdc, &rect, hBrush) ;
ReleaseDC (hwnd, hdc) ;
DeleteObject (hBrush) ;
}
//我改好了,不用那么麻烦
#include <windows.h>
#include <stdlib.h> //rand函数
#include <mmsystem.h> //timeGetTime函数
#pragma comment(lib,"winmm.lib") //timeGetTime函数
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void DrawRectangle(HWND);
long nLastTick;
int cxClient,cyClient;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
LPSTR lpCmdLine,int nShowCmd)
{
static TCHAR szAppName[]=TEXT("RandRect");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=szAppName;
wndclass.lpszMenuName=NULL;
wndclass.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT(""),szAppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,TEXT("Random Rectangles"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
//这是我修改的
nLastTick = timeGetTime();
//////////////////////////////////////////////这里开始是我改的
while(true)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) //接收消息
{
if(msg.message==WM_QUIT) //如果是退出消息,则退出循环
break;
TranslateMessage(&msg); //将虚拟键消息转换为字符消息
DispatchMessage(&msg); //处理消息
}
else
{
if(timeGetTime() - nLastTick > 1000) //如果游戏时间到达下次循环时间
{
//这是我修改的
nLastTick = timeGetTime();
DrawRectangle(hwnd);
}
}
}
////////////////////////////////////////////////////////////////
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
{
switch (message)
{
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,message,wParam,lParam);
}
void DrawRectangle (HWND hwnd)
{
HBRUSH hBrush ;
HDC hdc ;
RECT rect ;
if (cxClient == 0 || cyClient == 0)
return ;
SetRect (&rect, rand() % cxClient, rand() % cyClient,
rand() % cxClient, rand() % cyClient) ;
hBrush = CreateSolidBrush (RGB (rand() % 256, rand() % 256, rand() % 256)) ;
hdc = GetDC (hwnd) ;
FillRect (hdc, &rect, hBrush) ;
ReleaseDC (hwnd, hdc) ;
DeleteObject (hBrush) ;
}