C语言圆角矩形画法,GDI+ 如何绘制圆角矩形(vc++)

圆角矩形在GDI+中没有现成的函数,所以我们需要自行绘制,采用路径绘制方法,我已经封装成类

效果图

0818b9ca8b590ca3270a3433284dd417.png

使用方法:

1、创建路径对象

CGraphicsRoundRectPath RoundRectPath;//创建圆角矩形路径对象2、添加矩形区域到路径

RoundRectPath.AddRoundRect(rect.X,rect.Y,rect.Width,rect.Height,5,5);

3、使用Graphics绘制路径

Graphics g;

g.DrawPath(&myPen,&RoundRectPath);

g.FillPath(&SolidBrush(Color(0,0,0)),&RoundRectPath);

头文件

#pragma once

#include

class CGraphicsRoundRectPath: public GraphicsPath

{

public:

CGraphicsRoundRectPath();

CGraphicsRoundRectPath(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY);

public:

void AddRoundRect(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY);

};

CPP文件

#include "StdAfx.h"

#include "GraphicsRoundRect.h"

CGraphicsRoundRectPath::CGraphicsRoundRectPath(void)

:Gdiplus::GraphicsPath()

{

}

CGraphicsRoundRectPath::CGraphicsRoundRectPath(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY)

:Gdiplus::GraphicsPath()

{

AddRoundRect(x,y,width,height,cornerX,cornerY);

}

void CGraphicsRoundRectPath::AddRoundRect(INT x, INT y, INT width, INT height, INT cornerX, INT cornerY)

{

INT elWid = 2*cornerX;

INT elHei = 2*cornerY;

AddArc(x,y,elWid,elHei,180,90); // 左上角圆弧

AddLine(x+cornerX,y,x+width-cornerX,y); // 上边

AddArc(x+width-elWid,y, elWid,elHei,270,90); // 右上角圆弧

AddLine(x+width,y+cornerY, x+width,y+height-cornerY);// 右边

AddArc(x+width-elWid,y+height-elHei, elWid,elHei,0,90); // 右下角圆弧

AddLine(x+width-cornerX,y+height, x+cornerX,y+height); // 下边

AddArc(x,y+height-elHei, elWid,elHei,90,90);

AddLine(x,y+cornerY, x, y+height-cornerY);

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
圆角矩形渐变可以通过以下步骤实现: 1. 画一个填充了渐变色的矩形。 2. 剪切出圆角区域,可以通过使用路径和剪切函数来实现。 3. 在剪切出的圆角区域内使用渐变色填充函数,以实现圆角矩形渐变。 以下是一个简单的圆角矩形渐变算法示例,使用C语言和SDL库: ```c #include <SDL.h> void draw_rounded_gradient_rect(SDL_Renderer *renderer, int x, int y, int w, int h, int radius, SDL_Color start_color, SDL_Color end_color) { // 画一个渐变色矩形 SDL_Rect rect = { x, y, w, h }; SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawColor(renderer, start_color.r, start_color.g, start_color.b, start_color.a); SDL_RenderFillRect(renderer, &rect); SDL_SetRenderDrawColor(renderer, end_color.r, end_color.g, end_color.b, end_color.a); SDL_RenderDrawRect(renderer, &rect); // 剪切出圆角区域 SDL_Rect clip_rect = { x, y, w, h }; SDL_RenderSetClipRect(renderer, &clip_rect); SDL_RenderDrawLine(renderer, x + radius, y, x + w - radius, y); SDL_RenderDrawLine(renderer, x + radius, y + h, x + w - radius, y + h); SDL_RenderDrawLine(renderer, x, y + radius, x, y + h - radius); SDL_RenderDrawLine(renderer, x + w, y + radius, x + w, y + h - radius); const int diameter = radius * 2; int cx = x + radius; int cy = y + radius; for (int i = 0; i < 4; i++) { int ax = (i == 1 || i == 2) ? x + w - diameter : x; int ay = (i == 2 || i == 3) ? y + h - diameter : y; SDL_RenderDrawLine(renderer, cx, ay, cx, ay + diameter); SDL_RenderDrawLine(renderer, ax, cy, ax + diameter, cy); int dx = ax + ((i == 1 || i == 2) ? radius : diameter - radius); int dy = ay + ((i == 2 || i == 3) ? radius : diameter - radius); SDL_RenderDrawArc(renderer, dx, dy, radius, i * 90, (i + 1) * 90); } // 在圆角区域内使用渐变色填充函数 SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); SDL_SetRenderDrawColor(renderer, start_color.r, start_color.g, start_color.b, start_color.a); for (int i = 0; i < radius; i++) { float t = (float)i / radius; SDL_Color color = { start_color.r + (end_color.r - start_color.r) * t, start_color.g + (end_color.g - start_color.g) * t, start_color.b + (end_color.b - start_color.b) * t, start_color.a + (end_color.a - start_color.a) * t }; int dx = cx - i; int dy = cy - i; int dw = i * 2; int dh = i * 2; SDL_Rect gradient_rect = { dx, dy, dw, dh }; SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); SDL_RenderFillRect(renderer, &gradient_rect); } } ``` 需要注意的是,该算法仅演示了如何实现圆角矩形渐变,如果需要更加复杂的效果,可以根据实际需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值