如何:使用 OvalShape 和 RectangleShape 控件绘制形状 (Visual Studio)

更新:2007 年 11 月

可以使用 OvalShape 控件在设计时和运行时在窗体或容器上绘制圆形或椭圆形。可以使用 RectangleShape 控件在窗体或容器上绘制正方形、矩形或圆角矩形。还可以使用此控件在设计时和运行时绘制形状。

您可以通过更改边框的宽度、颜色和样式来自定义形状的外观。形状的背景默认情况下是透明的;您可以自定义背景使其显示纯色、图案、渐变填充(从一种颜色过渡到另一种颜色)或图像。

在设计时绘制简单形状

  1. 从“工具箱”的“Visual Basic PowerPacks”选项卡中将 OvalShapeRectangleShape 控件拖到窗体或容器控件中。

  2. 拖动大小调整控点和移动控点来调整形状的大小以及定位形状。

    还可以通过在“属性”窗口中更改 Size 和 Position 属性来调整形状的大小以及定位形状。

    若要创建圆角矩形,请选择“属性”窗口中的 CornerRadius 属性并将它设置为一个大于 0 的值。

  3. (可选)在“属性”窗口中,设置其他属性以更改形状的外观。

在运行时绘制简单形状

  1. 在“项目”菜单上,单击“添加引用”。

  2. 在“添加引用”对话框中选择“Microsoft.VisualBasic.PowerPacks.VS”,然后单击“确定”。

  3. 在“代码编辑器”中,将 Imports 或 using 语句添加到模块的顶部:

    Imports Microsoft.VisualBasic.PowerPacks
    
    using Microsoft.VisualBasic.PowerPacks;
    
  4. 将下面的代码添加到 Event 过程中:

    Dim canvas As New ShapeContainer
    ' To draw an oval, substitute 
    ' OvalShape for RectangleShape.
    Dim theShape As New RectangleShape
    ' Set the form as the parent of the ShapeContainer.
    canvas.Parent = Me
    ' Set the ShapeContainer as the parent of the Shape.
    theShape.Parent = canvas
    ' Set the size of the shape.
    theShape.Size = New System.Drawing.Size(200, 300)
    ' Set the location of the shape.
    theShape.Location = New System.Drawing.Point(100, 100)
    ' To draw a rounded rectangle, add the following code:
    theShape.CornerRadius = 12
    
    ShapeContainer canvas = new ShapeContainer();
    // To draw an oval, substitute 
    // OvalShape for RectangleShape.
    RectangleShape theShape = new RectangleShape();
    // Set the form as the parent of the ShapeContainer.
    canvas.Parent = this;
    // Set the ShapeContainer as the parent of the Shape.
    theShape.Parent = canvas;
    // Set the size of the shape.
    theShape.Size = new System.Drawing.Size(200, 300);
    // Set the location of the shape.
    theShape.Location = new System.Drawing.Point(100, 100);
    // To draw a rounded rectangle, add the following code:
    theShape.CornerRadius = 12;
    

自定义形状

使用默认设置时,OvalShapeRectangleShape 控件将显示一个纯黑色边框,此边框宽为一个像素且背景透明。您可以通过设置属性来更改边框的宽度、样式和颜色。通过其他属性可以将形状的背景改为纯色、图案、渐变填充或图像。

在更改形状背景之前,您应该了解这几个属性是如何相互影响的。

绘制具有自定义边框的圆形

  1. 从“工具箱”的“Visual Basic PowerPacks”选项卡中将 OvalShape 控件拖到窗体或容器控件中。

  2. 在“属性”窗口中,将 Size 属性的 Height 和 Width 设置为相等的值。

  3. 将 BorderColor 属性设置为所需颜色。

  4. 将 BorderStyle 属性设置为 Solid 以外的其他任意值。

  5. 将 BorderWidth 设置为所需的大小(以像素为单位)。

绘制具有纯色填充的圆形

  1. 从“工具箱”的“Visual Basic PowerPacks”选项卡中将 OvalShape 控件拖到窗体或容器控件中。

  2. 在“属性”窗口中,将 Size 属性的 Height 和 Width 设置为相等的值。

  3. 将 BackColor 属性设置为所需颜色。

  4. 将 BackStyle 属性设置为 Opaque。

绘制具有图案填充的圆形

  1. 从“工具箱”的“Visual Basic PowerPacks”选项卡中将 OvalShape 控件拖到窗体或容器控件中。

  2. 在“属性”窗口中,将 Size 属性的 Height 和 Width 设置为相等的值。

  3. 将 BackColor 属性设置为所需的背景色。

  4. 将 BackStyle 属性设置为 Opaque。

  5. 将 FillColor 属性设置为所需的图案颜色。

  6. 将 FillStyle 属性设置为 Transparent 或 Solid 以外的其他任意值。

绘制具有渐变填充的圆形

  1. 从“工具箱”的“Visual Basic PowerPacks”选项卡中将 OvalShape 控件拖到窗体或容器控件中。

  2. 在“属性”窗口中,将 Size 属性的 Height 和 Width 设置为相等的值。

  3. 将 FillColor 属性设置为所需的起始颜色。

  4. 将 FillGradientColor 属性设置为所需的结束颜色。

  5. 将 FillGradientStyle 属性设置为 None 以外的其他值。

绘制以图像填充的圆形

  1. 从“工具箱”的“Visual Basic PowerPacks”选项卡中将 OvalShape 控件拖到窗体或容器控件中。

  2. 在“属性”窗口中,将 Size 属性的 Height 和 Width 设置为相等的值。

  3. 选择 BackgroundImage 属性,然后单击省略号(“...”)按钮。

  4. 在“选择资源”对话框中选择要显示的图像。如果没有列出图像资源,则单击“导入”浏览到图像所在的位置。

  5. 单击“确定”插入图像。

请参见

任务

如何:使用 LineShape 控件绘制直线 (Visual Studio)

概念

Line 和 Shape 控件简介 (Visual Studio)

参考

OvalShape

RectangleShape

修订记录

日期

修订

原因

2008 年 7 月

新增主题。

SP1 功能更改。