SolidRoundRect在C语言中如何实现?

99ANYc3cd6
预计阅读时长 35 分钟
位置: 首页 C语言 正文

C 语言标准库(C Standard Library)本身并没有直接提供名为 solidroundrect 的函数。

solidroundrect c语言
(图片来源网络,侵删)

这个函数名听起来非常像 Borland C++ 或 Turbo C++ 等老式图形库(如 graphics.h)中的函数,在现代标准 C 语言中,我们需要自己实现这个功能,或者使用更现代的图形库。

下面我将为你提供几种实现方式,从最经典的 graphics.h 到现代的跨平台解决方案。


使用经典的 graphics.h 库(适用于 DOS 或 Windows 控制台环境)

这是 solidroundrect 函数最可能出现的“老家”,这个库主要用于 DOS 和早期的 Windows 应用程序开发。

函数原型

graphics.h 中,函数原型通常是:

solidroundrect c语言
(图片来源网络,侵删)
void far solidroundrect(int left, int top, int right, int bottom, int xradius, int yradius);

参数说明

  • left, top: 圆角矩形左上角的坐标。
  • right, bottom: 圆角矩形右下角的坐标。
  • xradius: 圆角在水平方向上的半径。
  • yradius: 圆角在垂直方向上的半径。

完整示例代码

这个示例代码只能在支持 graphics.h 的旧编译器(如 Turbo C++)或通过特殊方式(如 WinBGI)在现代系统上运行的编译器中编译。

#include <graphics.h>
#include <conio.h>
#include <dos.h>
int main() {
    // 初始化图形驱动
    int gd = DETECT, gm;
    initgraph(&gd, &gm, "C:\\TC\\BGI"); // 请确保路径正确
    // 设置填充样式和颜色
    setfillstyle(SOLID_FILL, BLUE);
    setcolor(WHITE);
    // 绘制一个实心圆角矩形
    // 参数: 左上角(100, 100), 右下角(500, 300), x半径=50, y半径=30
    solidroundrect(100, 100, 500, 300, 50, 30);
    // 在矩形内添加文字
    setcolor(WHITE);
    outtextxy(250, 200, "Hello, Solid Round Rect!");
    getch(); // 等待用户按键
    closegraph(); // 关闭图形模式
    return 0;
}

注意:在现代 Windows 系统上直接编译这个代码几乎肯定会失败,因为 graphics.h 不是标准库,你需要使用像 WinBGI 这样的兼容层,或者使用其他现代图形库。


使用现代图形库(推荐)

对于任何现代 C/C++ 项目,我们都应该使用更强大、更活跃的图形库,下面介绍两个非常流行的选择:SFMLSDL2

1 使用 SFML (Simple and Fast Multimedia Library)

SFML 是一个面向对象的 C++ 库,但它也提供了 C 风格的 API,非常适合 C 语言。

solidroundrect c语言
(图片来源网络,侵删)

安装 你需要从 SFML 官网 下载库文件,并配置你的编译器(如 MinGW, MSVC, GCC)。

C 语言示例代码 SFML 的 C API 需要包含 sfml-graphics.h 等头文件。

#include <SFML/Graphics.h>
int main() {
    // 创建主窗口
    sfRenderWindow* window = sfRenderWindow_create(
        (sfVideoMode){800, 600, 32}, "C Solid Round Rect (SFML)",
        sfTitlebar | sfClose, NULL
    );
    // 创建一个圆角矩形形状
    sfShape* roundRect = sfShape_create();
    // 定义矩形的顶点和圆角
    // 参数: x, y, corner_radius, point_count, ...
    sfShape_setPointCount(roundRect, 9); // 4个直角 + 4个圆角弧线的中心点 + 1个重复的起始点
    // 定义矩形的边界
    float left = 200.0f, top = 150.0f;
    float width = 400.0f, height = 300.0f;
    float radius = 50.0f;
    // 设置9个控制点来定义圆角矩形
    // 顺序: 左上角弧线中心 -> 左上角顶点 -> 左上角弧线中心 -> ... (顺时针)
    sfShape_setPoint(roundRect, 0, left + radius, top); // 上边中点
    sfShape_setPoint(roundRect, 1, left + radius, top + radius); // 左上角内点
    sfShape_setPoint(roundRect, 2, left, top + radius); // 左上角顶点
    sfShape_setPoint(roundRect, 3, left, top + height - radius); // 左下角内点
    sfShape_setPoint(roundRect, 4, left + radius, top + height); // 下边中点
    sfShape_setPoint(roundRect, 5, left + width - radius, top + height); // 下边中点
    sfShape_setPoint(roundRect, 6, left + width, top + height - radius); // 右下角内点
    sfShape_setPoint(roundRect, 7, left + width, top + radius); // 右上角内点
    sfShape_setPoint(roundRect, 8, left + width - radius, top); // 上边中点
    // 设置填充颜色
    sfShape_setFillColor(roundRect, sfBlue);
    // 设置轮廓颜色
    sfShape_setOutlineColor(roundRect, sfWhite);
    sfShape_setOutlineThickness(roundRect, 2.0f);
    // 主循环
    while (sfRenderWindow_isOpen(window)) {
        sfEvent event;
        while (sfRenderWindow_pollEvent(window, &event)) {
            if (event.type == sfEvtClosed) {
                sfRenderWindow_close(window);
            }
        }
        sfRenderWindow_clear(window, sfBlack);
        sfRenderWindow_drawShape(window, roundRect, NULL);
        sfRenderWindow_display(window);
    }
    // 清理资源
    sfShape_destroy(roundRect);
    sfRenderWindow_destroy(window);
    return 0;
}

编译命令 (Linux/macOS with g++):

gcc -o sfml_app main.c -lsfml-graphics -lsfml-window -lsfml-system

2 使用 SDL2 (Simple DirectMedia Layer)

SDL2 是一个更底层的多媒体库,广泛用于游戏开发,它本身不直接提供圆角矩形函数,但我们可以利用其纹理渲染功能轻松实现。

安装SDL 官网 下载库。

C 语言示例代码 这个思路是:

  1. 创建一个离屏的 SDL surface。
  2. 在这个 surface 上绘制一个圆角矩形。
  3. 将这个 surface 转换为一个 texture。
  4. 在主窗口上渲染这个 texture。
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h> // 用于加载图片,这里我们用它来创建 texture
#define WIDTH 800
#define HEIGHT 600
// 绘制圆角矩形到 surface
void draw_round_rect(SDL_Surface* surface, SDL_Rect rect, int radius, Uint32 color) {
    // 直线部分
    SDL_FillRect(surface, &(SDL_Rect){rect.x + radius, rect.y, rect.w - 2 * radius, rect.h}, color);
    SDL_FillRect(surface, &(SDL_Rect){rect.x, rect.y + radius, rect.w, rect.h - 2 * radius}, color);
    // 四个圆角部分
    for (int y = 0; y < radius; y++) {
        for (int x = 0; x < radius; x++) {
            int dist_sq = x * x + y * y;
            if (dist_sq <= radius * radius) {
                // 左上角
                SDL_FillRect(surface, &(SDL_Rect){rect.x + radius - 1 - x, rect.y + radius - 1 - y, 1, 1}, color);
                // 右上角
                SDL_FillRect(surface, &(SDL_Rect){rect.x + rect.w - radius + x, rect.y + radius - 1 - y, 1, 1}, color);
                // 左下角
                SDL_FillRect(surface, &(SDL_Rect){rect.x + radius - 1 - x, rect.y + rect.h - radius + y, 1, 1}, color);
                // 右下角
                SDL_FillRect(surface, &(SDL_Rect){rect.x + rect.w - radius + x, rect.y + rect.h - radius + y, 1, 1}, color);
            }
        }
    }
}
int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_VIDEO);
    IMG_Init(IMG_INIT_PNG);
    SDL_Window* window = SDL_CreateWindow("C Solid Round Rect (SDL)",
                                          SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                                          WIDTH, HEIGHT, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
    // 定义圆角矩形参数
    SDL_Rect rect = {200, 150, 400, 300};
    int radius = 50;
    SDL_Color color = {0, 0, 255, 255}; // 蓝色
    // 创建一个离屏 surface 来绘制圆角矩形
    SDL_Surface* roundRectSurface = SDL_CreateRGBSurface(0, rect.w, rect.h, 32, 0, 0, 0, 0);
    draw_round_rect(roundRectSurface, (SDL_Rect){0, 0, rect.w, rect.h}, radius, SDL_MapRGBA(roundRectSurface->format, color.r, color.g, color.b, color.a));
    // 将 surface 转换为 texture
    SDL_Texture* roundRectTexture = SDL_CreateTextureFromSurface(renderer, roundRectSurface);
    SDL_FreeSurface(roundRectSurface); // surface 不再需要,可以释放
    int running = 1;
    SDL_Event event;
    while (running) {
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = 0;
            }
        }
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // 清屏为黑色
        SDL_RenderClear(renderer);
        // 渲染圆角矩形纹理
        SDL_RenderCopy(renderer, roundRectTexture, NULL, &rect);
        SDL_RenderPresent(renderer);
        SDL_Delay(16); // ~60 FPS
    }
    // 清理
    SDL_DestroyTexture(roundRectTexture);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    IMG_Quit();
    SDL_Quit();
    return 0;
}

编译命令 (Linux/macOS with gcc):

gcc -o sdl_app main.c -lSDL2 -lSDL2_image

纯 C 语言实现(控制台输出)

如果你只是想在控制台(终端)中用字符画出一个“看起来像”圆角矩形的东西,这也是可行的。

#include <stdio.h>
#include <math.h>
void print_round_rect(int width, int height, int corner_radius) {
    if (width < 2 * corner_radius || height < 2 * corner_radius) {
        printf("Dimensions too small for the corner radius.\n");
        return;
    }
    // 上边框
    printf("┌");
    for (int i = 0; i < width - 2 * corner_radius; i++) {
        printf("─");
    }
    printf("┐\n");
    // 中间部分
    for (int row = 1; row < height - 1; row++) {
        printf("│");
        // 计算当前行是否在圆角区域
        int in_corner_top = row < corner_radius;
        int in_corner_bottom = row >= height - corner_radius;
        if (in_corner_top || in_corner_bottom) {
            // 圆角区域
            double y_center = in_corner_top ? (corner_radius - 0.5) : (height - corner_radius - 0.5);
            double dist_from_center = fabs(row - y_center);
            for (int col = 1; col < width - 1; col++) {
                double x_center = (col < corner_radius) ? (corner_radius - 0.5) : (width - corner_radius - 0.5);
                double dist_from_x_center = fabs(col - x_center);
                // 简化的圆角判断
                if (dist_from_center + dist_from_x_center < corner_radius - 0.5) {
                    printf(" ");
                } else {
                    printf(" ");
                }
            }
        } else {
            // 直线区域
            for (int i = 1; i < width - 1; i++) {
                printf(" ");
            }
        }
        printf("│\n");
    }
    // 下边框
    printf("└");
    for (int i = 0; i < width - 2 * corner_radius; i++) {
        printf("─");
    }
    printf("┘\n");
}
int main() {
    printf("A simple rounded rectangle in console:\n");
    print_round_rect(30, 10, 3);
    return 0;
}

这个控制台版本非常简陋,主要用于演示,无法实现真正的“实心”填充。

方法 优点 缺点 适用场景
graphics.h 语法简单,直观 过时,平台兼容性差,仅适用于学习历史代码 学习计算机图形学历史,在旧环境开发
SFML / SDL2 现代、强大、跨平台、活跃的社区 需要额外安装库,比 graphics.h 复杂 所有现代 C/C++ 图形应用程序、游戏
纯 C 控制台 无需任何库 效果差,功能极其有限 仅用于简单的字符画,娱乐或教学

如果你想在 C 语言中绘制圆角矩形,请忘记 solidroundrect 这个名字,直接选择 SFMLSDL2 这样的现代图形库,它们能为你提供更强大、更可靠的功能。

-- 展开阅读全文 --
头像
c语言 getprocaddress
« 上一篇 2025-12-02
dede留言板回复只显示管理回复几个字
下一篇 » 2025-12-03

相关文章

取消
微信二维码
支付宝二维码

目录[+]