码迷,mamicode.com
首页 > Windows程序 > 详细

章鱼哥出品—VB.NET 如何绘制圆角矩形,并适应窗体大小

时间:2014-11-04 11:09:10      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:vb.net   章鱼哥   绘制矩形   

Public Class Form1
    '*********************************************************************   
    '作者:章鱼哥,QQ:3107073263 群:309816713       
    '如有疑问或好的建议请联系我,大家一起进步     
    '*********************************************************************     
    '绘制圆角矩形函数
    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
    '绘制矩形
    Private Sub DrawingRect()
        Dim g As Graphics = Me.CreateGraphics '定义一个画布
        Dim Pen As New Pen(Brushes.DarkRed, 2) '定义一个画笔
        Dim Hei As Integer = Me.Height
        Dim Wid As Integer = Me.Width
        '矩形的位置和长宽随着窗体的变化而改变
        Dim Rec As New Rectangle(Int(Wid / 5), Int(Hei / 5), Int(Wid / 2), Int(Hei / 2))
        '  g.DrawRectangle(Pen, Rec)
        '清楚现有的矩形
        g.Clear(Me.BackColor)
        g.DrawPath(Pen, GetRoundedRectPath(Rec, 30))
    End Sub

    Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        DrawingRect()
    End Sub

    Private Sub Form1_SizeChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged
        Me.Invalidate() '此函数可引发Paint事件
    End Sub
End Class


原窗体绘制:
bubuko.com,布布扣
缩小窗体时:
bubuko.com,布布扣

章鱼哥出品—VB.NET 如何绘制圆角矩形,并适应窗体大小

标签:vb.net   章鱼哥   绘制矩形   

原文地址:http://blog.csdn.net/zhangyubishoulin/article/details/40780055

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!