本实例使用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