鱼C论坛

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

win32GDI函数问题

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

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

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

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

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

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

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


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

  35.         ShowWindow(hwnd, iCmdShow);

  36.         UpdateWindow(hwnd);

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

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

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

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

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

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

  68.                 //画出下方小半圆
  69.                 Pie(hdc, xBottomSmallCenter - Rsmall, yBottomSmallCenter - Rsmall, xBottomSmallCenter + Rsmall, yBottomSmallCenter + Rsmall, xBottomSmallCenter, yBottomSmallCenter - Rsmall, xBottomSmallCenter, yBottomSmallCenter + Rsmall);;
  70.                
  71.                 //小半圆的直径显示出来了,再贴一条白线盖住
  72.                 for (size_t i = yBottomSmallCenter - Rsmall; i <= yBottomSmallCenter + Rsmall; i++)
  73.                 {
  74.                         SetPixel(hdc, xBottomSmallCenter - 1, i, RGB(255, 255, 255));//为什么这里要减一?我发现如果不减一白线就盖不住半圆的半径了,这是为什么?
  75.                 }
  76.                
  77.                 ReleaseDC(hwnd, hdc);
  78.                 return 0;
  79.         case WM_SIZE:
  80.                 cxClient = LOWORD(lparam);
  81.                 cyClient = HIWORD(lparam);

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

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

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

  95.                 //设置下方小圆圆心坐标(应该是半圆)
  96.                 xBottomSmallCenter = xBigCenter;
  97.                 yBottomSmallCenter = yBigCenter + Rbig / 2;
  98.                 return 0;
  99.         case WM_DESTROY://接受WM_DESTROY消息并发送WM_QUIT消息
  100.                 PostQuitMessage(0);
  101.                 return 0;
  102.         }


  103.         return DefWindowProc(hwnd, message, wparam, lparam);
  104. }
复制代码
问题:SetPixel(hdc, xBottomSmallCenter - 1, i, RGB(255, 255, 255));为什么这里要减一?我发现如果不减一白线就盖不住半圆的半径了,这是为什么?
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-8-2 13:36:08 | 显示全部楼层
哈哈,这个问题就像是在说:“我想给我的半圆添点时尚,但我的白线总是缺了一角,怎么办呢?”

在你提到的这段代码中,`SetPixel` 函数的 `xBottomSmallCenter - 1` 实际上是为了让白线看起来完美地覆盖住下方小半圆的边缘。我们来分析一下:

1. **坐标系统**:在 GDI(图形设备接口)中,坐标系的原点(0,0)位于窗口的左上角,向右和向下分别是 X 和 Y 坐标的增加。因此,所有的绘图操作都是基于这个坐标系统进行的。

2. **像素大小**:当你绘制一个半圆的时候,实际上它的边缘是由多个像素组成的。如果你直接在边缘上绘制白线,可能因为某些像素的抗锯齿效果或其他原因,导致白线未能完全覆盖底色或者未能准确地对齐。

3. **减一的必要性**:通过将 `xBottomSmallCenter - 1` 的值传给 `SetPixel`,你实际上是将白线绘制在了比小半圆边缘更内部的位置。这确保了白线在视觉上能够完全覆盖半圆的边缘,避免出现“羞涩”的情况——即白线不敢靠近半圆的边缘。

所以,总结一下,减一是为了确保白线能在视觉上完美覆盖半圆边缘,避免任何不必要的“空隙”。这也像是在给绘图加了一层“时尚”的滤镜,让它更华丽!

希望这个解释帮助你理解了为什么要减一!如果你还有什么问题,随时问我哦!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-4 21:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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