Visual Basic is a great developers language it allows you to create applications that are very functional and to do so in a quick way. This is why people love VB6. However, at times it can be hard to figure out how to interact directly with the Windows Operating System. To do this we need to make API calls. Anyone who has delved into using VB and API's knows that it can be a little cumbersome at times. However, this tutorial demonstrates how you can make the API calls from your Visual Basic application and create a full fledged screen saver!
A screen saver at its core is very simple. It is simply a window that covers the entire screen. In Visual Basic 6 this is accomplished by createing a form that will be displayed full size overtop of everything else. Lets start with the simple. Create a new VB6 project with just the main form. Set the properties to the following:
Property Setting
Caption ""
ControlBox False
MinButton False
MaxButton False
BorderStyle 0 - None
WindowState 2 - Maximized
BackColor &H0
Private Declare Function SetWindowPos Lib "user32" _
(ByVal hwnd As Long, ByVal hWndInsertAfter As Long, _
ByVal x As Long, ByVal Y As Long, ByVal cx As Long, _
ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function ShowCursor Lib "user32" _
(ByVal bShow As Long) As Long
We are going to use these functions a little later. First lets create a helper subroutine that will always keep our form on top.
Sub AlwaysOnTop (FrmID As Form, OnTop As Boolean)
Const SWP_NOMOVE = 2
Const SWP_NOSIZE = 1
Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
If OnTop Then
OnTop = SetWindowPos(FrmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
Else
OnTop = SetWindowPos(FrmID.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
End If
End Sub
The next part is the load-up of our Screen Saver: put these lines in the Load event of the form. This will handle things when the Windows OS launches our screen saver.
Select Case LCase(Left(Command, 2))
Case "/p": End
Case "/s"
Case Else: Me.Hide: frmSettings.Show: Exit Sub
End Select
Dim x As Integer
Call AlwaysOnTop(Me, True)
x = ShowCursor(False)
The form will always stay on top and the cursor will desappear.
When the Screen Saver unloads, we must do the inverse. Put these lines in the Unload event of the form.
Dim x As Integer
x = ShowCursor(True)
The first 5 lines of the Form_Load event are needed to make the Screen Saver work, work as an example or show its settings. Windows tells the Screen Saver what to do through command line parameters: /S tells the Screen Saver to start working, /P tells the Screen Saver to start working in a preview area, and no parameters makes the Screen Saver show its settings dialog. So, you may create a form called frmSettings that gives the opportunity to set Screen Saver parameters and to save them in a INI file or in the Registry; that dialog must unload the program when it is finished.
Another important thing to do, is to handle the events that unload the Screen Saver. A Screen Saver is usually unloaded when we move the mouse or hit a key, so we'll put the instruction Unload Me in the KeyPress event of the form. For the MouseMove or MouseClick events, use these lines:
Static Count As Integer
Count = Count + 1
If Count > 5 Then
Unload Me
End If
This makes sure that the mouse is moved atleast a little bit before it exits. Otherwise your screen saver will exit too easily (with just a bump of the desk or something).
Well, it is your turn now! Can you creat an original and good Screen Saver. You can use controls (even OCXs) to create the graphic, and add code anywhere you want. If you want, you can download this example, located in the samples section of the site, demostrating a full working Screen Saver.
Installing The Screen Saver
When you have finished creating the look of the Screen Saver, it's the moment to compile it and bring it to life. Once you have compiled your Screen Saver, change its EXE extension to SCR. Then, copy it to the Windows\System folder.
To use the Screen Saver, go in the Control Panel, double-click the icon Screen, select the second tab (Screen Saver) and select your program from the Screen Saver dropdown list. Then, click OK.
With these properties, the form will be black, will have no border, buttons or title bar and will cover all the screen.
So, create a form and give it the above properties.
Now we must do two other things. First, each screen saver is a form that stays on the top of all others. Second, we notice that, during the display of a Screen Saver, the mouse cursor desappear.
To make our program do these two things, we first put these two lines in the declarations section of the form:
from http://www.vb6.us/tutorials/createing-visual-basic-screen-saver
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment