共用方式為


HOW TO:使用 OvalShape 和 RectangleShape 控制項繪製圖案 (Visual Studio)

更新: 2008 年 7 月

不論在設計階段或執行階段,您都可以使用 OvalShape 控制項在表單或容器上繪製圓形或橢圓形。您可以使用 RectangleShape 控制項在表單或容器上繪製正方形、矩形或圓角矩形,也可以使用這個控制項在設計階段及執行階段繪製形狀。

只要變更框線的寬度、色彩和樣式,就能自訂形狀的外觀。形狀的背景預設是透明的,您可以自訂背景來顯示純色、圖樣或漸層填滿 (由一個色彩轉換為另一個色彩) 或影像。

若要在設計階段繪製簡單形狀

  1. 從 [工具箱] 的 [Visual Basic PowerPacks] 索引標籤中,將 OvalShapeRectangleShape 控制項拖曳到表單或容器控制項。

  2. 拖曳縮放控點 (Sizing Handle) 和移動控制軸,以調整形狀的大小及位置。

    您也可以在 [屬性] 視窗中變更 [Size] 和 [Position] 屬性,以調整形狀的大小及位置。

    若要建立圓角矩形,請選取在 [屬性] 視窗中選取 [CornerRadius] 屬性,並將它的值設定為大於 0。

  3. 在 [屬性] 視窗中,選擇性地設定其他屬性來變更形狀的外觀。

若要在執行階段繪製簡單形狀

  1. 在 [專案] 功能表上,按一下 [加入參考]。

  2. 在 [加入參考] 對話方塊中,選取 [Microsoft.VisualBasic.PowerPacks.VS],然後按一下 [確定]。

  3. 在 [程式碼編輯器] 中,將 Imports 或 using 陳述式 (Statement) 加入至模組的最上方:

    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. 按一下 [確定] 以插入影像。

請參閱

工作

HOW TO:使用 LineShape 控制項繪製線條 (Visual Studio)

概念

Line 和 Shape 控制項簡介 (Visual Studio)

參考

OvalShape

RectangleShape

變更記錄

日期

記錄

原因

2008 年 7 月

加入主題。

SP1 功能變更。