开发者

How to create a different type of shapes of forms?

开发者 https://www.devze.com 2023-02-17 19:15 出处:网络
I am in real troubles: I want to create a Windows form application in VB.NET 2008, and I want to create a circle-开发者_运维技巧shaped window.

I am in real troubles: I want to create a Windows form application in VB.NET 2008, and I want to create a circle-开发者_运维技巧shaped window.

How can I do it? Can anyone help me?


You can set the form's Region property.

To make a circular region, create a GraphicsPath, call AddEllipse, then pass it to the Region constructor.


Some things you have to do to make this work properly. First off, it is important to wait until the OnLoad() method runs. Only then do you know how large the window got. It won't be the design size on another machine when the user runs the video adapter at a different DPI. You'll also have to remove the border and caption, they no longer work well when you give the window a shape. Which leaves it up to you to re-implement the work they do. At a minimum you'd want to allow the user to still move the window.

A sample form that does this:

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

Use this as a starting point to implement your own window chrome. You'll probably want to add a glyph that lets the user close the window. The BackgroundImage property is a good way to give the window a 'texture'. Or modify OnPaint() to draw your own.


Its called irregular forms or irregular shapes. Here is a good article on it: http://www.codeproject.com/Tips/149249/Simplest-way-to-implement-irregular-forms-in-NET.aspx


Set these properties to your form

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 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号