【VB6 GDI+进阶】通过拼接圆弧和线绘制圆角矩形

时间:2022-02-14 19:11:29

GDI+中没有直接绘制圆角矩形的函数。本篇将详细介绍如何通过拼接圆弧和线绘制圆角矩形,结尾附封装好的函数,可以直接调用。

1.GdipDrawArcI(绘制圆弧)

函数声明如下:

Public Declare Function GdipDrawArcI _
Lib "gdiplus" (ByVal graphics As Long, _
ByVal Pen As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal Width As Long, _
ByVal Height As Long, _
ByVal StartAngle As Single, _
ByVal sweepAngle As Single) As GpStatus

第一、第二个参数传入相应的Graphics与Pen,第三、第四个函数为圆弧所在圆的左上角XY坐标、Width和Height则为圆弧所在圆的宽和高;重要的是最后两个参数:StatrAngle是指圆弧起点与下图红线的夹角,SweepAngle则是圆弧的角度。

【VB6 GDI+进阶】通过拼接圆弧和线绘制圆角矩形

上图蓝圈为XY坐标,StarAngle为90度,SweepAngle为90度,紫色圆弧就是绘制的效果。

2.GdipDrawLineI(绘制线)

VistaSwx介绍过我就不多说了。

 

3.绘制圆角矩形

思路:四个角落绘制90度的圆弧,接着用线连接。

Private Sub DrawRoundRectangle(Graphics As Long, Pen As Long, X As Long, Y As Long, Width As Long, Height As Long, Optional RoundSize As Long = 30)
GdipDrawArcI Graphics, Pen, X, Y, RoundSize
* 2, RoundSize * 2, 180, 90
GdipDrawLineI Graphics, Pen, X
+ RoundSize, Y, X + Width - RoundSize, Y
GdipDrawArcI Graphics, Pen, X
+ Width - RoundSize * 2, Y, RoundSize * 2, RoundSize * 2, 270, 90
GdipDrawLineI Graphics, Pen, X
+ Width, Y + RoundSize, X + Width, Y + Height - RoundSize
GdipDrawArcI Graphics, Pen, X
+ Width - RoundSize * 2, Y + Height - RoundSize * 2, RoundSize * 2, RoundSize * 2, 0, 90
GdipDrawLineI Graphics, Pen, X
+ RoundSize, Y + Height, X + Width - RoundSize, Y + Height
GdipDrawArcI Graphics, Pen, X, Y
+ Height - RoundSize * 2, RoundSize * 2, RoundSize * 2, 90, 90
GdipDrawLineI Graphics, Pen, X, Y
+ RoundSize, X, Y + Height - RoundSize
End Sub

XY为圆角矩形坐标,Height width为高和宽,RoundSize为圆弧的大小。如果想填充的话把Pen改成Brush,Draw改成Fill即可,自己试试吧。