鱼C论坛

 找回密码
 立即注册
查看: 2542|回复: 1

[学习笔记] 中国象棋棋盘

[复制链接]
发表于 2018-5-28 22:55:22 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
学了小甲鱼的视频之后写了个象棋盘,大神多多指点一下哪里需要简写的
高手们就不要笑了,该指点的,还得拜托说说意见,毕竟俺是菜鸟
  1. /* -------------------------------------------------------------------
  2.                     MyWindows.c -- 基本窗口模型  
  3.                                 《Windows 程序设计(SDK)》视频教程                    
  4. --------------------------------------------------------------------*/

  5. #include <windows.h>
  6. void XY(HDC, int, int);
  7. void RXY(HDC, int, int);
  8. void LXY(HDC, int, int);
  9. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

  10. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  11. {
  12.         static TCHAR szAppName[] = TEXT("MyWindows");
  13.         HWND hwnd;
  14.         MSG msg;
  15.         WNDCLASS wndclass;

  16.         wndclass.style = CS_HREDRAW | CS_VREDRAW;
  17.         wndclass.lpfnWndProc = WndProc;
  18.         wndclass.cbClsExtra = 0;
  19.         wndclass.cbWndExtra = 0;
  20.         wndclass.hInstance = hInstance;
  21.         wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  22.         wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
  23.         wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  24.         wndclass.lpszMenuName = NULL;
  25.         wndclass.lpszClassName = szAppName;

  26.         if (!RegisterClass(&wndclass))
  27.         {
  28.                 MessageBox(NULL, TEXT("这个程序需要在 Windows NT 才能执行!"), szAppName, MB_ICONERROR);
  29.                 return 0;
  30.         }

  31.         hwnd = CreateWindow(szAppName,
  32.                 TEXT("鱼C工作室"),
  33.                 WS_OVERLAPPEDWINDOW,
  34.                 CW_USEDEFAULT,
  35.                 CW_USEDEFAULT,
  36.                 CW_USEDEFAULT,
  37.                 CW_USEDEFAULT,
  38.                 NULL,
  39.                 NULL,
  40.                 hInstance,
  41.                 NULL);
  42.        
  43.         ShowWindow(hwnd, iCmdShow);
  44.         UpdateWindow(hwnd);

  45.         while (GetMessage(&msg, NULL, 0, 0))
  46.         {
  47.                 TranslateMessage(&msg);
  48.                 DispatchMessage(&msg);
  49.         }

  50.         return msg.wParam;
  51. }

  52. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  53. {
  54.         HDC hdc;
  55.         PAINTSTRUCT ps;
  56.         static int cxChar, cyChar;
  57.         int i, j;
  58.    
  59.         switch (message)
  60.         {
  61.         case WM_SIZE:
  62.                 cxChar = LOWORD(lParam);
  63.                 cyChar = HIWORD(lParam);
  64.                 return 0;
  65.         case WM_PAINT:
  66.                 hdc = BeginPaint(hwnd, &ps);
  67.                 MoveToEx(hdc, cxChar / 2 + 25, 50, NULL);
  68.                 LineTo(hdc, cxChar / 2 + 25, 400);
  69.                 //右竖线
  70.                 for (i = 0;i < 5;i++)
  71.                 {
  72.                         MoveToEx(hdc, cxChar / 2 + 25 + 50 * i, 50, NULL);
  73.                         LineTo(hdc, cxChar / 2 + 25 + 50 * i, 450);
  74.                 }

  75.                 MoveToEx(hdc, cxChar / 2 - 25, 50, NULL);
  76.                 LineTo(hdc, cxChar / 2 - 25, 450);
  77.                 //左竖线
  78.                 for (i = 4;i > 0;i--)
  79.                 {
  80.                         MoveToEx(hdc, cxChar / 2 - 25 - (50 * i), 50, NULL);
  81.                         LineTo(hdc, cxChar / 2 - 25 - (50 * i), 450);
  82.                 }

  83.                 MoveToEx(hdc, cxChar / 2 - 25 - 50 * 4, 50, NULL);
  84.                 LineTo(hdc, cxChar / 2 + 25 + 50 * 4, 50);
  85.                 //左横线
  86.                 for (i = 2; i < 9; i++)
  87.                 {
  88.                         MoveToEx(hdc, cxChar / 2 - 25 - 50 * 4, 50 * i, NULL);
  89.                         LineTo(hdc, cxChar / 2 - 25, 50 * i);
  90.                 }
  91.                 //右横线
  92.                 for (i = 2; i < 9; i++)
  93.                 {
  94.                         MoveToEx(hdc, cxChar / 2 + 25 + 50 * 4, 50 * i, NULL);
  95.                         LineTo(hdc, cxChar / 2 + 25, 50 * i);
  96.                 }

  97.                 MoveToEx(hdc, cxChar / 2 - 25 - 50 * 4, 450, NULL);
  98.                 LineTo(hdc, cxChar / 2 + 25 + 50 * 4, 450);

  99.                 MoveToEx(hdc, cxChar / 2 - 25 - 50 * 4, 200, NULL);
  100.                 LineTo(hdc, cxChar / 2 - 25 - 50 * 2, 300);

  101.                 MoveToEx(hdc, cxChar / 2 - 25 - 50 * 4, 300, NULL);
  102.                 LineTo(hdc, cxChar / 2 - 25 - 50 * 2, 200);

  103.                 MoveToEx(hdc, cxChar / 2 + 25 + 50 * 4, 200, NULL);
  104.                 LineTo(hdc, cxChar / 2 + 25 + 50 * 2, 300);

  105.                 MoveToEx(hdc, cxChar / 2 + 25 + 50 * 4, 300, NULL);
  106.                 LineTo(hdc, cxChar / 2 + 25 + 50 * 2, 200);


  107.                 XY(hdc, cxChar / 2 - 25 - 50 * 2, 100);
  108.                 XY(hdc, cxChar / 2 + 25 + 50 * 2, 100);
  109.                 XY(hdc, cxChar / 2 - 25 - 50 * 2, 400);
  110.                 XY(hdc, cxChar / 2 + 25 + 50 * 2, 400);
  111.                 for (j = 0;j < 3;j++)
  112.                 {
  113.                         XY(hdc, cxChar / 2 - 25 - 50, 200 + (100 * j)-50);
  114.                         XY(hdc, cxChar / 2 + 25 + 50, 200 + (100 * j)-50);
  115.                 }
  116.                 RXY(hdc, cxChar / 2 + 25 + 50, 50);
  117.                 RXY(hdc, cxChar / 2 - 25 - 50, 50);
  118.                 LXY(hdc, cxChar / 2 + 25 + 50, 450);
  119.                 LXY(hdc, cxChar / 2 -25 - 50, 450);
  120.                 EndPaint(hwnd, &ps);
  121.                 return 0;

  122.         case WM_DESTROY:
  123.                 PostQuitMessage(0);
  124.                 return 0;
  125.         }

  126.         return DefWindowProc(hwnd, message, wParam, lParam);
  127. }

  128. void XY(HDC hdc,int lx, int ly)
  129. {

  130.         MoveToEx(hdc, lx - 16, ly+6, NULL);
  131.         LineTo(hdc, lx - 6, ly+6);
  132.         LineTo(hdc, lx - 6, ly+16);

  133.         MoveToEx(hdc, lx - 16, ly-6, NULL);
  134.         LineTo(hdc, lx - 6, ly-6);
  135.         LineTo(hdc, lx - 6, ly-16);

  136.         MoveToEx(hdc, lx + 16, ly+6, NULL);
  137.         LineTo(hdc, lx + 6, ly+6);
  138.         LineTo(hdc, lx + 6, ly+16);

  139.         MoveToEx(hdc,lx + 16, ly-6, NULL);
  140.         LineTo(hdc, lx + 6, ly-6);
  141.         LineTo(hdc, lx + 6, ly-16);

  142. }
  143. void RXY(HDC hdc, int lrx, int lry)
  144. {
  145.         MoveToEx(hdc, lrx - 16, lry + 6, NULL);
  146.         LineTo(hdc, lrx - 6, lry + 6);
  147.         LineTo(hdc, lrx - 6, lry + 16);

  148.         MoveToEx(hdc, lrx + 16, lry + 6, NULL);
  149.         LineTo(hdc, lrx + 6, lry + 6);
  150.         LineTo(hdc, lrx + 6, lry + 16);
  151. }
  152. void LXY(HDC hdc, int lx, int ly)
  153. {
  154.         MoveToEx(hdc, lx + 16, ly - 6, NULL);
  155.         LineTo(hdc, lx + 6, ly - 6);
  156.         LineTo(hdc, lx + 6, ly - 16);

  157.         MoveToEx(hdc, lx - 16, ly - 6, NULL);
  158.         LineTo(hdc, lx - 6, ly - 6);
  159.         LineTo(hdc, lx - 6, ly - 16);
  160. }
复制代码
QQ截图20180528224502.png

评分

参与人数 1荣誉 +10 鱼币 +10 贡献 +10 收起 理由
alltolove + 10 + 10 + 10 支持楼主!

查看全部评分

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-5-28 23:02:34 | 显示全部楼层
别骂我没有注释,刚开始学的
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 12:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表