16,548
社区成员




#include <windows.h>
#define DIVISIONS 5
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName [] = TEXT ("RECT") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, TEXT ("RECT TEST"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
static int cxClient, cyClient;
static BOOL state[DIVISIONS][DIVISIONS];
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
HPEN hpen;
PAINTSTRUCT ps ;
RECT rect;
int i,j;
int x,y;
TCHAR szAppName[]="RECTANGLE";
switch (message)
{
case WM_CREATE:
return 0 ;
case WM_SIZE:
cxClient = LOWORD (lParam)/DIVISIONS ;
cyClient = HIWORD (lParam)/DIVISIONS ;
return 0 ;
case WM_LBUTTONDOWN:
x=LOWORD(lParam);
y=HIWORD(lParam);
i=j=0;
while(!(x<i*cxClient))
{
++i;
}
while(!(y<j*cyClient))
{
++j;
}
if(state[i-1][j-1]==TRUE)
{
state[i-1][j-1]=FALSE;
}
else
{
state[i-1][j-1]=TRUE;
}
InvalidateRect(hwnd,NULL,TRUE);
return 0;
case WM_PAINT:
hdc=BeginPaint(hwnd,&ps);
GetClientRect(hwnd,&rect);
hpen=CreatePen(PS_SOLID,2,RGB(255,0,0));
for(j=0;j<DIVISIONS;j++)
for(i=0;i<DIVISIONS;i++)
{
Rectangle(hdc,i*cxClient,j*cyClient,(i+1)*cxClient,(j+1)*cyClient);
if(state[i][j])
{
SelectObject(hdc,hpen);
MoveToEx(hdc,i*cxClient,j*cyClient,NULL);
LineTo(hdc,(i+1)*cxClient,(j+1)*cyClient);
MoveToEx(hdc,i*cxClient,(j+1)*cyClient,NULL);
LineTo(hdc,(i+1)*cxClient,j*cyClient);
DeleteObject(hpen);
SelectObject(hdc,GetStockObject(BLACK_PEN));
}
}
EndPaint(hwnd,&ps);
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
if(state[i][j])
{
hpen=(HPEN)SelectObject(hdc,CreatePen(PS_SOLID,2,RGB(255,0,0)));
MoveToEx(hdc,i*cxClient,j*cyClient,NULL);
LineTo(hdc,(i+1)*cxClient,(j+1)*cyClient);
MoveToEx(hdc,i*cxClient,(j+1)*cyClient,NULL);
LineTo(hdc,(i+1)*cxClient,j*cyClient);
DeleteObject(SelectObject(hdc,hpen));
//SelectObject(hdc,GetStockObject(BLACK_PEN));
}