鱼C论坛

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

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

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

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

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

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

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

int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
        static TCHAR szAppName[] = TEXT("MyAppWindows");
        HWND hwnd = NULL;
        MSG msg;
        WNDCLASS wc;
        wc.style = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc = WndProc;
        wc.cbClsExtra = 0;
        wc.cbWndExtra = 0;
        wc.hInstance = hInstance;
        wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wc.lpszMenuName = NULL;
        wc.lpszClassName = szAppName;

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


        hwnd = CreateWindow(szAppName, TEXT("我的窗口"),
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                CW_USEDEFAULT,
                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;
        RECT rect;
        static int cxClient, cyClient;//客户区宽度和高度
        static int Dbig,Rbig,Rsmall,Rmin;//太极图最大圆形的直径,半径,中间圆形的半径,最小圆形的半径
        static int xBigCenter, yBigCenter;//设置大圆的中心,是客户区中心
        static int xTopSmallCenter, yTopSmallCenter,xBottomSmallCenter,yBottomSmallCenter;//设置顶部小圆圆心和底部小圆圆心坐标
        static int xTopMinCenter, yTopMinCenter,xBottomMinCenter,yBottomMinCenter;//设置顶部和底部最小圆圆心的坐标
        switch (message)
        {
        case WM_PAINT:
                hdc = BeginPaint(hwnd, &ps);

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

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

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

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

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

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

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

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

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

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

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


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

评分

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

查看全部评分

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

使用道具 举报

发表于 2024-8-2 16:59:36 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-11-21 16:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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