简单的圆角无边框窗体

见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

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