首页
社区
课程
招聘
windows程序设计中RandRect的改编,有些问题!!!
发表于: 2005-10-24 13:20 5423

windows程序设计中RandRect的改编,有些问题!!!

2005-10-24 13:20
5423
windows程序设计中RandRect的改编,有些问题!!!  
  请大家把DrawRandRect函数中的注释行保留和去掉注释个运行一遍
问题是为什么去掉注释行后,出现的矩形一直闪动?????

#include <windows.h>
#include "math.h"
#include "time.h"
//#define WINVER 0X500

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void DrawRandRect(HWND);
int cxClient,cyClient;

int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)

{
WNDCLASS wndclass;
TCHAR szAppName[]=TEXT("Rand rect");
HWND hwnd;
MSG msg;

wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_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,"This need NT",szAppName,0);
return 0;
}

hwnd=CreateWindow(szAppName,szAppName,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
400,300,NULL,
NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

while(TRUE)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else  
DrawRandRect(hwnd);

}

谢谢了!!!

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

收藏
免费 0
支持
分享
最新回复 (3)
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
1  windows程序设计中RandRect的改编,有些问题!!!  
  请大家把DrawRandRect函数中的注释行保留和去掉注释个运行一遍
问题是为什么去掉注释行后,出现的矩形一直闪动?????

#include <windows.h>
#include "math.h"
#include "time.h"
//#define WINVER 0X500

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
void DrawRandRect(HWND);
int cxClient,cyClient;

int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)

{
WNDCLASS wndclass;
TCHAR szAppName[]=TEXT("Rand rect");
HWND hwnd;
MSG msg;

wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_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,"This need NT",szAppName,0);
return 0;
}

hwnd=CreateWindow(szAppName,szAppName,WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
400,300,NULL,
NULL,hInstance,NULL);
ShowWindow(hwnd,SW_SHOWNORMAL);
UpdateWindow(hwnd);

while(TRUE)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else  
DrawRandRect(hwnd);

}

return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT message, WPARAM wParam,LPARAM lParam)
{
                    

switch (message)
{
case WM_CREATE:
PlaySound("Windows XP 启动.wav",NULL,SND_ASYNC |SND_FILENAME);

return 0;
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;

case WM_DESTROY:
PostQuitMessage(0);
return 0;

default:
return DefWindowProc(hwnd,message,wParam,lParam);

}

}

void DrawRandRect(HWND hwnd)
{ RECT rect;
HDC hdc;
HBRUSH hbr;
int i;
if(cxClient==0||cyClient==0)
return;
hdc=GetDC(hwnd);
SetRect(&rect,0,0,cxClient,cyClient);
hbr=(HBRUSH)GetStockObject(WHITE_BRUSH);
FillRect(hdc,&rect,hbr);
//for(i=0;i<250000000;i++);
srand((unsigned)time(NULL));
SetRect(&rect,rand()%cxClient,rand()%cyClient,rand()%cxClient,rand()%cyClient);
hbr=CreateSolidBrush(RGB(rand()%256,rand()%256,rand()%256));
FillRect(hdc,&rect,hbr);
ReleaseDC(hwnd,hdc);
DeleteObject(hbr);
2005-10-24 13:23
0
雪    币: 200
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
我也在关注这个问题
2005-11-1 22:25
0
雪    币: 214
活跃值: (70)
能力值: ( LV3,RANK:20 )
在线值:
发帖
回帖
粉丝
4
把这个for(i=0;i<250000000;i++);
换成Sleep(n);试试
2005-11-2 04:05
0
游客
登录 | 注册 方可回帖
返回
//