首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

请教一个圆角矩形的有关问题

2012-02-02 
请问一个圆角矩形的问题。这是一个获取圆角的的函数:PublicSharedFunctionget圆角矩形(ByValrecAsRectangle

请问一个圆角矩形的问题。
这是一个获取圆角的的函数:
        Public   Shared   Function   get圆角矩形(ByVal   rec   As   Rectangle,   ByVal   r   As   Integer)   As   Drawing2D.GraphicsPath
                Dim   gp   As   New   Drawing2D.GraphicsPath
                gp.AddLine(rec.Left   +   r,   rec.Top,   rec.Right   -   r,   rec.Top)   '上
                gp.AddArc(rec.Right   -   r   *   2,   rec.Top,   r   *   2,   r   *   2,   270,   90)   '右上
                gp.AddLine(rec.Right,   rec.Top   +   r,   rec.Right,   rec.Bottom   -   r)   '右
                gp.AddArc(rec.Right   -   r   *   2,   rec.Bottom   -   r   *   2,   r   *   2,   r   *   2,   0,   90)   '右下
                gp.AddLine(rec.Right   -   r,   rec.Bottom,   rec.Left   +   r,   rec.Bottom)   '下
                gp.AddArc(rec.Left,   rec.Bottom   -   r   *   2,   r   *   2,   r   *   2,   90,   90)   '左下
                gp.AddLine(rec.Left,   rec.Bottom   -   r,   rec.Left,   rec.Top   +   r)   '左
                gp.AddArc(rec.Left,   rec.Top,   r   *   2,   r   *   2,   180,   90)   '左上
                gp.CloseFigure()

                Return   gp

使用时:
        Protected   Overrides   Sub   OnPaint(ByVal   e   As   System.Windows.Forms.PaintEventArgs)
                MyBase.OnPaint(e)

                Dim   rec   As   New   Rectangle(20,   20,   100,   60)
                Dim   gp   As   Drawing2D.GraphicsPath   =   Form1.get圆角矩形(rec,   4)
                e.Graphics.FillPath(Brushes.Red,   gp)

        End   Sub


四个角的大小并不一致,特别是当半径小于6左右时,很明显。
请高手指点一下好吗?


[解决办法]
使用 点 的误差问题吧



[解决办法]
误差,没办法,就像在特定分辨率下的圆永远化不圆(不连续,类似下面的效果)
--
-- --
-- --


Public Shared Function get圆角矩形(ByVal rec As Rectangle, ByVal r As Integer) As Drawing2D.GraphicsPath
Dim gp As New Drawing2D.GraphicsPath
gp.AddLine(rec.Left + r, rec.Top, rec.Right - r, rec.Top) '上
gp.AddArc(rec.Right - r * 2, rec.Top, r * 2, r * 2, 270, 90) '右上
gp.AddLine(rec.Right, rec.Top + r, rec.Right, rec.Bottom - r) '右
gp.AddArc(rec.Right - r * 2, rec.Bottom - r * 2, r * 2, r * 2, 0, 90) '右下
gp.AddLine(rec.Right - r, rec.Bottom, rec.Left + r, rec.Bottom) '下


gp.AddArc(rec.Left, rec.Bottom - r * 2, r * 2, r * 2, 90, 90) '左下
gp.AddLine(rec.Left, rec.Bottom - r, rec.Left, rec.Top + r) '左
gp.AddArc(rec.Left, rec.Top, r * 2, r * 2, 180, 90) '左上
gp.CloseFigure()

Return gp

End Function

Private Function CreateRoundedRectPath(ByVal rect As Rectangle, ByVal radius As Integer) As System.Drawing.Drawing2D.GraphicsPath
Dim RoundRect As New System.Drawing.Drawing2D.GraphicsPath

RoundRect.AddLine(rect.Left + radius - 2, rect.Top - 1, rect.Right - radius, rect.Top - 1) '顶端
RoundRect.AddArc(rect.Right - radius, rect.Top - 1, radius, radius, 270, 90) '右上角
RoundRect.AddLine(rect.Right, rect.Top + radius, rect.Right, rect.Bottom - radius) '右边
RoundRect.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90) '右下角
RoundRect.AddLine(rect.Right - radius, rect.Bottom, rect.Left + radius, rect.Bottom) '底边
RoundRect.AddArc(rect.Left - 1, rect.Bottom - radius, radius, radius, 90, 90) '左下角
RoundRect.AddLine(rect.Left - 1, rect.Top + radius, rect.Left - 1, rect.Bottom - radius) '左边
RoundRect.AddArc(rect.Left - 1, rect.Top - 1, radius, radius, 180, 90) '左上角

Return RoundRect
End Function


Private Function GetRoundedRectPath(ByVal rect As Rectangle, ByVal radius As Integer) As System.Drawing.Drawing2D.GraphicsPath
rect.Offset(-1, -1)
Dim RoundRect As New Rectangle(rect.Location, New Size(radius - 1, radius - 1))
Dim path As New System.Drawing.Drawing2D.GraphicsPath

path.AddArc(RoundRect, 180, 90) '左上角

RoundRect.X = rect.Right - radius '右上角
path.AddArc(RoundRect, 270, 90)

RoundRect.Y = rect.Bottom - radius '右下角
path.AddArc(RoundRect, 0, 90)

RoundRect.X = rect.Left '左下角
path.AddArc(RoundRect, 90, 90)

path.CloseFigure()

Return path
End Function


Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
MyBase.OnPaint(e)

Dim rec As New Rectangle(20, 20, 100, 60)
Dim rec1 As New Rectangle(20, 100, 100, 60)
Dim rec2 As New Rectangle(20, 180, 100, 60)
Dim gp As Drawing2D.GraphicsPath = get圆角矩形(rec, 5)
Dim gp1 As Drawing2D.GraphicsPath = GetRoundedRectPath(rec1, 10)
Dim gp2 As Drawing2D.GraphicsPath = CreateRoundedRectPath(rec2, 10)
e.Graphics.FillPath(Brushes.Red, gp)
e.Graphics.FillPath(Brushes.Red, gp1)
e.Graphics.FillPath(Brushes.Red, gp2)

End Sub
[解决办法]
我也试过了,的确不圆。如果程序没问题的话那也没办法了。
建议手动的设置下几个角的偏移量吧,程序不对称就算了,看上去圆就可以了。

热点排行