创建圆角或者圆形窗体

本实例使用API函数CreateRoundRectRgn和SetWindowRgn来创建圆角或者圆形窗体。

 

窗口代码如下:

'用户昵称: 留下些什么
'个人简介: 一个会做软件的货代
'CSDN网址:https://blog.csdn.net/zezese
'电子邮箱:31319180@qq.com

Option Explicit

Private Const WM_NCLBUTTONDOWN As Long = &HA1
Private Const HTCAPTION As Long = 2

Private Declare Sub ReleaseCapture Lib "user32" ()
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal x1 As Long, ByVal y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
Private Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Long, ByRef lpRect As RECT) As Long

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type


Private Sub Command1_Click()
    Unload Me
End Sub

Private Sub Form_DblClick()
    Unload Me
End Sub

Private Sub Form_Load()
    'Me.BorderStyle = 0 设计时 设置为 无边框窗体
    
    '调整直径,窗体大小,可以实现方形圆角窗体 或者 圆形窗体 (窗体边长=直径)
    
    Const diameter As Long = 400 '直径,单位像素
    
    Me.Height = diameter * VB.Screen.TwipsPerPixelY
    Me.Width = diameter * VB.Screen.TwipsPerPixelX
    
    Dim FormWidthInPixels As Long, FormHeightInPixels As Long
    
    Dim rWin As RECT
    GetWindowRect hwnd, rWin
    FormWidthInPixels = rWin.Right - rWin.Left
    FormHeightInPixels = rWin.Bottom - rWin.Top
    
    Dim rtn As Long

    rtn = CreateRoundRectRgn(0, 0, FormWidthInPixels, FormHeightInPixels, diameter, diameter)
    Call SetWindowRgn(hwnd, rtn, True)
    
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    
    '点击鼠标左键,可以移动窗口
    
    If Button = vbLeftButton Then
        ReleaseCapture
        SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0&
    End If
    
End Sub

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值