C # / Net -. screensaver create
Introduction In this article I would like you can program with C # just a screen saver. I will therefore give in this article the basics. Building on this, I have a manual for a "transparent screen saver". All I have programmed with Visual Studio from Microsoft. A screen saver
is a Windows Form created. The frame is turned off, so we have only a bare Windows Form. This form is adapted to load the screen saver on the size of the screen.
preparation
First we create in Visual Studio is a new Windows Forms Application ". One form is thus created automatically. Since we set the screensaver is no border around the window we want to disable this in the Properties under
FormBorderStyle by setting the value here
None. Then we choose
Color a background color for our screen saver. Alternatively can be integrated as a background image. Thus, the last preparations already completed. Create
main program
Main method
First we create the Main method. From this latter is subsequently started, the screen saver. If you want to start under Windows screen saver, you can choose three options: Test
,
Configure, Install
. These options give it the screen saver call one of the following values:
/ c ,
/ s or / p .
These values we want to catch and then start each desired option. We use the following IF statement:
using System;
using System.Windows.Forms;
ScreensaverHowTo
namespace {static class
screensaver
{[STAThread]
static void Main (string [] args) {
if (args.length> 0) {
if (args [0]. ToLower () . Trim (). Substring (0,2) == "/ c") {
/ / Configurations dialog loading ...
} else if (args [0]. ToLower () == "/ s")
{/ / Load screen saver ...
} else if (args [0]. ToLower () == "/ p") {
Music / / Preview ...
}}
else
{/ / No arguments specified ... Screen saver start anyway ...
/ / This case occurs, for example by double-clicking on the. scr file.
}}
}}
screen saver start
to start the screen saver we call the following method:
System.Windows.Forms.Application.Run
enter as transfer parameters, we then specify the name of our pre-created form.
It should look something like this:
System.Windows.Forms.Application.Run (new screensaver form ());
I will ask for this screen saver is no configuration available, I give the argument "/ c" the message from:
MessageBox.Show ("This screen saver has no options that presupposes they can." "Screen Saver", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
like in all other cases, I let the screen saver's normal, so I've abbreviated the code above a bit.
static void Main (string [] args) {
if (args.length> 0) {
if (args [0]. ToLower (). Trim (). Substring (0,2) == " / c ")
{MessageBox.Show (" This screen saver has no options that presupposes they may, "" Screen Saver ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation).
return;}
}
System.Windows. Forms.Application.Run (new screensaver form ());}
Screen saver to screen size
Since the screen saver as already described above is only a simple Windows Form without border, we must bring this form now to the size of the screen. We do it just right here, so the screen saver runs later on as many screens. So even if you are working on two monitors. So we then have a proper multi-screen screen saver.
this we must add together when the form is loaded all the coordinates of the screens. We do this as follows: private void
ScreenSaverForm_Load (object sender, System.EventArgs e) {
int miny = 0;
int maxY = 0;
int minX = 0;
int maxX = 0;
foreach (Screen screen in Screen.AllScreens)
{/ / The size of all screens
determine if (screen.Bounds.Left \u0026lt;minX) minX = screen.Bounds . Left if (screen.Bounds.Right> maxX) maxX =
screen.Bounds.Right;
if (screen.Bounds.Top \u0026lt;miny) miny = screen.Bounds.Top if (screen.Bounds. Bottom> maxY) maxY =
screen.Bounds.Bottom;}
Location = new Point (minX, miny);
Height = maxY - miny;
width = maxX - minX;
Cursor.Hide ();
TopMost = true;}
We first calculate the X coordinate and then the Y coordinates. This we must do for each screen. The new values if they are larger than already read over monitors. We have determined these values, the form is enlarged accordingly.
also hides the mouse pointer so that we now need not mer.
screen saver
stop the screen saver to stop again, we catch up the following events: Pressing a button, moving the mouse and pressing a mouse button. For this purpose we created
following events, which are linked to the form: catch
for the keyboard we express the above a key. If anything made from the keyboard we close the open form of simple, and an end as the screen saver.
private void ScreenSaverForm_KeyDown (object sender, System.Windows.Forms.KeyEventArgs e) {
Close ();}
The same is true for the mouse, except that here are still moving the mouse caught. (! MouseXY.IsEmpty)
private (object sender, System.EventArgs e) {void OnMouseEvent
if
{if (MouseXY = new Point (eX, eY)) {
Close ();
} if (e.Clicks> 0) {
Close ();}
}
MouseXY = new Point (eX, eY);}
is first checked whether a change has occurred in the mouse, this is the case, we see that the mouse was moved or whether a key was pressed. In both cases, we close the form so that the screen saver is ended.
The variable MouseXY is the position of the cursor at the time of the call defined. Changed this position, we close the screen saver.
Note: Do not forget to define the variable
MouseXY .
private Point MouseXY;
create screen saver.
A screen saver is actually a normal executable file. This file must be renamed just yet, so it afterward. Scr as the extension is. Thus, the operating system detects that there is a screen saver.
Conclusion
So we've created a screen saver that covers the screen with any color. It is also possible to use instead of a color image. To find out how to create a transparent screen saver or how to lock the computer can then read the article
Transparent screensaver create they want a little variety to the screen saver bring you read further
Geekpedia .