如何创建不同类型的表单形状?

时间:2011-03-20 13:42:01

标签: vb.net

我遇到了麻烦:我想在VB.NET 2008中创建一个Windows窗体应用程序,我想创建一个圆形窗口。
我该怎么做?任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:3)

您可以设置表单的Region属性。

要制作圆形区域,请创建GraphicsPath,调用AddEllipse,然后将其传递给Region构造函数。

答案 1 :(得分:2)

它被称为不规则形状或不规则形状。这是一篇很好的文章:http://www.codeproject.com/Tips/149249/Simplest-way-to-implement-irregular-forms-in-NET.aspx

答案 2 :(得分:2)

要使其正常工作,您需要做些些事情。首先,重要的是等待OnLoad()方法运行。只有这样你才知道窗户有多大。当用户在不同的DPI下运行视频适配器时,它不会是另一台机器上的设计大小。你还必须删除边框和标题,当你给窗口一个形状时它们不再有效。这让你不得不重新实施他们所做的工作。至少你想让用户仍然移动窗口。

执行此操作的示例表单:

Public Class Form1
    Public Sub New()
        InitializeComponent()
        Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
        Me.DoubleBuffered = True
    End Sub

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        '' Set window shape
        Using path As New System.Drawing.Drawing2D.GraphicsPath
            path.AddEllipse(0, 0, Me.ClientSize.Width, Me.ClientSize.Height)
            Me.Region = New Region(path)
        End Using
        MyBase.OnLoad(e)
    End Sub

    Private Const WM_NCHITTEST As Integer = &H84
    Private Const HTCLIENT As Integer = 1
    Private Const HTCAPTION As Integer = 2
    Private Const CaptionHeight As Integer = 30

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
        '' Detect clicks at top of window to allow it to be moved
        If m.Msg = &H84 AndAlso m.Result.ToInt32() = HTCLIENT Then
            Dim pos As Point = New Point(m.LParam.ToInt32())
            pos = Me.PointToClient(pos)
            If pos.Y < CaptionHeight Then m.Result = CType(HTCAPTION, IntPtr)
        End If
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        '' Draw a simple caption
        e.Graphics.FillRectangle(Brushes.Blue, 0, 0, Me.ClientSize.Width, CaptionHeight)
        MyBase.OnPaint(e)
    End Sub
End Class

以此为起点,实现自己的窗口镶边。您可能希望添加一个允许用户关闭窗口的字形。 BackgroundImage属性是一种为窗口提供“纹理”的好方法。或者修改OnPaint()以绘制自己的。

答案 3 :(得分:1)

将这些属性设置为表单

1. BackgroundImage  =  your_Image            ' image of shape you want
2. BackColor        =  Outside_Area_Color    ' color of outside area of image
3. FormBorderStyle  =  None                  ' to hide border and TitleBar of form
4. TransparentKey   =  Same_as_BackColor