Win32 API 有很多让你意想不到的功能。要创建特殊的不规则窗口看上去似乎很难。 但我们运用API,则几行代码就可以实现,这似乎不可思议。但事实就是如此,不信看看吧!实现步骤:
(1 ) 建立一个新项目文件DEMO1.PRJ;
(2 ) 建立模块文件DEMO1.BAS.并在模块的声明部分加入以下代码:
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long '定义顶点结构 Public Type POINTAPI X As Long Y As Long End Type Public Dim XYPOINT() As POINTAPI
(3)在窗体Form1的Command1_Click处理程序中加入以下代码:
Private Sub Command1_Click() '定义区域句柄 Dim hRgn As Long Dim lRes As Long '定义T型顶点坐标数组 ReDim XYPOINT(7) As POINTAPI 'T shape has 8 points '确定T型顶点坐标的值 With Me XYPOINT(0).X = 0 XYPOINT(0).Y = 0 XYPOINT(1).X = .ScaleWidth XYPOINT(1).Y = 0 XYPOINT(2).X = .ScaleWidth XYPOINT(2).Y = .ScaleHeight / 2 XYPOINT(3).X = .ScaleWidth - (.ScaleWidth / 3) XYPOINT(3).Y = .ScaleHeight / 2 XYPOINT(4).X = .ScaleWidth - (.ScaleWidth / 3) XYPOINT(4).Y = .ScaleHeight XYPOINT(5).X = .ScaleWidth / 3 XYPOINT(5).Y = .ScaleHeight XYPOINT(6).X = .ScaleWidth / 3 XYPOINT(6).Y = .ScaleHeight / 2 XYPOINT(7).X = 0 XYPOINT(7).Y = .ScaleHeight / 2 End With hRgn = CreatePolygonRgn(XYPOINT(0), 8, 1) lRes = SetWindowRgn(Me.hWnd, hRgn, True) Me.BackColor = vbBlue End Sub
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Public Type POINTAPI X As Long Y As Long End Type Public Oldx As Long Public Oldy As Long Public Newx As Long Public Newy As Long Public Pnt As POINTAPI