鱼C论坛

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

[技术交流] 自制wn32绘画太极图

[复制链接]
发表于 2024-8-2 13:52:17 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<windows.h>
  2. #include<windowsx.h>

  3. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

  4. int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
  5. {
  6.         static TCHAR szAppName[] = TEXT("MyAppWindows");
  7.         HWND hwnd = NULL;
  8.         MSG msg;
  9.         WNDCLASS wc;
  10.         wc.style = CS_HREDRAW | CS_VREDRAW;
  11.         wc.lpfnWndProc = WndProc;
  12.         wc.cbClsExtra = 0;
  13.         wc.cbWndExtra = 0;
  14.         wc.hInstance = hInstance;
  15.         wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  16.         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  17.         wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  18.         wc.lpszMenuName = NULL;
  19.         wc.lpszClassName = szAppName;

  20.         if (!RegisterClass(&wc))
  21.         {
  22.                 MessageBox(NULL, TEXT("Need Windows NT!!!!"), TEXT("Error"), MB_OK);
  23.         }


  24.         hwnd = CreateWindow(szAppName, TEXT("我的窗口"),
  25.                 WS_OVERLAPPEDWINDOW,
  26.                 CW_USEDEFAULT,
  27.                 CW_USEDEFAULT,
  28.                 CW_USEDEFAULT,
  29.                 CW_USEDEFAULT,
  30.                 NULL,
  31.                 NULL,
  32.                 hInstance,
  33.                 NULL);

  34.         ShowWindow(hwnd, iCmdShow);

  35.         UpdateWindow(hwnd);

  36.         while (GetMessage(&msg, NULL, 0, 0))
  37.         {
  38.                 TranslateMessage(&msg);
  39.                 DispatchMessage(&msg);
  40.         }

  41.         return msg.wParam;
  42. }
  43. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
  44. {
  45.         HDC hdc;
  46.         PAINTSTRUCT ps;
  47.         RECT rect;
  48.         static int cxClient, cyClient;//客户区宽度和高度
  49.         static int Dbig,Rbig,Rsmall,Rmin;//太极图最大圆形的直径,半径,中间圆形的半径,最小圆形的半径
  50.         static int xBigCenter, yBigCenter;//设置大圆的中心,是客户区中心
  51.         static int xTopSmallCenter, yTopSmallCenter,xBottomSmallCenter,yBottomSmallCenter;//设置顶部小圆圆心和底部小圆圆心坐标
  52.         static int xTopMinCenter, yTopMinCenter,xBottomMinCenter,yBottomMinCenter;//设置顶部和底部最小圆圆心的坐标
  53.         switch (message)
  54.         {
  55.         case WM_PAINT:
  56.                 hdc = BeginPaint(hwnd, &ps);

  57.                 //画出最大圆形
  58.                 Ellipse(hdc, xBigCenter - Rbig, yBigCenter - Rbig, xBigCenter + Rbig, yBigCenter + Rbig);

  59.                 //画出上方小圆,用黑色填充
  60.                 SelectObject(hdc, GetStockObject(BLACK_BRUSH));
  61.                 Ellipse(hdc, xTopSmallCenter - Rsmall, yTopSmallCenter - Rsmall, xTopSmallCenter + Rsmall, yTopSmallCenter + Rsmall);

  62.                 //画出左半边半圆,用黑色填充
  63.                 Pie(hdc, xBigCenter - Rbig, yBigCenter - Rbig, xBigCenter + Rbig, yBigCenter + Rbig, xBigCenter, yBigCenter - Rbig, xBigCenter, yBigCenter + Rbig);

  64.                 //画出上方最小圆,用白色填充
  65.                 SelectObject(hdc, GetStockObject(WHITE_BRUSH));
  66.                 Ellipse(hdc, xTopMinCenter - Rmin, yTopMinCenter - Rmin, xTopMinCenter + Rmin, yTopMinCenter + Rmin);

  67.                 //画出下方小半圆
  68.                 Pie(hdc, xBottomSmallCenter - Rsmall, yBottomSmallCenter - Rsmall, xBottomSmallCenter + Rsmall, yBottomSmallCenter + Rsmall, xBottomSmallCenter, yBottomSmallCenter - Rsmall, xBottomSmallCenter, yBottomSmallCenter + Rsmall);;
  69.                
  70.                 //小半圆的直径显示出来了,再贴一条白线盖住
  71.                 for (size_t i = yBottomSmallCenter - Rsmall; i <= yBottomSmallCenter + Rsmall; i++)
  72.                 {
  73.                         SetPixel(hdc, xBottomSmallCenter - 1, i, RGB(255, 255, 255));//发现咋也盖不住,尝试了半天,才发现减去1就可以盖住了,有哪位大佬知道这是为什么吗??
  74.                 }

  75.                 //画出底部最小圆,用黑色填充
  76.                 SelectObject(hdc, GetStockObject(BLACK_BRUSH));
  77.                 Ellipse(hdc, xBottomMinCenter - Rmin, yBottomMinCenter - Rmin, xBottomSmallCenter + Rmin, yBottomSmallCenter + Rmin);
  78.                
  79.                 ReleaseDC(hwnd, hdc);
  80.                 return 0;
  81.         case WM_SIZE:
  82.                 cxClient = LOWORD(lparam);
  83.                 cyClient = HIWORD(lparam);

  84.                 //设置大圆圆心坐标
  85.                 Dbig = (((cxClient > cyClient)?cyClient:cxClient));//直径是客户区短的那一边
  86.                 Rbig = Dbig / 2;
  87.                 Rsmall = Rbig / 2;
  88.                 Rmin = Rsmall / 3;
  89.                 xBigCenter = cxClient / 2;
  90.                 yBigCenter = cyClient / 2;

  91.                 //设置上方小圆圆心坐标
  92.                 xTopSmallCenter = xBigCenter;
  93.                 yTopSmallCenter = yBigCenter - Rbig / 2;

  94.                 //设置上方最小圆圆心坐标
  95.                 xTopMinCenter = xBigCenter;
  96.                 yTopMinCenter = yBigCenter - Rbig / 2;

  97.                 //设置下方小圆圆心坐标(应该是半圆)
  98.                 xBottomSmallCenter = xBigCenter;
  99.                 yBottomSmallCenter = yBigCenter + Rbig / 2;

  100.                 //设置下方最小圆半径
  101.                 xBottomMinCenter = xBigCenter;
  102.                 yBottomMinCenter = yBigCenter + Rbig / 2;
  103.                 return 0;
  104.         case WM_DESTROY://接受WM_DESTROY消息并发送WM_QUIT消息
  105.                 PostQuitMessage(0);
  106.                 return 0;
  107.         }


  108.         return DefWindowProc(hwnd, message, wparam, lparam);
  109. }
复制代码
(只有我把小甲鱼的作业认真完成了吗??)
PixPin_2024-08-02_13-51-04.png

评分

参与人数 1荣誉 +1 鱼币 +1 收起 理由
Lynn_oyl + 1 + 1 鱼C有你更精彩^_^

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-8-2 16:59:36 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-7 16:02

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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