![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
ScheduleWakeup takes as parameters the time the alarm should occur and an object to receive the alarm. The subroutine stores these values for later use and then enables a timer control. The controls Timer event checks to see if the alarms time has arrived. If it has, it disables the timer control and invokes the objects WakeUp subroutine. The WakeUp subroutine performs whatever actions are necessary at the alarms scheduled time.
:
Object that notifies the user of an appointment.
Private WakeupObject As Object
The time at which we should notify the user.
Private WakeupTime As Date
:
Public Sub ScheduleWakeup(wakeup_time As Date, wakeup_obj As Object)
Save the wakeup time and object for later use.
WakeupTime = wakeup_time
Set WakeupObject = wakeup_obj
Activate the timer.
AlarmTimer.Interval = 60000
AlarmTimer.Enabled = True
Trigger the Timer event now in case the
wakeup time has already passed.
AlarmTimer_Timer
End Sub
Private Sub AlarmTimer_Timer()
If not time yet, wait for the next Timer event.
If Date + Time < WakeupTime Then Exit Sub
Disable the timer.
AlarmTimer.Enabled = False
Activate the alarm object.
WakeupObject.WakeUp
Set WakeupObject = Nothing
End Sub
AppointmentBooks AlarmV view class uses this system to set alarms. The AlarmV object is notified whenever appointment data changes and when an appointment is created or deleted. It is also notified when the user is warned about an appointment so the AlarmV object can prepare to notify the user of the next appointment. AlarmV uses the following code to schedule an alarm to notify the user of the next appointment. The models GetNextAppointment function locates the earliest appointment that has not yet been reported to the user. The code is reasonably straightforward so it is not presented here. You can see it on the compact disk.
Private Sub ResetAlarm()
Get the next appointment.
TheModel.GetNextAppointment _
next_appt, next_time, next_warn
If there is no next appointment, do nothing.
If next_appt Is Nothing Then Exit Sub
Schedule the wakeup call.
CalendarForm.ScheduleWakeup next_warn, Me
End Sub
Creating Nonrectangular FormsIrregularly shaped screens can be eye-catching, but they also have a few disadvantages. AppointmentBooks splash screen, for example, has no title bar or borders. This means the user cannot move it or resize it. The user does not need to move or resize a splash screen, so this is not a problem in this case. This could cause trouble for other forms, however, so a program must use irregularly shaped forms with caution. The main step in creating an irregularly shaped form is to set the forms window region. This region determines which parts of the form are visible. The Windows API provides several functions for creating round, elliptical, rectangular, and polygonal regions. AppointmentBook uses the CreatePolygonRgn function to create a polygonal region. Once the program has created a region, it uses the SetWindowRgn API function to restrict the forms visible portion to that region. When AppointmentBooks splash screen loads, it invokes the ShapeForm subroutine. This routine initializes an array of the user-defined data type POINTAPI. Each POINTAPI variable has two fields, X and Y, that define the coordinates of a point. The array of POINTAPI variables defines the polygonal boundary for the form. After initializing the POINTAPI values, ShapeForm uses the CreatePolygonRgn API function to create the polygonal region. Next, subroutine ShapeForm uses the SetWindowRgn API function to set the forms background area to the region created by the Polygon function. To make the edges of the form easy to see, it also uses the Polygon function to draw a black border along the regions edges. It uses a special pen with a width of two to ensure that the lines are not too thin.
Sub ShapeForm()
Const NUM_PTS = 24
Dim status As Long
Dim pts(1 To NUM_PTS) As POINTAPI
Dim rgn As Long
Dim old_rgn As Long
Dim pen As Long
Dim old_pen As Long
pts(1).X = 132
pts(1).Y = 155
:
More points omitted.
:
pts(24).X = 82
pts(24).Y = 191
Set the form region.
rgn = CreatePolygonRgn(pts(1), NUM_PTS, WINDING)
old_rgn = SetWindowRgn(hWnd, rgn, True)
Create a pen to draw the regions edge.
pen = CreatePen(PS_SOLID, 2, vbBlack)
old_pen = SelectObject(hDC, pen)
status = Polygon(hDC, pts(1), NUM_PTS)
pen = SelectObject(hDC, old_pen)
status = DeleteObject(pen)
End Sub
To make the splash screen easy to use, the form provides two public methods, ShowSplash and ShowAbout, that display the form. ShowSplash presents the form as a splash screen. It enables the forms timer so it can keep the screen visible for a certain minimum amount of time. This is accomplished just as it is by the splash screen used in ExpenseReporter. ShowSplash then hides the forms OK button because it is not needed when the form is presented as splash screen. Next, ShowSplash presents the form and uses the Refresh method to force itself to display immediately. The form is presented modelessly so the application can continue loading the applications main form and initializing itself. The call to the Refresh method is necessary to guarantee that the splash screen is completely drawn before the main program continues initializing.
Public Sub ShowSplash()
SplashTimer.Enabled = True
CmdOk.Visible = False
Me.Show vbModeless
Me.Refresh
End Sub
When AppointmentBooks main form loads, it invokes ShowSplash and then continues preparing the main form. When it has finished, it calls the splash screens ReadyToUnload subroutine. The splash screen unloads itself when ReadyToUnload is called or when a certain minimum amount of time has passed, whichever happens second.
Private Sub Form_Load()
Disable the main form so the user cannot
interact with it. The splash screen will
enable the form when it unloads.
Enabled = False
Display the splash screen.
Splash.ShowSplash
Arrange controls, initialize data, etc.
:
Make the form visible.
Me.Show
Load the appointment data.
This will draw the month area and notify the other views.
TheCalendarModel.LoadData
Indicate we have finished initialization.
Splash.ReadyToUnload = True
End Sub
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|