#include<windows.h>
#include<stdlib.h>
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);
int cxClient,cyClient;
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)); //创建画刷,只要一个RGB作为参数
hdc=GetDC(hwnd);
FillRect(hdc,&rect,hBrush);
ReleaseDC(hwnd,hdc);
DeleteObject(hBrush);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPreInstance,PSTR szCmdLine,int nCmdShow)
{
static TCHAR szAppName[]=TEXT("RandRect");
HWND hWnd;
tagMSG msg;
WNDCLASS wndclass;
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.style=CS_VREDRAW|CS_HREDRAW;
wndclass.lpszMenuName=NULL;
if(!RegisterClass(&wndclass))
{
MessageBox(NULL,TEXT("Registe Class Error!!"),szAppName,MB_ICONERROR);
return 0;
}
hWnd=CreateWindow(szAppName,TEXT("RANDRECT"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);
while(GetMessage(&msg,NULL,0,0))
{
//if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
//{
TranslateMessage(&msg);
DispatchMessage(&msg);
//}
/*else
{
DrawRectangle(hWnd);
}*/
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(message)
{
case WM_SIZE:
cxClient=LOWORD(lParam);
cyClient=HIWORD(lParam);
return 0;
case WM_LBUTTONDOWN:
DrawRectangle(hWnd);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
/*case WM_PAINT:
hdc=BeginPaint(hWnd,&ps);
EndPaint(hWnd,&ps);
return 0;*/
}
return DefWindowProc(hWnd,message,wParam,lParam);
}
第五章 RANDRECT
最新推荐文章于 2023-08-09 19:44:47 发布