| Some other things are necessary. The ability to put controls on the window is one of them. Since controls are only windows themselves with some additional properties, we can use the clsWindow class to represent them with some modifications.
We want the base window to know about the controls it contains. A collection is a simple way to allow this. Add the following to the Declarations section of the clsWindow class (see the WindowSample2 project):
Private colChildren As New Collection
This collection will hold references to the controls that the window that contains them. We need a function to allow us to add controls to the class. The following function will handle that:
Public Sub AddChild(clsChild As clsWindow)
colChildren.Add clsChild
End Sub
Since there are many types of controls we抣l need a way to tell what kind of control we抮e placing on the window. Add the following to the Declarations section as well:
Private iObjectType As eObjectType
Private iObjectState As eObjectState
and add the following to the modMain module:
Public Enum eObjectState
iEnabled
iDisabled
iPressed
iChecked
iUnchecked
iChecked_iDisabled
End Enum
Public Enum eObjectType
Btn
ChkBox
End Enum
These enums will represent the type of control and the state of that control. These enums will grow as new control types and states are added to them. For now a button and a checkbox are enough to demonstrate their usage.
Now we need objects of the new window types. Add the following to the modDirectDraw module:
Public OKButton As New clsWindow
Public Check As New clsWindow
These will represent the new controls. This will require us to update the InitDD function as well. Add the following to the function:
Dim ddsdSurf3 As DDSURFACEDESC2
Dim ddsdSurf4 As DDSURFACEDESC2
OKButton.ObjectSurface = objDD.CreateSurfaceFromFile(App.Path & "\ok.bmp", ddsdSurf3)
OKButton.ObjectType = Btn
OKButton.ObjectState = iEnabled
OKButton.ParentHeight = Window.Height
OKButton.ParentWidth = Window.Width
OKButton.ParentX = Window.X
OKButton.ParentY = Window.Y
OKButton.CenterX = True
OKButton.CenterY = True
Window.AddChild OKButton
Check.ObjectSurface = objDD.CreateSurfaceFromFile(App.Path & "\check.bmp", ddsdSurf4)
Check.ObjectType = ChkBox
Check.ObjectState = iUnchecked
Check.ParentHeight = Window.Height
Check.ParentWidth = Window.Width
Check.ParentX = Window.X
Check.ParentY = Window.Y
Check.X = 200
Check.Y = 200
Window.AddChild Check
Take a look at the ok.bmp and check.bmp files. These are the bitmaps that are used for the controls. Notice that there are several versions of the control in the file. They represent the various states that the control can have. For the button bitmap they are iEnabled, iPressed, and iDisabled. The checkbox has iUnchecked, iChecked, iDisabled, and iChecked_iDisabled. The iEnabled enum could have been used instead of the iUnchecked, but I felt it wouldn抰 have described the state of the control as well. Notice also that we抮e centering the button inside the window, but setting the X and Y coordinates of the checkbox. We抳e also added the text 揙K?to the bitmap for the button, but this could have been left out. This will be done later when we add text support to the class. This will also allow us to add text to the checkbox and the window.
Change the Property Let ObjectSurface to the following:
Dim ddsd As DDSURFACEDESC2
Set objBitmap = objSurface
objBitmap.GetSurfaceDesc ddsd
iWidth = ddsd.lWidth
Select Case iObjectType
Case Btn
iHeight = ddsd.lHeight / 3
Case ChkBox
iHeight = ddsd.lHeight / 4
Case BaseWindow
iHeight = ddsd.lHeight
End Select
This will handle the new types of controls. This could also have been done by adding Property Let statements to the class and setting the Width and Height properties in the InitDD function.
Now we need to handle clicking on the controls. Add the following to the clsWindow class:
Public Sub MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
Dim iLp As Integer
If X >= iX And X <= iX + iWidth And Y >= iY And Y <= iY + iHeight Then
For iLp = 1 To colChildren.Count
colChildren(iLp).MouseDown Button, Shift, X, Y
Next iLp
If Not (iObjectState = iDisabled) Then
Select Case iObjectType
Case ChkBox
If iObjectState = iChecked Then
iObjectState = iUnchecked
Else
iObjectState = iChecked
End If
Case Btn
iObjectState = iPressed
End Select
End If
End If
End Sub
Public Sub MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Si 上一页 [1] [2] [3] 下一页 |