Screen Saver Development

Screen Saver Development

Question:
What all do I need to know to create a screen saver in Visual Basic?

Answer:
Windows screensavers are easy and fun to write in Visual Basic, but there are some very important things to know. Experts can go straight to the Code Section of this document.

A Windows screensaver is nothing more that a regular Windows executable file that has been renamed with the .scr extension. In Visual Basic, when you are making the executable file, you will need to set the Application Title in the Make EXE dialog box. This Application Title MUST be set to “SCRNSAVE title”, where title is the text you want displayed in the Control Panel screensaver dropdown box.

Command Line Arguments

When Windows starts up a screensaver it calls it with the “/s” argument, and when it wants to Setup the screensaver it uses the “/c” argument. So, we use a code module called SCRNSAV.BAS. In SCRNSAV.BAS you will see that a Select Case statement is used to capture this argument. You will need to change the Startup Form in the options|Project Dialog Box to Sub Main.

Telling Windows that the Saver is Running

How long Windows waits before loading the screensaver is specified in the Control Panel. But if your screensaver doesn’t tell Windows that it is running, Windows will reload the screensaver after that time passes again, even though the screensaver is already running. At first I thought I could remedy this situation by using VB 3.0’s App object. The App.PrevInstance property will tell you whether or not there is a previous instance loaded.

This should’ve worked, and I got many comments saying that I must have messed something up, but it didn’t. For some reason, with the screensaver this kills both instances, not just the second. But there is a way out. To fix this I found a Windows API call which tells Windows that the screensaver is inactive, so don’t load one. To use this API you need to use the API call SystemParametersInfo. This function is used to change system wide parameters, such as whether or not the screensaver is active. Be careful when using this call, since changes are permanent. You will need to make sure that your screensaver turns the screensaver back off when it has ended.See Sub Main, and Sub ExitNice in the Code Section.

Hiding The Cursor

When you write a screensaver, you’ll want it to hide the mouse cursor as well as whatever else your saver does. To do this you need to use the API call – ShowCursor. When ShowCursor( False ) is called, the cursor is hidden; when ShowCursor( True ) is called, the cursor is re-displayed. The Windows cursor is a shared object, so if your process hides it your process needs to redisplay it as well. See Code section.

Knowing when to end

When your screensaver ends is up to you, but generally you’ll want it to end if any of the following occur: mouse moves, button pressed, key pressed. To do this you will need to call a routine to exit properly from each of these events in your screensaver form. See SaverForm. You need to call this routine because this is where the other half of the SystemParametersInfo call is made. If this is left out, the screensaver won’t run again after it wakes up. Another problem is that the MouseMove message is sent if the cursor is over the form, REGARDLESS if it is moved or not. So, you need to check to see if it has moved somehow. See the Code Section for my solution. (Not necessarily the prettiest.

Code

ScrnSave.Bas

declarations    DefInt A-Z    Const SWP_NOSIZE = 1    Const SWP_NOMOVE = 2    Const SPI_SETSCREENSAVEACTIVE = 17    Declare Function ShowCursor Lib “User” (ByVal bShow As Integer) As Integer    Declare Sub SetWindowPos Lib “User” (ByVal hWnd, ByVal After, ByVal X, ByVal Y, ByVal cx, ByVal cy, ByVal Flags)    Declare Function SystemParametersInfo Lib “User” (ByVal uAction As Integer, ByVal uparam As Integer, lpvParam As Any, ByVal fuWinIni      As Integer) As Integer  Sub Main    Select Case Command$      Case “/s”, “/S”        Res = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 0, ByVal 0&, 0)        Load SaverForm        SetWindowPos SaverForm.hWnd, -1, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE        ok = DoEvents()      Case “/c”, “/C”        ConfigForm.Show    End Select  End Sub
You may also need to add some initialization code for whatever your screensaver does.
Sub ExitNice    Res = ShowCursor(True)  ‘Turn the cursor back on    ‘reset screensaver    Res = SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, 1, ByVal 0&, 0)     End  End Sub
Saver.Frm
  Form_Load    WindowState = 2		‘Maximize the screensaver    Me.Show		‘Show the form    This = ShowCursor(False)	‘Hide the cursor

Form_MouseMove If (OldX = 0) And (OldY = 0) Then OldX = X OldY = Y Exit Sub End If If (OldX <> X) Or (OldY <> Y) Then ExitNice Else OldX = 0 OldX = 0 End If Form_Click ExitNice Form_MouseDown ExitNice Form_KeyDown ExitNice Form_KeyPress ExitNice

Config.Frm

Windows will pass the “/c” argument to Sub Main if the “Setup” option is chosen from control panel. Here you can setup specific options for your screensaver. You might want to save these options in a .ini file (win.ini or your own). Its up to you! If your Config.Frm has a “Test” feature which starts the screensaver from the Config form, then you will need to be careful about remembering to turn on the cursor after the screensaver starts, and then turn it off before it ends.

Sources:

Conger, James L.., The Wait Group’s Windows API Bible: The Definitive Programmer’s Reference. The Wait Group: 1993. ISBN 1-878739-15-8VBZ: The Electronic Journal on Visual Basic. Copyright 1993 User Friendly, Inc. Issue 01: January/February 1993

Disclaimer/Distribution:

This information is provided free of charge, and may be freely distributed. If you use portions of this document elsewhere, please indicate where you got it. All of the information here has been used and tested by me in Visual Basic 3.0 Professional. Use at your own risk. Visual Basic and Microsoft Windows are registered trademarks of Microsoft Corp.Some small further info can be found in the Microsoft Knowledge Base (at the Microsoft Web Site). The article number is Q146907. It seems (according to this article) that in VB4.0 you can only create screen savers with the 16-bit version?! Seems a trifle silly to me. If anyone has the method to do this in 32-bit mode I’m sure the rest of the readers would be interested.

devx-admin

devx-admin

Share the Post:
Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023,

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed

Poland Energy Future

Westinghouse Builds Polish Power Plant

Westinghouse Electric Company and Bechtel have come together to establish a formal partnership in order to design and construct Poland’s inaugural nuclear power plant at the Lubiatowo-Kopalino site in Pomerania.

EV Labor Market

EV Industry Hurting For Skilled Labor

The United Auto Workers strike has highlighted the anticipated change towards a future dominated by electric vehicles (EVs), a shift which numerous people think will result in job losses. However,

Soaring EV Quotas

Soaring EV Quotas Spark Battle Against Time

Automakers are still expected to meet stringent electric vehicle (EV) sales quotas, despite the delayed ban on new petrol and diesel cars. Starting January 2023, more than one-fifth of automobiles

Affordable Electric Revolution

Tesla Rivals Make Bold Moves

Tesla, a name synonymous with EVs, has consistently been at the forefront of the automotive industry’s electric revolution. The products that Elon Musk has developed are at the forefront because

Sunsets' Technique

Inside the Climate Battle: Make Sunsets’ Technique

On February 12, 2023, Luke Iseman and Andrew Song from the solar geoengineering firm Make Sunsets showcased their technique for injecting sulfur dioxide (SO₂) into the stratosphere as a means

AI Adherence Prediction

AI Algorithm Predicts Treatment Adherence

Swoop, a prominent consumer health data company, has unveiled a cutting-edge algorithm capable of predicting adherence to treatment in people with Multiple Sclerosis (MS) and other health conditions. Utilizing artificial

Personalized UX

Here’s Why You Need to Use JavaScript and Cookies

In today’s increasingly digital world, websites often rely on JavaScript and cookies to provide users with a more seamless and personalized browsing experience. These key components allow websites to display

Geoengineering Methods

Scientists Dimming the Sun: It’s a Good Thing

Scientists at the University of Bern have been exploring geoengineering methods that could potentially slow down the melting of the West Antarctic ice sheet by reducing sunlight exposure. Among these

why startups succeed

The Top Reasons Why Startups Succeed

Everyone hears the stories. Apple was started in a garage. Musk slept in a rented office space while he was creating PayPal with his brother. Facebook was coded by a

Bold Evolution

Intel’s Bold Comeback

Intel, a leading figure in the semiconductor industry, has underperformed in the stock market over the past five years, with shares dropping by 4% as opposed to the 176% return

Semiconductor market

Semiconductor Slump: Rebound on the Horizon

In recent years, the semiconductor sector has faced a slump due to decreasing PC and smartphone sales, especially in 2022 and 2023. Nonetheless, as 2024 approaches, the industry seems to

Elevated Content Deals

Elevate Your Content Creation with Amazing Deals

The latest Tech Deals cater to creators of different levels and budgets, featuring a variety of computer accessories and tools designed specifically for content creation. Enhance your technological setup with

Learn Web Security

An Easy Way to Learn Web Security

The Web Security Academy has recently introduced new educational courses designed to offer a comprehensible and straightforward journey through the intricate realm of web security. These carefully designed learning courses

Military Drones Revolution

Military Drones: New Mobile Command Centers

The Air Force Special Operations Command (AFSOC) is currently working on a pioneering project that aims to transform MQ-9 Reaper drones into mobile command centers to better manage smaller unmanned

Tech Partnership

US and Vietnam: The Next Tech Leaders?

The US and Vietnam have entered into a series of multi-billion-dollar business deals, marking a significant leap forward in their cooperation in vital sectors like artificial intelligence (AI), semiconductors, and

Huge Savings

Score Massive Savings on Portable Gaming

This week in tech bargains, a well-known firm has considerably reduced the price of its portable gaming device, cutting costs by as much as 20 percent, which matches the lowest

Cloudfare Protection

Unbreakable: Cloudflare One Data Protection Suite

Recently, Cloudflare introduced its One Data Protection Suite, an extensive collection of sophisticated security tools designed to protect data in various environments, including web, private, and SaaS applications. The suite

Drone Revolution

Cool Drone Tech Unveiled at London Event

At the DSEI defense event in London, Israeli defense firms exhibited cutting-edge drone technology featuring vertical-takeoff-and-landing (VTOL) abilities while launching two innovative systems that have already been acquired by clients.

2D Semiconductor Revolution

Disrupting Electronics with 2D Semiconductors

The rapid development in electronic devices has created an increasing demand for advanced semiconductors. While silicon has traditionally been the go-to material for such applications, it suffers from certain limitations.

Cisco Growth

Cisco Cuts Jobs To Optimize Growth

Tech giant Cisco Systems Inc. recently unveiled plans to reduce its workforce in two Californian cities, with the goal of optimizing the company’s cost structure. The company has decided to

FAA Authorization

FAA Approves Drone Deliveries

In a significant development for the US drone industry, drone delivery company Zipline has gained Federal Aviation Administration (FAA) authorization, permitting them to operate drones beyond the visual line of

Mortgage Rate Challenges

Prop-Tech Firms Face Mortgage Rate Challenges

The surge in mortgage rates and a subsequent decrease in home buying have presented challenges for prop-tech firms like Divvy Homes, a rent-to-own start-up company. With a previous valuation of

Lighthouse Updates

Microsoft 365 Lighthouse: Powerful Updates

Microsoft has introduced a new update to Microsoft 365 Lighthouse, which includes support for alerts and notifications. This update is designed to give Managed Service Providers (MSPs) increased control and

Website Lock

Mysterious Website Blockage Sparks Concern

Recently, visitors of a well-known resource website encountered a message blocking their access, resulting in disappointment and frustration among its users. While the reason for this limitation remains uncertain, specialists