使用SetWindowRgn函数可以设置窗体形状,定义如下:
Declare Function SetWindowRgn Lib "user32" Alias "SetWindowRgn" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
hWnd既可以是窗体的句柄,又可以是按钮的句柄
hRgn是要用下述几个函数设置的
bRedraw设置为True就行了
在设置之前首先要设置区域句柄(0为整个窗体):
CreateRectRgn、CreateEllipticRgn、CreatePolygonRgn等可以创建各种形状
然后用CombineRgn函数合并形状
例如(真对不起,我遇到麻烦了):
Option Explicit
Private Type POINTAPI
x As Long
y As Long
End Type
Dim XY() As POINTAPI
'创建一个由一系列点围成的区域。windows在需要时自动将最后点与第一点相连以封闭多边形
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
'创建一个由点X1,Y1和X2,Y2描述的矩形区域
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
'创建一个椭圆,该椭圆与X1,Y1和X2,Y2坐标点确定的矩形内切
Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
'API函数中的一个。本函数允许您改变窗口的区域。 _
通常所有窗口都是矩形的——窗口一旦存在就含有一个矩形区域。本函数允许您放弃该区域。这意味着您可以创建圆的、星形的窗口,也可以将它分为两个或许多部分——实际上可以是任何形状。为区域指定的所有坐标都以窗口坐标(不是客户坐标)表示,它们以整个窗口(包括标题栏和边框)的左上角为起点
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
'将两个区域组合为一个新区域
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
'nCombineMode 的可能值
Private Const RGN_AND As Long = 1
Private Const RGN_COPY As Long = 5
Private Const RGN_DIFF As Long = 4
Private Const RGN_OR As Long = 2
Private Const RGN_XOR As Long = 3
Private Sub Form_Load()
Dim hRgn As Long, h1 As Long, h2 As Long
Dim lRes As Long
ReDim XY(7) As POINTAPI 'T shape has 8 points
Me.ScaleMode = vbPixels
With Me
XY(0).x = 0
XY(0).y = 0
XY(1).x = .ScaleWidth
XY(1).y = 0
XY(2).x = .ScaleWidth
XY(2).y = .ScaleHeight / 2
XY(3).x = .ScaleWidth - (.ScaleWidth / 3)
XY(3).y = .ScaleHeight / 2
XY(4).x = .ScaleWidth - (.ScaleWidth / 3)
XY(4).y = .ScaleHeight
XY(5).x = .ScaleWidth / 3
XY(5).y = .ScaleHeight
XY(6).x = .ScaleWidth / 3
XY(6).y = .ScaleHeight / 2
XY(7).x = 0
XY(7).y = .ScaleHeight / 2
End With
h1 = CreatePolygonRgn(XY(0), 8, 2)
h2 = CreateRectRgn(XY(5).x, 0, XY(3).x, 50)
' 这个语句我调试半天不知为什么不成功
' 这个要靠你自己研究了……
'Call CombineRgn(hRgn, h1, h2, RGN_OR)
lRes = SetWindowRgn(Me.hWnd, h1, True)
End Sub
Private Sub Form_Click()
Unload Me
End Sub