窗体是用户进行界面设计的窗口,我们一般见到的都是标准的WINDOWS方形窗体,但在有的时候,我们却需要一些非标准的窗体,如多边形窗体,圆形窗体,椭圆形窗体,以满足程序开发的需要,这时候我们可以利用WINDOWS API 函数来解决这些问题,下面这个程序就向大家介绍如何利用WINDOWS API函数设计五角星窗体.
本程序有两个关键部分,一是如何利用API函数,二是如何完成五角星的顶点坐标定位,现分别作一介绍.
Windows API 函数 CreatePolygonRgn() and SetWindowRgn() 使用如下:
1)函数声明:
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
Private Type COORD
x As Long
y As Long
End Type
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As Any, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function Polygon Lib "gdi32" (ByVal hdc As Long, lpPoint As Any, ByVal nCount As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
Private Declare Function GetStockObject Lib "gdi32" (ByVal nIndex As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Const ALTERNATE = 1 ' ALTERNATE and WINDING are
Const WINDING = 2 ' constants for FillMode.
Const BLACKBRUSH = 4 ' Constant for brush type.
Private Sub Form_Paint()
Dim poly(1 To 3) As COORD, NumCoords As Long, hBrush As Long, hRgn As Long
Me.Cls
' Number of vertices in polygon.
NumCoords = 3
' Set scalemode to pixels to set up points of triangle.
Me.ScaleMode = vbPixels
' Assign values to points.
Poly(1).x = Form1.ScaleWidth / 2
poly(1).y = Form1.ScaleHeight / 2
poly(2).x = Form1.ScaleWidth / 4
poly(2).y = 3 * Form1.ScaleHeight / 4
poly(3).x = 3 * Form1.ScaleWidth / 4
poly(3).y = 3 * Form1.ScaleHeight / 4
' Polygon function creates unfilled polygon on screen.
' Remark FillRgn statement to see results.
Polygon Me.hdc, poly(1), NumCoords
' Gets stock black brush.
hBrush = GetStockObject(BLACKBRUSH)
' Creates region to fill with color.
hRgn = CreatePolygonRgn(poly(1), NumCoords, ALTERNATE)
' If the creation of the region was successful then color.
If hRgn Then FillRgn Me.hdc, hRgn, hBrush
DeleteObject hRgn
End Sub
Private Sub Form_Resize()
Form_Paint
End Sub
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
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function FillRgn Lib "gdi32" (ByVal hDC As Long, ByVal hRgn As Long, ByVal hBrush As Long) As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Private Declare Function Polyline Lib "gdi32" (ByVal hDC As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
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 ReleaseCapture Lib "user32" () As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
X As Long
Y As Long
End Type
Private scnPts() As POINTAPI
Private rgnPts() As POINTAPI
' Used to support captionless drag
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2
Private m_FillMode As Long
Private Const nPts& = 36
Private Sub Command1_Click()
Dim hRgn As Long
Static UsingPoly As Boolean
'
' Flag variable tracks current state.
'
UsingPoly = Not UsingPoly
If UsingPoly Then
'
' Create a region, then turn on
' clipping to that region.
'
hRgn = CreatePolygonRgn(rgnPts(0), nPts, m_FillMode)
Call SetWindowRgn(Me.hWnd, hRgn, True)
Else
'
' Turn off clipping.
'
Call SetWindowRgn(Me.hWnd, 0&, True)
End If
Timer1.Enabled = UsingPoly
End Sub
Private Sub Form_Load()
m_FillMode = ALTERNATE
With Me
.ScaleMode = vbPixels
.Width = Screen.Width \ 2
.Height = .Width
.Move (Screen.Width - .Width) \ 2, (Screen.Height - .Height) \ 2
.Icon = Nothing
End With
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'
' Allow captionless drag if form is clipped to region
'
If Timer1.Enabled Then
Call ReleaseCapture
Call SendMessage(Me.hWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
End If
End Sub
Private Sub Form_Paint()
Dim hBrush As Long
Dim hRgn As Long
'
' Create region and a brush to fill it with.
'
hBrush = CreateSolidBrush(vbRed)
hRgn = CreatePolygonRgn(scnPts(0), nPts, m_FillMode)
Call FillRgn(Me.hDC, hRgn, hBrush)
'
' Clean up GDI objects.
'
Call DeleteObject(hRgn)
Call DeleteObject(hBrush)
'
' Draw outline around polygon.
'
Call Polyline(Me.hDC, scnPts(0), nPts + 1)
End Sub
Private Sub Form_Resize()
With Me
Command1.Move (.ScaleWidth - Command1.Width) \ 2, _
(.ScaleHeight - Command1.Height) \ 2
If .Visible Then
CalcRgnPoints
.Refresh
End If
End With
End Sub
Private Static Sub CalcRgnPoints()
ReDim scnPts(0 To nPts) As POINTAPI
ReDim rgnPts(0 To nPts) As POINTAPI
Dim offset As Long
Dim angle As Long
Dim theta As Double
Dim radius1 As Long
Dim radius2 As Long
Dim x1 As Long
Dim y1 As Long
Dim xOff As Long
Dim yOff As Long
Dim n As Long
'
' Some useful constants.
'
Const Pi# = 3.14159265358979
Const DegToRad# = Pi / 180
'
' Calc radius based on form size.
'
x1 = Me.ScaleWidth \ 2
y1 = Me.ScaleHeight \ 2
If x1 > y1 Then
radius1 = y1 * 0.85
Else
radius1 = x1 * 0.85
End If
radius2 = radius1 * 0.5
'
' Offsets to move origin to upper
' left of window.
'
xOff = GetSystemMetrics(SM_CXFRAME)
yOff = GetSystemMetrics(SM_CYFRAME) + GetSystemMetrics(SM_CYCAPTION)
'
' Step through a circle, 10 degrees each
' loop, finding points for polygon.
'
n = 0
For angle = 0 To 360 Step 10
theta = (angle - offset) * DegToRad
'
' First region is for drawing.
' One long, one short, one long...
'
If n Mod 2 Then
scnPts(n).X = x1 + (radius1 * (Sin(theta)))
scnPts(n).Y = y1 + (radius1 * (Cos(theta)))
Else
scnPts(n).X = x1 + (radius2 * (Sin(theta)))
scnPts(n).Y = y1 + (radius2 * (Cos(theta)))
End If
'
' Second region is for clipping.
' Add offsets.
'
rgnPts(n).X = scnPts(n).X + xOff
rgnPts(n).Y = scnPts(n).Y + yOff
n = n + 1
Next angle
offset = (offset + 2) Mod 360
End Sub
Private Sub Option1_Click(Index As Integer)
m_FillMode = Index + 1
End Sub
Private Static Sub Timer1_Timer()
Dim nRet As Long
Dim hRgn As Long
CalcRgnPoints
hRgn = CreatePolygonRgn(rgnPts(0), nPts, m_FillMode)
nRet = SetWindowRgn(Me.hWnd, hRgn, True)
End Sub