C++ GDI+ 如何绘制带边框半径的矩形

2023-12-27

这就是我使用 GDI+ 绘制矩形的方法。

Graphics g(hdc);

SolidBrush blueColor((Color(255, 74, 134, 232)));                   
g.FillRectangle(&blueColor, x, y, width, height);
DeleteObject(&blueColor); 

现在我想添加一些border-radius, 我怎样才能做到这一点?


您可以通过以下方式实现该功能图形路径::添加弧 https://learn.microsoft.com/en-us/windows/win32/api/gdipluspath/nf-gdipluspath-graphicspath-addarc(real_real_real_real_real_real)以及相关的API。

一些对我有用的代码:

void GetRoundRectPath(GraphicsPath *pPath, Rect r, int dia)
{
    // diameter can't exceed width or height
    if(dia > r.Width)    dia = r.Width;
    if(dia > r.Height)    dia = r.Height;

    // define a corner 
    Rect Corner(r.X, r.Y, dia, dia);

    // begin path
    pPath->Reset();

    // top left
    pPath->AddArc(Corner, 180, 90);    

    // tweak needed for radius of 10 (dia of 20)
    if(dia == 20)
    {
        Corner.Width += 1; 
        Corner.Height += 1; 
        r.Width -=1; r.Height -= 1;
    }

    // top right
    Corner.X += (r.Width - dia - 1);
    pPath->AddArc(Corner, 270, 90);    
    
    // bottom right
    Corner.Y += (r.Height - dia - 1);
    pPath->AddArc(Corner,   0, 90);    
    
    // bottom left
    Corner.X -= (r.Width - dia - 1);
    pPath->AddArc(Corner,  90, 90);

    // end path
    pPath->CloseFigure();
}

void DrawRoundRect(Graphics* pGraphics, Rect r,  Color color, int radius, int width)
{
    int dia = 2*radius;

    // set to pixel mode
    int oldPageUnit = pGraphics->SetPageUnit(UnitPixel);

    // define the pen
    Pen pen(color, 1);    
    pen.SetAlignment(PenAlignmentCenter);

    // get the corner path
    GraphicsPath path;

    // get path
    GetRoundRectPath(&path, r, dia);

    // draw the round rect
    pGraphics->DrawPath(&pen, &path);

    // if width > 1
    for(int i=1; i<width; i++)
    {
        // left stroke
        r.Inflate(-1, 0);
        // get the path
        GetRoundRectPath(&path, r, dia);
            
        // draw the round rect
        pGraphics->DrawPath(&pen, &path);

        // up stroke
        r.Inflate(0, -1);

        // get the path
        GetRoundRectPath(&path, r, dia);
            
        // draw the round rect
        pGraphics->DrawPath(&pen, &path);
    }

    // restore page unit
    pGraphics->SetPageUnit((Unit)oldPageUnit);
}

更多参考:圆角矩形 https://social.msdn.microsoft.com/Forums/en-US/68d409ee-af69-475e-aeae-cdbf82313b1a/roundrect?forum=vcmfcatl, WINAPI 带有自定义边框的编辑控件 https://stackoverflow.com/questions/28684334/winapi-edit-control-with-custom-border

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++ GDI+ 如何绘制带边框半径的矩形 的相关文章

随机推荐

  • C#.NET - 如何让 typeof() 与继承一起使用?

    我将首先用代码解释我的场景 public class A public class B A public class C B public class D public class Test private A a new A privat
  • 在指令中使用 $compile 会触发 AngularJS 无限摘要错误

    关于为什么该指令会触发无限摘要错误有什么想法吗 http jsfiddle net smithkl42 cwrgLd0L 13 http jsfiddle net smithkl42 cwrgLd0L 13 var App angular
  • Compose Spacer 与视图填充性能

    我更喜欢使用 Spacer 在视图之间添加一些填充 有时您可以将此空间添加为视图的填充 所以我的问题是 使用 Spacers 与使用旧的填充值相比是否存在性能缺陷 性能会有所不同 在下面的测试中 50 个项目使用填充进行渲染 随后 50 个
  • 使用 XML 文件中的数据生成 Word 文档 (docx) / 基于模板将 XML 转换为 Word 文档

    我有一个 XML 文件 其中包含需要填充到 Word 文档中的数据 我需要找到一种方法来定义一个模板 该模板可以用作从 XML 文件填充数据并创建输出文档的基线 我相信有两种方法可以做到这一点 创建一个将作为 模板 的 XSLT 文件 并使
  • Oracle REGEXP_REPLACE 大写替换字符串

    我试图将我的 reg 表达式中的替换字符串大写 但没有成功 SELECT regexp replace src i uie v2 js uie v2 upper 1 from dual returns src i uie v2 js 我知道
  • CountDownLatch 的latch.await() 方法与Thread.join() 方法

    我看到一位 stackoverflow 成员建议使用 Thread join 让 主 线程等待 2 个 任务 线程完成 我经常会做一些不同的事情 如下所示 我想知道我的方法是否有任何问题 final CountDownLatch latch
  • 禁用 cookie 时会话还能工作吗?

    如果用户在浏览器中禁用了 cookie 会话还能工作吗 因为我知道当我创建会话时客户端中有一个会话 cookie 会话 ID 可以附加到 URL 所以 是的 他们可以 查看here http www webmasterworld com f
  • 在 OpenGL 中旋转三角形

    我正在尝试围绕其中心点旋转三角形 我知道 OpenGL 围绕原点旋转 因此我需要将中间点平移到原点 然后旋转 然后平移回来 我已经注释掉了最后一行 以确保它至少绕原点的中心旋转 它不是 尽管进行了翻译 但它似乎围绕其旧原点旋转 请注意 cc
  • Java ConcurrentHashMap 中增加分区数量的缺点?

    Java ConcurrentHashMap 在内部维护分区 每个分区可以单独加锁 在某些情况下 多个线程访问的所有键都落入同一分区 而分区可能没有帮助 进一步增加分区数量应该会提高并发性 为什么 Java 为分区计数提供默认值 16 而不
  • 如何检查是否支持naturalWidth?

    我有以下 jQuery 并想测试是否支持naturalWidth function special image if typeof this naturalWidth undefined do something 但这似乎不起作用 有任何想
  • 以日期时间字符串作为 x 值的等值线图

    我正在尝试生成一个颜色等值线图 其中 x 轴显示时间 y 轴深度 z 值显示温度 时间给出如下 2011 01 01 00 01 i e Y m d H M 有没有一种方法可以从中生成颜色等高线图 并使用 filled contour Ti
  • 更新已安装包中的数据集

    是否可以更新本地已安装软件包中的数据集 我维护的包有一个基于定期更新数据的数据集 我想更新数据集的本地版本并将更改保存回包中 以便下次加载数据时 即data xxx 将加载数据集的更新版本 从中长期来看 我将更新软件包 然后将新版本上传到
  • 如何返回带有错误消息或异常的 NotFound() IHttpActionResult?

    我正在返回 NotFoundIHttpActionResult 当我的 WebApi GET 操作中找不到某些内容时 除了此响应之外 我还想发送自定义消息和 或异常消息 如果有 目前的ApiController s NotFound 方法不
  • Python浮点舍入错误[重复]

    这个问题在这里已经有答案了 使用列表理解表达式时 x 0 1 for x in range 0 5 我希望得到这样的列表 0 0 0 1 0 2 0 3 0 4 然而我却得到了这个 0 0 0 1 0 2 0 300000000000000
  • 如何接收流式传输的 HTTP 响应

    当使用 Go 抛出 HTTP 请求并接收响应时 考虑到 ResponseBody 很大 1 GB 或更多 的情况 我希望在流式传输时接收响应 resp err http Client Do req 在这种情况下 如果正文很大 我无法读取标题
  • 可可应用程序的卸载程序

    我使用 PackageMaker 作为我的应用程序的安装程序 这不仅仅是一个简单的捆绑包 我想知道如何创建卸载程序 在哪里安装它以及如何向用户提供启动它的方式 在此先感谢您的帮助 在为某些 MAC 操作系统应用程序实现卸载程序时 我们想到了
  • 如何管理 Feign 错误?

    我们正在使用弹簧启动 with 春云 and Spring cloud Netflix with Spring cloud feign 我们正在创建我们的网关应用程序 它的帮助是Feign将尝试与我们沟通authentication微服务以
  • JSON 到 Java 类

    有没有一种简单的方法可以通过 android API 将数据从 JSON 映射到我的类的字段 JSON email email password pass 我的课 class Credentials string email string
  • 了解自适应龙格库塔积分器的局部截断误差

    我正在实现一个 RKF4 5 积分器 我无法确定我的代码是否正常工作 并且我不明白本地截断错误 或者我的代码是否无法正常工作 对于代码块的大小 我深表歉意 但在这种情况下 最小可重现示例相当大 import numpy as np def
  • C++ GDI+ 如何绘制带边框半径的矩形

    这就是我使用 GDI 绘制矩形的方法 Graphics g hdc SolidBrush blueColor Color 255 74 134 232 g FillRectangle blueColor x y width height D