分享
 
 
 

DX: Full Screen GUI Development 2

王朝vb·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Full Screen GUI Development

Pt 2 ?More Controls & Text

By Jim (Machaira) Perry

Welcome back! If you抮e saying 揌uh?!?then you probably haven抰 read the first part of this tutorial. Please read it before continuing because we抣l be building on it.

Download this tutorial (in Word format) and the sample projects in a zip file (40 KB)

First Up

Let抯 build on our base window by adding the standard Minimize, Maximize/Restore, and Close buttons (see WindowSample3 project).

We抳e added enums for the new buttons:

CloseBtn

MinBtn

MaxBtn

RestoreBtn

created bitmaps for them (close.bmp, minimize.bmp, maximize.bmp, and restore.bmp), and added instances of the clsWindow class in the modDirectDraw module to create them:

Public WithEvents CloseButton As New clsWindow

Public WithEvents MinButton As New clsWindow

Public WithEvents MaxButton As New clsWindow

Public WithEvents RestoreButton As New clsWindow

Notice that we抳e added WithEvents to the declarations. This will allow us to do things outside of the class when the control is clicked. We抣l go over this a little later.

New controls mean that we have to add them as child controls to the base window object. We抳e done this in the CreateWindowObjects function, setting the necessary properties for them:

With frmMain.CloseButton

.ObjectType = CloseBtn

.ParentHeight = frmMain.Window.Height

.ParentWidth = frmMain.Window.Width

.ParentX = frmMain.Window.X

.ParentY = frmMain.Window.Y

.ObjectSurface = objDD.CreateSurfaceFromFile(App.Path & "\close.bmp", ddsdSurf4)

.WindowName = "CloseBtn"

End With

frmMain.Window.AddChild frmMain.CloseButton

With frmMain.MinimizeButton

.ObjectType = MinBtn

.ParentHeight = frmMain.Window.Height

.ParentWidth = frmMain.Window.Width

.ParentX = frmMain.Window.X

.ParentY = frmMain.Window.Y

.ObjectSurface = objDD.CreateSurfaceFromFile(App.Path & "\minimize.bmp", ddsdSurf4)

.WindowName = "MinBtn"

End With

frmMain.Window.AddChild frmMain.MinimizeButton

With frmMain.MaximizeButton

.ObjectType = MaxBtn

.ParentHeight = frmMain.Window.Height

.ParentWidth = frmMain.Window.Width

.ParentX = frmMain.Window.X

.ParentY = frmMain.Window.Y

.ObjectSurface = objDD.CreateSurfaceFromFile(App.Path & "\maximize.bmp", ddsdSurf4)

.WindowName = "MaxBtn"

End With

frmMain.Window.AddChild frmMain.MaximizeButton

With frmMain.RestoreButton

.ObjectType = RestoreBtn

.ParentHeight = frmMain.Window.Height

.ParentWidth = frmMain.Window.Width

.ParentX = frmMain.Window.X

.ParentY = frmMain.Window.Y

.ObjectSurface = objDD.CreateSurfaceFromFile(App.Path & "\restore.bmp", ddsdSurf4)

.WindowName = "RestoreBtn"

End With

frmMain.Window.AddChild frmMain.RestoreButton

This is code that used to be in the InitDD function, but has been moved out for a good reason, as you抣l see soon. It allows us to recreate the base window object and all children whenever we want. Notice also that we抳e changed the code to use the With匛ndWIth statements. It抯 saves our poor little fingers from having to type as much and speeds up the code a bit (not that it抣l be noticeable unless you timed it.

If you compare this chunk of code with the code to create the OK button, you抣l notice that we抮e not setting any X and Y or ObjectState properties for the buttons. That抯 because you can抰. The buttons only go in one place, the standard position on the window they抮e attached to. The properties are set in the ObjectSurface Property Set method:

Case MinBtn

iHeight = ddsd.lHeight / 2

iX = iParentWidth + iParentX - iWidth - 40

iY = iParentY + 7

iObjectState = iEnabled

Case MaxBtn

iHeight = ddsd.lHeight / 2

iX = iParentWidth + iParentX - iWidth - 24

iY = iParentY + 7

iObjectState = iEnabled

Case CloseBtn

iHeight = ddsd.lHeight / 2

iX = iParentWidth + iParentX - iWidth - 6

iY = iParentY + 7

iObjectState = iEnabled

Case RestoreBtn

iHeight = ddsd.lHeight / 2

iX = iParentWidth + iParentX - iWidth - 24

iY = iParentY + 7

iObjectState = iEnabled

The DrawObject function is also modified to handle drawing the new controls:

Case MinBtn, MaxBtn, CloseBtn, RestoreBtn

rectObject.Left = 0

rectObject.Right = iWidth

Select Case iObjectState

Case iEnabled

rectObject.Top = 0

rectObject.Bottom = iHeight

Case iPressed

rectObject.Top = iHeight

rectObject.Bottom = iHeight * 2

End Select

If you run the app, you抣l see the new buttons in the upper right hand corner of the window. You抣l also notice that we抳e added a caption for the window. This is held by the sCaption member of the clsWindow class. If you look at the declarations section for the class you抣l also see two other new members:

Private iCaptionX As Integer

Private iCaptionY As Integer

These will be used to draw the caption where it is needed for the specific control type. If you look at the DrawObject function in the class you抣l see we抳e added the code necessary to handle the caption:

Case BaseWindow

'Nothing needed here since we use the base rectangle

If Len(sCaption) > 0 Then

iCaptionX = iX + 10

iCaptionY = iY + 5

bDrawCaption = True

Else

bDrawCaption = False

End If

?/SPAN>

If bDrawCaption Then

lOldColor = objSurface.GetForeColor

objSurface.SetForeColor RGB(255, 255, 255)

objSurface.DrawText iCaptionX, iCaptionY, sCaption, False

objSurface.SetForeColor lOldColor

End If

The setup of the base window object has been changed to add the caption:

With frmMain.Window

.ObjectSurface = objDD.CreateSurfaceFromFile(App.Path & "\window.bmp", ddsdSurf2)

.ParentX = 0

.ParentY = 0

.ParentHeight = 600

.ParentWidth = 800

.CenterX = True

.CenterY = True

.WindowName = "Base"

.Caption = "Test Window"

End With

The next thing that has been added is the event raising. Take a look at the MouseUp function in the clsWindow class:

Public Function MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) As Boolean

Dim iLp As Integer

Dim bRetVal As Boolean

MouseUp = False

If X >= iX And X <= iX + iWidth And Y >= iY And Y <= iY + iHeight Then

For iLp = 1 To colChildren.Count

'If a child handles the event no need to do anything else

bRetVal = colChildren(iLp).MouseUp(Button, Shift, X, Y)

If bRetVal Then Exit Function

Next iLp

If iObjectType >= CloseBtn Then

iObjectState = iEnabled

If iObjectType = CloseBtn Then

RaiseEvent Clicked

End If

Else

If Not (iObjectState = iDisabled) Then

If iObjectState = iPressed And iObjectType = Btn Then

iObjectState = iEnabled

RaiseEvent Clicked

End If

End If

End If

MouseUp = True

End If

End Function

If the control has been clicked we use RaiseEvent to notify the app that this is so. The app can then decide what to do based on which control has been clicked. In our case we do two things ?destroy the window and close the app:

Private Sub CloseButton_Clicked()

Window.RemoveChildren

Set Window = Nothing

End Sub

Private Sub OKButton_Clicked()

gbRun = False

End Sub

You can recreate the window by pressing the F1 key. Try it and see. I抣l wait while you do. J

Got all that? Good, then lets more on to more cool stuff.

Next Up

Open up the WindowSample4 project and take a look at the modMain module. You抣l see we抳e added RadioBtn and FrameWnd enums. Also not the constant cFrameGrey. We抣l be using this a little later on to help draw the FrameWnd control.

Next take a look at the frmMain code. We抳e added declarations for the new frame and radio button objects. Notice the two sets of radio buttons ?WindowRadio1,2, and 3 and Radio1, 2, and 3. The WindowRadio controls will go on the base window and the Radio controls will be contained by the Frame control. You抣l see that you can change a control in one group without affecting the other, just as you can in VB. This is due to the Parent/Child relationship of the clsWindow objects. We抳e also added the code to support the RaiseEvent call in the class for each of the objects. We merely set two controls that weren抰 clicked to be unchecked.

Not much has changed in the modDirectDraw module. The only thing of significance is in setting up the radio buttons that are contained by the Frame control:

With frmMain.Radio1

.ObjectType = RadioBtn

.ObjectState = iUnchecked

.ParentHeight = frmMain.Frame1.Height

.ParentWidth = frmMain.Frame1.Width

.ParentX = frmMain.Frame1.X

.ParentY = frmMain.Frame1.Y

.X = 100 + .ParentX

.Y = 100 + .ParentY

.ObjectSurface = objDD.CreateSurfaceFromFile(App.Path & "\radio.bmp", ddsdSurf6)

.WindowName = "Radio1"

.Caption = "Radio 1"

End With

'Add to frame not base window

frmMain.Frame1.AddChild frmMain.Radio1

With frmMain.Radio2

.ObjectType = RadioBtn

.ObjectState = iUnchecked

.ParentHeight = frmMain.Frame1.Height

.ParentWidth = frmMain.Frame1.Width

.ParentX = frmMain.Frame1.X

.ParentY = frmMain.Frame1.Y

.X = 100 + .ParentX

.Y = 115 + .ParentY

.ObjectSurface = objDD.CreateSurfaceFromFile(App.Path & "\Radio.bmp", ddsdSurf6)

.WindowName = "Radio2"

.Caption = "Radio 2"

End With

'Add to frame not base window

frmMain.Frame1.AddChild frmMain.Radio2

With frmMain.Radio3

.ObjectType = RadioBtn

.ObjectState = iUnchecked

.ParentHeight = frmMain.Frame1.Height

.ParentWidth = frmMain.Frame1.Width

.ParentX = frmMain.Frame1.X

.ParentY = frmMain.Frame1.Y

.X = 100 + .ParentX

.Y = 130 + .ParentY

.ObjectSurface = objDD.CreateSurfaceFromFile(App.Path & "\Radio.bmp", ddsdSurf6)

.WindowName = "Radio3"

.Caption = "Radio 3"

End With

'Add to frame not base window

frmMain.Frame1.AddChild frmMain.Radio3

Notice that we抮e calling AddChild for the Frame1 control, not the Window control.

The clsWindow class has changed a bit as you probably expected. We抳e exposed the Width and Height members of the clsWindow class in order to set up the Frame control:

With frmMain.Frame1

.ObjectType = FrameWnd

.ObjectState = iEnabled

.ParentHeight = frmMain.Window.Height

.ParentWidth = frmMain.Window.Width

.ParentX = frmMain.Window.X

.ParentY = frmMain.Window.Y

.X = 75

.Y = 75

.Width = 200

.Height = 200

.WindowName = "Frame1"

.Caption = "Test Frame"

End With

frmMain.Window.AddChild frmMain.Frame1

The Property Let ObjectSurface and MouseDown function have had the RadioBtn added to the cases for the ChkBox:

Public Property Let ObjectSurface(ByVal objSurface As DirectDrawSurface7)

?/SPAN>

Case ChkBox, RadioBtn

iHeight = ddsd.lHeight / 4

?/SPAN>

End Property

Public Function DrawObject(objSurface As DirectDrawSurface7)

Dim clsWindow As clsWindow

Dim iLp As Integer

Dim ddsd As DDSURFACEDESC2

Dim rectBitmap As RECT

Dim rectObject As RECT

Dim lRet As Long

Dim bDrawCaption As Boolean

Dim lOldColor As Long

Dim lBltFlags As Long

On Error GoTo DrawObjectErr

rectBitmap.Left = iX

rectBitmap.Right = iX + iWidth

rectBitmap.Top = iY

rectBitmap.Bottom = iY + iHeight

Select Case iObjectType

Case Btn

rectObject.Left = 0

rectObject.Right = iWidth

Select Case iObjectState

Case iEnabled

rectObject.Top = 0

rectObject.Bottom = iHeight

Case iDisabled

rectObject.Top = iHeight * 2

rectObject.Bottom = iHeight * 3

Case iPressed

rectObject.Top = iHeight

rectObject.Bottom = iHeight * 2

End Select

Case ChkBox, RadioBtn

rectObject.Left = 0

rectObject.Right = iWidth

Select Case iObjectState

Case iUnchecked

rectObject.Top = 0

rectObject.Bottom = iHeight

Case iDisabled

rectObject.Top = iHeight * 2

rectObject.Bottom = iHeight * 3

Case iChecked

rectObject.Top = iHeight

rectObject.Bottom = iHeight * 2

Case iChecked_iDisabled

rectObject.Top = iHeight * 3

rectObject.Bottom = iHeight * 4

End Select

If Len(sCaption) > 0 Then

iCaptionX = iX + 15

iCaptionY = iY

bDrawCaption = True

Else

bDrawCaption = False

End If

Case MinBtn, MaxBtn, CloseBtn, RestoreBtn

rectObject.Left = 0

rectObject.Right = iWidth

Select Case iObjectState

Case iEnabled

rectObject.Top = 0

rectObject.Bottom = iHeight

Case iPressed

rectObject.Top = iHeight

rectObject.Bottom = iHeight * 2

End Select

Case FrameWnd

'see framesample.bmp for how this should look as a normal VB control

lOldColor = objSurface.GetFillColor

objSurface.SetForeColor cFrameGrey

objSurface.DrawLine iX + iParentX, iY + iParentY, iX + iParentX + iWidth, iY + iParentY

objSurface.DrawLine iX + iParentX, iY + iParentY, iX + iParentX, iY + iParentY + iHeight

objSurface.DrawLine iX + iParentX, iY + iParentY + iHeight, iX + iParentX + iWidth, iY + iParentY + iHeight

objSurface.DrawLine iX + iParentX + iWidth, iY + iParentY, iX + iParentX + iWidth, iY + iParentY + iHeight

objSurface.SetForeColor vbWhite

objSurface.DrawLine iX + iParentX + 1, iY + iParentY + 1, iX + iParentX + iWidth - 1, iY + iParentY + 1

objSurface.DrawLine iX + iParentX + 1, iY + iParentY + 1, iX + iParentX + 1, iY + iParentY + iHeight - 1

objSurface.DrawLine iX + iParentX, iY + iParentY + iHeight + 1, iX + iParentX + iWidth + 1, iY + iParentY + iHeight + 1

objSurface.DrawLine iX + iParentX + iWidth + 1, iY + iParentY, iX + iParentX + iWidth + 1, iY + iParentY + iHeight + 1

objSurface.SetForeColor lOldColor

If Len(sCaption) > 0 Then

iCaptionX = iX + iParentX + 10

iCaptionY = iY + ParentY - 8

bDrawCaption = True

Else

bDrawCaption = False

End If

Case BaseWindow

'Nothing needed here since we use the base rectangle

If Len(sCaption) > 0 Then

iCaptionX = iX + 10

iCaptionY = iY + 5

bDrawCaption = True

Else

bDrawCaption = False

End If

End Select

lBltFlags = DDBLT_WAIT

'Need to use transparency if it's a radio button

If iObjectType = RadioBtn Then

lBltFlags = DDBLT_WAIT Or DDBLT_KEYSRC

End If

'Don't blit if it a frame control

If iObjectType <> FrameWnd Then lRet = objSurface.Blt(rectBitmap, objBitmap, rectObject, lBltFlags)

If bDrawCaption Then

lOldColor = objSurface.GetForeColor

objSurface.SetForeColor RGB(255, 255, 255)

objSurface.DrawText iCaptionX, iCaptionY, sCaption, False

objSurface.SetForeColor lOldColor

End If

For iLp = 1 To colChildren.Count

colChildren(iLp).DrawObject objSurface

Next iLp

Exit Function

DrawObjectErr:

objErr.WriteLine "Error drawing " & sName & " - " & Err.Description

Exit Function

End Function

As you can see the changes are pretty extensive. The function now has to handle the two new controls and the new Caption member. The RadioBtn is fairly simple since it pretty much acts like the ChkBox inside the class. If you looked at the files included with this project you抣l notice that there抯 no bitmap for the frame object. That抯 because it uses DirectDraw抯 DrawLine function to draw the outline of the frame. Open the framesample.bmp file and zoom in to see how it抯 actually drawn.

We抳e added a function in the class to return a child object. This is necessary to change the RadioBtn抯 ObjectState member when one radio button in a group is clicked:

Public Function GetChild(ByVal sKey As String) As clsWindow

Set GetChild = colChildren(sKey)

End Function

The AddChild method was changed to specify a key when adding a child in order to support this function:

Public Sub AddChild(clsChild As clsWindow)

colChildren.Add clsChild, clsChild.WindowName

End Sub

Take a minute to run the app and play with the new controls. There are a couple of things that could be changed to make the class better but I抣l leave them up to you. Some things could be:

Add members to allow the font type, size and color to be changed.

Add a Style member to allow the ChkBox and RadioBtn objects to be graphical similar to the way the VB controls act.

Change the frame drawing to not draw under the caption.

Adapt the class to add a LabelWnd object type. This would just use the Caption property

That抯 it for this lesson. I hope it抯 been fun and interesting. The next lesson will attempt to cover listboxes and comboboxes. Hope to see you there.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有