如何画自己的控件
SUMMARY
The Win32 API includes a handy function called DrawFrameControl. This function is useful for drawing many of the standard Windows 95, Windows 98, or Windows Me 3D controls as graphic objects on your forms. The function only draws the controls on your form. To make these drawings behave like controls, you must add code to provide this functionality.
MORE INFORMATION
This sample demonstrates how to use the DrawFrameControl function. You create a module that declares the necessary constants and the DrawFrameControl function. The form contains code to call the function and draw the controls on your form. When you run the program, control drawings appear on the form.
1.Start a new project in Visual Basic. Form1 is created by default.
2.Add a BAS module to the project and add the following code to the module:
'*****************************************************************
' DrawFram.bas - Contains API declarations and constants for the
' DrawFrameControl API function.
'*****************************************************************
Option Explicit
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
' flags for DrawFrameControl
Public Const DFC_CAPTION = 1 'Title bar
Public Const DFC_MENU = 2 'Menu
Public Const DFC_SCROLL = 3 'Scroll bar
Public Const DFC_BUTTON = 4 'Standard button
Public Const DFCS_CAPTIONCLOSE = &H0 'Close button
Public Const DFCS_CAPTIONMIN = &H1 'Minimize button
Public Const DFCS_CAPTIONMAX = &H2 'Maximize button
Public Const DFCS_CAPTIONRESTORE = &H3 'Restore button
Public Const DFCS_CAPTIONHELP = &H4 'Windows 95 only:
'Help button
Public Const DFCS_MENUARROW = &H0 'Submenu arrow
Public Const DFCS_MENUCHECK = &H1 'Check mark
Public Const DFCS_MENUBULLET = &H2 'Bullet
Public Const DFCS_MENUARROWRIGHT = &H4
Public Const DFCS_SCROLLUP = &H0 'Up arrow of scroll
'bar
Public Const DFCS_SCROLLDOWN = &H1 'Down arrow of
'scroll bar
Public Const DFCS_SCROLLLEFT = &H2 'Left arrow of
'scroll bar
Public Const DFCS_SCROLLRIGHT = &H3 'Right arrow of
'scroll bar
Public Const DFCS_SCROLLCOMBOBOX = &H5 'Combo box scroll
'bar
Public Const DFCS_SCROLLSIZEGRIP = &H8 'Size grip
Public Const DFCS_SCROLLSIZEGRIPRIGHT = &H10 'Size grip in
'bottom-right
'corner of window
Public Const DFCS_BUTTONCHECK = &H0 'Check box
Public Const DFCS_BUTTONRADIO = &H4 'Radio button
Public Const DFCS_BUTTON3STATE = &H8 'Three-state button
Public Const DFCS_BUTTONPUSH = &H10 'Push button
Public Const DFCS_INACTIVE = &H100 'Button is inactive
'(grayed)
Public Const DFCS_PUSHED = &H200 'Button is pushed
Public Const DFCS_CHECKED = &H400 'Button is checked
Public Const DFCS_ADJUSTRECT = &H2000 'Bounding rectangle is
'adjusted to exclude the
'surrounding edge of the
'push button
Public Const DFCS_FLAT = &H4000 'Button has a flat border
Public Const DFCS_MONO = &H8000 'Button has a monochrome
'border
Public Declare Function DrawFrameControl Lib "user32" (ByVal _
hDC&, lpRect As RECT, ByVal un1 As Long, ByVal un2 As Long) _
As Boolean
3.Copy the following code to the Form1 Code Window:
'********************************************************************
' DrawFram.frm - Demonstrates how to use DrawFrameControl
'********************************************************************
Option Explicit
'********************************************************************
' Helper function that allows a you to load a rect on one line
'********************************************************************
Private Function MakeRect(l As Long, t As Long, w As Long, _
h As Long) As RECT
With MakeRect
.Left = l
.Top = t
.Right = l + w
.Bottom = t + h
End With
End Function
Private Sub Form_Load()
ScaleMode = vbPixels
AutoRedraw = True
Dim LineAngle As Double
Dim OldLineAngle As Double
'Dim rc As RECT
Dim X1 As Single, X2 As Single ''''声明圆心坐标变量
Private Sub UserControl_Show()
UserControl.Width = 200
UserControl.Height = 200
End Sub
Public Property Get DrawMode() As Integer
DrawMode = UserControl.DrawMode
End Property
Public Property Let DrawMode(ByVal New_DrawMode As Integer)
UserControl.DrawMode() = New_DrawMode
PropertyChanged "DrawMode"
End Property
Public Sub PaintAngle(BegX As Single, BegY As Single, EndX As Single, EndY As Single)
UserControl.DrawMode = vbCopyPen
UserControl.Cls
DrawWidth = 1
If EndX = BegX Then
If EndY > BegY Then
X1 = 100
Y1 = 200
LineAngle = PI / 2
Else
X1 = 100
Y1 = 0
LineAngle = PI * 3 / 2
End If
ElseIf EndX > BegX Then
LineAngle = PI - Atn((EndY - BegY) / (EndX - BegX))
If EndY > BegY Then
If (EndY - BegY) / (EndX - BegX) < 1 Then
X1 = 200
Y1 = 100 + 100 * (EndY - BegY) / (EndX - BegX)
Else
X1 = 100 + 100 * (EndX - BegX) / (EndY - BegY)
Y1 = 200
End If
Else
If (EndY - BegY) / (EndX - BegX) > -1 Then
X1 = 200
Y1 = 100 + 100 * (EndY - BegY) / (EndX - BegX)
Else
X1 = 100 - 100 * (EndX - BegX) / (EndY - BegY)
Y1 = 0
End If
End If
Else
LineAngle = 2 * PI - Atn((EndY - BegY) / (EndX - BegX))
If EndY > BegY Then
If (EndY - BegY) / (EndX - BegX) > -1 Then
X1 = 0
Y1 = 100 - 100 * (EndY - BegY) / (EndX - BegX)
Else
X1 = 100 + 100 * (EndX - BegX) / (EndY - BegY)
Y1 = 200
End If
Else
If (EndY - BegY) / (EndX - BegX) > 1 Then
X1 = 100 - 100 * (EndX - BegX) / (EndY - BegY)
Y1 = 0
Else
X1 = 0
Y1 = 100 - 100 * (EndY - BegY) / (EndX - BegX)
End If
End If
End If
''''''''''''''''开始画扇形
If LineAngle + PI / 12 <= PI * 2 Then
Circle (X1, Y1), 200, &H80000007, -(LineAngle - PI / 12), -(LineAngle + PI / 12)
ElseIf LineAngle - PI / 12 >= PI * 2 Then
Circle (X1, Y1), 200, &H80000007, -(LineAngle - PI / 12 - PI * 2), -(LineAngle + PI / 12 - PI * 2)
Else 'LineAngle - PI / 12 < PI * 2 and LineAngle + PI / 12 > PI * 2
Circle (X1, Y1), 200, &H80000007, -(LineAngle - PI / 12), -(LineAngle + PI / 12 - PI * 2)
End If
End Sub
'在Form窗体上放一个Line1,一个Arrow1
'代码如下
Option Explicit
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Line1.X1 = X
Line1.Y1 = Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Line1.X2 = X
Line1.Y2 = Y