簡單的圓角無邊框窗體

見CSDN網友提出這樣的問題(做一個象qq一樣的外殼),查了一下資料,發現可以通過API做個簡單的效果,至於QQ是怎麼實現的,還沒研究過.

新建一個窗體Form1,拖一個Button控件進去,保留Button默認名字Button1不變,用以下代碼替換Form1.vb中的代碼:

Public Class Form1

    Inherits System.Windows.Forms.Form

 

#Region " Windows 窗體設計器生成的代碼 "

 

    Public Sub New()

        MyBase.New()

 

        '該調用是 Windows 窗體設計器所必需的。

        InitializeComponent()

 

        ' InitializeComponent() 調用之後添加任何初始化

 

    End Sub

 

    '窗體重寫 dispose 以清理組件列表。

    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 Button1 As System.Windows.Forms.Button

    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        Me.Button1 = New System.Windows.Forms.Button

        Me.SuspendLayout()

        '

        'Button1

        '

        Me.Button1.Location = New System.Drawing.Point(112, 40)

        Me.Button1.Name = "Button1"

        Me.Button1.TabIndex = 0

        Me.Button1.Text = "Close"

        '

        'Form1

        '

        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)

        Me.ClientSize = New System.Drawing.Size(292, 273)

        Me.Controls.Add(Me.Button1)

        Me.Name = "Form1"

        Me.Text = "Form1"

        Me.ResumeLayout(False)

 

    End Sub

 

#End Region

 

  

 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        '窗體最小尺寸

        Me.MinimumSize = New Size(100, 100)

 

        '窗體最大尺寸

        Me.MaximumSize = New Size(500, 500)

 

        '窗體爲無邊框類型

        Me.FormBorderStyle = FormBorderStyle.None

 

        SetRoundWnd(Me)

       

    End Sub

 

    Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged

        '窗體大小改變時重新設置顯示區域

        SetRoundWnd(Me)

    End Sub

 

    '讓無邊框窗體也能通過拖動邊框來改變大小

    Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams

        Get

            Dim cp As CreateParams = MyBase.CreateParams

            cp.Style = cp.Style Or WS_THICKFRAME

            Return cp

        End Get

    End Property

 

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown

        '點擊窗體任意位置進行拖動

        ReleaseCapture()

        SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0)

    End Sub

 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        '沒有標題欄了, 提供關閉窗體的地方

        Me.Close()

    End Sub

 

End Class

 

增加如下的模塊:

Module Module_AntingZ

 

    'API聲明

 

    Public Declare Function CreateRoundRectRgn Lib "gdi32" Alias "CreateRoundRectRgn" (ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer, ByVal X3 As Integer, ByVal Y3 As Integer) As Integer

 

    Public Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As IntPtr, ByVal hRgn As Integer, ByVal bRedraw As Boolean) As Integer

 

    Public Declare Function ReleaseCapture Lib "user32" Alias "ReleaseCapture" () As Integer

 

    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Object) As Integer

 

    '常量定義

 

    Public Const WM_NCLBUTTONDOWN = &HA1

 

    Public Const HTCAPTION = 2

    Public Const WS_THICKFRAME = &H40000

 

    '設置窗體顯示區域(圓型邊角)

    Public Sub SetRoundWnd(ByVal frm As Form)

        Dim r As Integer = CreateRoundRectRgn(0, 0, frm.Width, frm.Height, 100, 100)

        SetWindowRgn(frm.Handle, r, True)

    End Sub

 

End Module

 

Window Styles參考MSDN:

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/vclib/html/_mfc_Window_Styles.htm

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章