C++ 使用Windows.h库绘制正方形函数 - square()
#include "Windows.h"\n\nvoid square(int x, int y, int z, int height, int weight, int borderColor, int fillColor)\n{\n HWND console = GetConsoleWindow();\n HDC hdc = GetDC(console);\n \n // 设置边框颜色\n HPEN borderPen = CreatePen(PS_SOLID, weight, borderColor);\n SelectObject(hdc, borderPen);\n \n // 设置填充颜色\n HBRUSH fillBrush = CreateSolidBrush(fillColor);\n SelectObject(hdc, fillBrush);\n \n // 绘制正方形\n Rectangle(hdc, x, y, x + height, y + height);\n \n // 释放资源\n DeleteObject(borderPen);\n DeleteObject(fillBrush);\n \n ReleaseDC(console, hdc);\n}\n\nint main()\n{\n // 示例用法\n square(50, 50, 200, 100, 2, RGB(255, 0, 0), RGB(0, 255, 0));\n \n return 0;\n}\n\n在这个示例中,square函数的参数包括:\n- x和y:正方形左上角的坐标\n- z:线的长度(正方形的边长)\n- height和weight:边框的高度和宽度\n- borderColor:边框的颜色,使用RGB颜色值表示\n- fillColor:填充的颜色,使用RGB颜色值表示\n\n在main函数中,示例用法调用了square函数,绘制了一个边长为100像素的正方形,边框颜色为红色,填充颜色为绿色。
原文地址: https://www.cveoy.top/t/topic/pT8B 著作权归作者所有。请勿转载和采集!