c steps for creating screen saver of subtitle screensaver

  • 2020-06-01 10:53:15
  • OfStack

The screen saver's extension is "scr", but it is actually an executable "exe" file. But it is also a unique "exe" file. Let's take a look at the whole process of writing a screen saver using C#.

2. C# writing subtitles to display the key steps of the screensaver and the solution:
(1) the form of the setup program meets the requirements of the screen saver:
Because the screen saver is an executable program, so when writing the screen saver, the first step is to design according to the executable program. But screen savers have their own characteristics. For example, screen savers are full
The entire screen, and no infinity. The taskbar cannot be displayed while the screen saver is running. In the program design to achieve these characteristics, the key is to set some properties of the form. The following is for the form properties
Setting can meet the requirements of screen saver, as follows:


this.Name = "ScreenSaver" ;
// The form runs without boundaries 
this.FormBorderStyle = FormBorderStyle.None ;
// The program does not appear on the taskbar after it is run 
this.ShowInTaskbar = false ;
// After the form runs, maximize and fill the entire screen 

(2) make the characters constantly move on the screen:
The implementation of the character on the screen like the character screensaver 1 constantly moving, is done by a timer. The constantly moving character is actually an Label component. When the Label component is set to display the characters,
It also sets the characters to move on the screen. The timer subtracts a fixed value from the Label component's abscissa every 1 interval, so that the position of the Label component changes under the timer drive
The effect is that the characters are constantly moving.
When the Label component has moved to the outside of the screen, the Label component's horizontal coordinate is returned to the starting value (that is, the far right side of the screen), and the Label component's vertical coordinate is located on the screen
The top of the curtain, if at the top, resets the vertical position in the middle; If in the middle, reset at the bottom; If at the bottom, reset at the top. After these judgments, characters can not only be implemented from right to left
Move, you can also change the character into the screen position. It also enriches the screen saver. In fact, to make these decisions, you must first know the working area of the screen, because only the working area of the screen is known first
, which is able to set the horizontal and vertical coordinates of the Label component. Here is the code to implement this step:


// Get the working area of the computer screen 
Rectangle ssWorkArea = Screen.GetWorkingArea ( this ) ;
lblMarquee.Location = new Point ( ssWorkArea.Width - iDistance ,
lblMarquee.Location.Y ) ;
// According to the label 
lblMarquee.Visible = true ;
//  increase 2 A pixel point , You can modify it speed To change the movement speed of the tag 
iDistance += speed ;
//  If the label is already off the screen, reposition the label to the right of the screen 
if ( lblMarquee.Location.X <= -( lblMarquee.Width ) )
{
//Reset the distance to 0.
iDistance = 0 ;
// Determine if the position of the tag is at the top, and if so, relocate to the middle 
if ( lblMarquee.Location.Y == 0)
lblMarquee.Location = new Point ( lblMarquee.Location.X , ( ssWorkArea.Height / 2 ) ) ;
// Determine if the position of the tag is in the middle and, if so, reposition to the bottom  
else if ( lblMarquee.Location.Y == ssWorkArea.Height / 2 )
lblMarquee.Location = new Point ( lblMarquee.Location.X , ssWorkArea.Height - lblMarquee.Height ) ;
// Reposition to the top  
else
lblMarquee.Location = new Point ( lblMarquee.Location.X , 0 ) ;
} 

(3) check the state of keyboard and mouse and make sure to exit the screen saver:
When the screen saver is running, it is turned off when the keyboard is pressed or the mouse is moved. In actual programming, the "KeyDown" event can be used to detect if the keyboard is being pressed. in
In the program, the way to judge whether the mouse moves is to record the mouse position (X and Y) at the beginning of the program through two global variables. When the mouse moves, the mouse position is determined and recorded
Is there any discrepancy in the position? If so, close the screensaver. The function of the following code is to detect the mouse movement and end the screen saver, as follows:


//  Record where the mouse has just moved 
if ( ixStart == 0 && iyStart == 0 )
{
ixStart = e.X ;
iyStart = e.Y ;
return ;
}
// Determines whether the mouse position has changed since the screen saver was run 
else if ( e.X != ixStart || e.Y != iyStart )
{
Cursor .Show ( ) ; 
timerSaver.Enabled = false ;
Application .Exit ( ) ;
};

Among them, "ixStar" and "iyStart" are the starting position of the mouse when recording the program.

(4). Response of Windows to screen saver Settings:
When the screen saver is added to the Windows system, you can set the screen saver properties through the Windows system. Since we have made this screen saver, there is no option to set it, but it has to be correct
The Settings button of Windows responds. To do this, all you need to know is to press the "Settings" button on the Windows system to set the screen saver, and then pass the word "/c" to the screensaver
Operator. To set the password, the "/a" character is passed, from which the code can be obtained:


if (args [ 0 ].Substring ( 0 , 2 ).Equals ( "/c" ) )
{
MessageBox.Show(" This screen saver has no options to set! "," with C# Manufacturing screen saver " ,
MessageBoxButtons.OK ,MessageBoxIcon.Information ) ;
Application.Exit ( ) ;
}
else if ( args [ 0 ] == "/a" )
{
MessageBox.Show(" This screen saver does not have the option to set a password! "," with C# Manufacturing screen saver " ,
MessageBoxButtons.OK ,MessageBoxIcon.Information ) ;
Application.Exit ( ) ;
}

3. Conclusion:
Since the screen saver is Windows program, when compiling, to produce Windows program, the compilation command is as follows:
csc /t:winexe screen.cs
After correctly compiling "Screen.cs", you can get the "Screen.exe" file, rename "Screen.exe" to "Screen.scr", and copy it to the "System32" directory of Windows. So far the character screensaver
You're done with everything from writing to configuration.


Related articles: