在窗体中添加一个ContextMenu1控件
Imports System.Drawing.Drawing2D Public Class Form1 Inherits System.Windows.Forms.Form #Region " Windows 窗体设计器生成的代码 " Public Sub New() MyBase.New() '该调用是 Windows 窗体设计器所必需的。 InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub '窗体重写处置以清理组件列表。 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Windows 窗体设计器所必需的 Private components As System.ComponentModel.IContainer '注意:以下过程是 Windows 窗体设计器所必需的 '可以使用 Windows 窗体设计器修改此过程。 '不要使用代码编辑器修改它。 Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form1)) Me.ContextMenu1 = New System.Windows.Forms.ContextMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.MenuItem4 = New System.Windows.Forms.MenuItem Me.MenuItem5 = New System.Windows.Forms.MenuItem Me.MenuItem6 = New System.Windows.Forms.MenuItem Me.MenuItem7 = New System.Windows.Forms.MenuItem Me.SuspendLayout() ' 'ContextMenu1 ' Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1,Me.MenuItem2,Me.MenuItem3,Me.MenuItem4,Me.MenuItem5,Me.MenuItem6,Me.MenuItem7}) ' 'MenuItem1 ' Me.MenuItem1.Index = 0 Me.MenuItem1.Text = "椭圆" ' 'MenuItem2 ' Me.MenuItem2.Index = 1 Me.MenuItem2.Text = "扇形" ' 'MenuItem3 ' Me.MenuItem3.Index = 2 Me.MenuItem3.Text = "圆形" ' 'MenuItem4 ' Me.MenuItem4.Index = 3 Me.MenuItem4.Text = "环形" ' 'MenuItem5 ' Me.MenuItem5.Index = 4 Me.MenuItem5.Text = "三角" ' 'MenuItem6 ' Me.MenuItem6.Index = 5 Me.MenuItem6.Text = "恢复矩形" ' 'MenuItem7 ' Me.MenuItem7.Index = 6 Me.MenuItem7.Text = "退出" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(14,37) Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"),System.Drawing.Image) Me.ClientSize = New System.Drawing.Size(344,304) Me.ContextMenu = Me.ContextMenu1 Me.Font = New System.Drawing.Font("Arial Narrow",24.0!,System.Drawing.FontStyle.Bold,System.Drawing.GraphicsUnit.Point,CType(0,Byte)) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region Private Sub MenuItem1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MenuItem1.Click,MenuItem2.Click,MenuItem3.Click,MenuItem4.Click,MenuItem5.Click,MenuItem6.Click,MenuItem7.Click Dim p As GraphicsPath = New GraphicsPath() Select Case CType(sender,MenuItem).Text Case "椭圆" Dim Width As Integer = Me.ClientSize.Width Dim Height As Integer = Me.ClientSize.Height p.AddEllipse(0,20,Width - 50,Height - 100) '根据要绘制椭圆的形状来填写AddEllipse方法中椭圆对应的相应参数 Case "扇形" p.AddPie(10,10,250,5,150) '根据要实现的扇形形状来填写AddPie方法中的相应参数 Case "圆形" Dim Width As Integer = Me.ClientSize.Width Dim Height As Integer = Me.ClientSize.Height p.AddEllipse(0,Height,Height) '圆形即是椭圆的一种特例, Case "环形" Dim Height As Integer = Me.ClientSize.Height Dim width As Integer = 100 p.AddEllipse(0,Height) 'p.AddRectangle(New Rectangle(width,width,width)) p.AddEllipse(width,Height - (width * 2),Height - (width * 2)) '根据环形的形状来分别填写AddEllipse方法中相应的参数 Case "三角" p.AddLine(0,150) p.AddLine(250,150,300) p.AddLine(0,300) '根据三角形的形状特征来分别填写AddLine方法中相应的参数 Case "恢复矩形" p.AddRectangle(New Rectangle(0,Me.Width,Me.Height)) '用窗体尺寸矩形填充addrectangle()方法 Case "退出" Close() End Select '设置窗体的外形 Me.Region = New Region(p) End Sub Private Sub ContextMenu1_Popup(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles ContextMenu1.Popup End Sub Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load End Sub End Class