中国象棋的棋盘,程序加源代码,本人是个菜鸟求大神帮忙简化一下代码
本帖最后由 jchao288195 于 2015-12-12 23:24 编辑先声明 本人是个菜鸟,,程序是我自己写出来,没有一定的编程基础的话,我建议还是不要看我的源代码,一看就乱了,代码也没什么注释;因为程序是我自己写的,我都看了一头雾水,我才用了两个API函数,就是MoveToEx和LineTo这两个,我整理3天才整出一个像样的棋盘,,求大神们帮忙简化一下程序
链接:http://pan.baidu.com/s/1eRrJu8a 密码:8h33
程序源代码:
/* -------------------------------------------------------------------
MyWindows.c -- 基本窗口模型
《Windows 程序设计(SDK)》视频教程
--------------------------------------------------------------------*/
#include <windows.h>
void XY(HDC, int, int);
void RXY(HDC, int, int);
void LXY(HDC, int, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT("MyWindows");
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(LTGRAY_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("鱼C工作室"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
600,
550,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
static int cxChar, cyChar;
int i, j;
static HPEN hpen, holbpen;
switch (message)
{
case WM_SIZE:
cxChar = LOWORD(lParam);
cyChar = HIWORD(lParam);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
MoveToEx(hdc, cxChar / 2 + 25, 50, NULL);
LineTo(hdc, cxChar / 2 + 25, 400);
hpen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
holbpen = SelectObject(hdc, hpen);
//右竖线
for (i = 0;i < 5;i++)
{
MoveToEx(hdc, cxChar / 2 + 25 + 50 * i, 50, NULL);
LineTo(hdc, cxChar / 2 + 25 + 50 * i, 450);
}
MoveToEx(hdc, cxChar / 2 - 25, 50, NULL);
LineTo(hdc, cxChar / 2 - 25, 450);
//左竖线
for (i = 4;i > 0;i--)
{
MoveToEx(hdc, cxChar / 2 - 25 - (50 * i), 50, NULL);
LineTo(hdc, cxChar / 2 - 25 - (50 * i), 450);
}
MoveToEx(hdc, cxChar / 2 - 25 - 50 * 4, 50, NULL);
LineTo(hdc, cxChar / 2 + 25 + 50 * 4, 50);
//左横线
for (i = 2; i < 9; i++)
{
MoveToEx(hdc, cxChar / 2 - 25 - 50 * 4, 50 * i, NULL);
LineTo(hdc, cxChar / 2 - 25, 50 * i);
}
//右横线
for (i = 2; i < 9; i++)
{
MoveToEx(hdc, cxChar / 2 + 25 + 50 * 4, 50 * i, NULL);
LineTo(hdc, cxChar / 2 + 25, 50 * i);
}
MoveToEx(hdc, cxChar / 2 - 25 - 50 * 4, 450, NULL);
LineTo(hdc, cxChar / 2 + 25 + 50 * 4, 450);
MoveToEx(hdc, cxChar / 2 - 25 - 50 * 4, 200, NULL);
LineTo(hdc, cxChar / 2 - 25 - 50 * 2, 300);
MoveToEx(hdc, cxChar / 2 - 25 - 50 * 4, 300, NULL);
LineTo(hdc, cxChar / 2 - 25 - 50 * 2, 200);
MoveToEx(hdc, cxChar / 2 + 25 + 50 * 4, 200, NULL);
LineTo(hdc, cxChar / 2 + 25 + 50 * 2, 300);
MoveToEx(hdc, cxChar / 2 + 25 + 50 * 4, 300, NULL);
LineTo(hdc, cxChar / 2 + 25 + 50 * 2, 200);
XY(hdc, cxChar / 2 - 25 - 50 * 2, 100);
XY(hdc, cxChar / 2 + 25 + 50 * 2, 100);
XY(hdc, cxChar / 2 - 25 - 50 * 2, 400);
XY(hdc, cxChar / 2 + 25 + 50 * 2, 400);
for (j = 0;j < 3;j++)
{
XY(hdc, cxChar / 2 - 25 - 50, 200 + (100 * j)-50);
XY(hdc, cxChar / 2 + 25 + 50, 200 + (100 * j)-50);
}
RXY(hdc, cxChar / 2 + 25 + 50, 50);
RXY(hdc, cxChar / 2 - 25 - 50, 50);
LXY(hdc, cxChar / 2 + 25 + 50, 450);
LXY(hdc, cxChar / 2 -25 - 50, 450);
SelectObject(hdc, holbpen);
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
DeleteObject(holbpen);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
void XY(HDC hdc,int lx, int ly)
{
MoveToEx(hdc, lx - 16, ly+6, NULL);
LineTo(hdc, lx - 6, ly+6);
LineTo(hdc, lx - 6, ly+16);
MoveToEx(hdc, lx - 16, ly-6, NULL);
LineTo(hdc, lx - 6, ly-6);
LineTo(hdc, lx - 6, ly-16);
MoveToEx(hdc, lx + 16, ly+6, NULL);
LineTo(hdc, lx + 6, ly+6);
LineTo(hdc, lx + 6, ly+16);
MoveToEx(hdc,lx + 16, ly-6, NULL);
LineTo(hdc, lx + 6, ly-6);
LineTo(hdc, lx + 6, ly-16);
}
void RXY(HDC hdc, int lrx, int lry)
{
MoveToEx(hdc, lrx - 16, lry + 6, NULL);
LineTo(hdc, lrx - 6, lry + 6);
LineTo(hdc, lrx - 6, lry + 16);
MoveToEx(hdc, lrx + 16, lry + 6, NULL);
LineTo(hdc, lrx + 6, lry + 6);
LineTo(hdc, lrx + 6, lry + 16);
}
void LXY(HDC hdc, int lx, int ly)
{
MoveToEx(hdc, lx + 16, ly - 6, NULL);
LineTo(hdc, lx + 6, ly - 6);
LineTo(hdc, lx + 6, ly - 16);
MoveToEx(hdc, lx - 16, ly - 6, NULL);
LineTo(hdc, lx - 6, ly - 6);
LineTo(hdc, lx - 6, ly - 16);
}
Mark一下,晚一点来学习~{:7_140:} :smile
页:
[1]