?? 3? ????.cpp
字號:
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow)
{
static TCHAR szAppname[] = TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wndclass.lpfnWndProc = WndProc; //磊悼欄肺 齲免且 竊薦狼 撈撫(林家)
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance; //WinMain狼 霉鍋擄 頗扼皋磐肺 逞絹吭帶...
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 Windwos NT !"), szAppname, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppname, //window class name
"The Hello Program", //window caption
WS_OVERLAPPEDWINDOW, //window style
CW_USEDEFAULT, //initial x position
CW_USEDEFAULT, //initial y position
300, //initial x size
300, //initial y size
NULL, //parent window handle
NULL, //window menu handle
hInstance, //program instance handle
NULL); //creation parameters
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
#define MAXROW 25
#define MAXCOL 25
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static int Board[MAXROW][MAXCOL];
static int cxBlock;
static int cyBlock;
switch(message)
{
case WM_CREATE:
{
RECT rect;
SetRect(&rect, 0, 0, 30*MAXCOL, 30*MAXROW);
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
MoveWindow(hwnd, 100, 100, rect.right - rect.left, rect.bottom - rect.top, TRUE);
}
return 0;
case WM_SIZE:
cxBlock = LOWORD(lParam) / MAXCOL;
cyBlock = HIWORD(lParam) / MAXROW;
return 0;
case WM_LBUTTONDOWN:
{
int x = LOWORD(lParam);
int y = HIWORD(lParam);
int xPos = x / cxBlock;
int yPos = y / cyBlock;
Board[yPos][xPos] = 1;
RECT rect;
SetRect(&rect, xPos*cxBlock, yPos*cyBlock, (xPos+1)*cxBlock, (yPos+1)*cyBlock);
InvalidateRect(hwnd, &rect, TRUE);
}
return 0;
case WM_RBUTTONDOWN:
{
int x = LOWORD(lParam);
int y = HIWORD(lParam);
int xPos = x / cxBlock;
int yPos = y / cyBlock;
Board[yPos][xPos] = 2;
RECT rect;
SetRect(&rect, xPos*cxBlock, yPos*cyBlock, (xPos+1)*cxBlock, (yPos+1)*cyBlock);
InvalidateRect(hwnd, &rect, TRUE);
}
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_RETURN:
{
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
SendMessage(hwnd, WM_LBUTTONDOWN, NULL, MAKELPARAM(pt.x, pt.y));
}
return 0;
case VK_SPACE:
{
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
SendMessage(hwnd, WM_RBUTTONDOWN, NULL, MAKELPARAM(pt.x, pt.y));
}
return 0;
case VK_LEFT:
{
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
int xPos = pt.x / cxBlock;
int yPos = pt.y / cyBlock;
xPos = ((xPos - 1) + MAXCOL) % MAXCOL;
pt.x = xPos * cxBlock + cxBlock/2;
pt.y = yPos * cyBlock + cyBlock/2;
ClientToScreen(hwnd, &pt);
SetCursorPos(pt.x, pt.y);
}
break;
case VK_RIGHT:
{
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
int xPos = pt.x / cxBlock;
int yPos = pt.y / cyBlock;
xPos = ((xPos + 1) + MAXCOL) % MAXCOL;
pt.x = xPos * cxBlock + cxBlock/2;
pt.y = yPos * cyBlock + cyBlock/2;
ClientToScreen(hwnd, &pt);
SetCursorPos(pt.x, pt.y);
}
break;
case VK_UP:
{
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
int xPos = pt.x / cxBlock;
int yPos = pt.y / cyBlock;
yPos = ((yPos - 1) + MAXROW) % MAXROW;
pt.x = xPos * cxBlock + cxBlock/2;
pt.y = yPos * cyBlock + cyBlock/2;
ClientToScreen(hwnd, &pt);
SetCursorPos(pt.x, pt.y);
}
break;
case VK_DOWN:
{
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwnd, &pt);
int xPos = pt.x / cxBlock;
int yPos = pt.y / cyBlock;
yPos = ((yPos + 1) + MAXROW) % MAXROW;
pt.x = xPos * cxBlock + cxBlock/2;
pt.y = yPos * cyBlock + cyBlock/2;
ClientToScreen(hwnd, &pt);
SetCursorPos(pt.x, pt.y);
}
break;
}
return 0;
case WM_PAINT:
{
HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
for (int y = 0; y < MAXROW-1; ++y)
{
for (int x = 0; x < MAXCOL-1; ++x)
{
Rectangle(hdc, x*cxBlock+cxBlock/2, y*cyBlock+cyBlock/2, (x+1)*cxBlock+cxBlock/2, (y+1)*cyBlock+cyBlock/2);
}
}
for (y = 0; y < MAXROW; ++y)
{
for (int x = 0; x < MAXCOL; ++x)
{
if (Board[y][x] == 1)
{
HBRUSH hBrush = CreateSolidBrush(RGB(255,0,0));
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Ellipse(hdc, x*cxBlock, y*cyBlock, (x+1)*cxBlock, (y+1)*cyBlock);
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);
}
else if(Board[y][x] == 2)
{
HBRUSH hBrush = CreateSolidBrush(RGB(0,0,255));
HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
Ellipse(hdc, x*cxBlock, y*cyBlock, (x+1)*cxBlock, (y+1)*cyBlock);
SelectObject(hdc, hOldBrush);
DeleteObject(hBrush);
}
}
}
EndPaint(hwnd, &ps);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -