Простой ScreenSaver на C#
// Отображение заставки
static void ShowScreenSaver()
{
foreach (Screen screen in Screen.AllScreens)
{
frmMain screensaver = new frmMain(screen.Bounds);
screensaver.Show();
}
}
}
}
Файл FormMain.cs.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace SampleScreenSaver
{
public partial class frmMain : Form
{
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out Rectangle lpRect);// Флаг предварительного просмотра
private bool IsPreviewMode;Graphics graph;
Pen pen;// Цвет фона
SolidBrush bg;// Цвет фигуры
SolidBrush figure;// Величина радиуса
int radius;Random rnd;
// Координаты для рисования
Point OriginalLocation = new Point(int.MaxValue, int.MaxValue);public frmMain(Rectangle Bounds)
{
InitializeComponent();this.Bounds = Bounds;
// Скрыть отображение курсора мышки
Cursor.Hide();
}// Дескриптор для отображения диалогового окна
// с предвартельным просмотром заставки
public frmMain(IntPtr PreviewHandle)
{
InitializeComponent();// Окно предварительного просмотра в качестве родительского
SetParent(this.Handle, PreviewHandle);// сделать дочерним окном, когда диалог выбора заставки
// закроется, то оно также закроется
SetWindowLong(this.Handle, -16,
new IntPtr(GetWindowLong(this.Handle, -16) | 0x40000000));// установить размер окна в соответствии с размером вашего
// нового родительского окна
Rectangle ParentRect;
GetClientRect(PreviewHandle, out ParentRect);
this.Size = ParentRect.Size;this.Location = new Point(0, 0);
IsPreviewMode = true;
}private void frmMain_Load(object sender, EventArgs e)
{
// Выравнивание изображения по середине
pbLogo.Left = Bounds.Width / 2 - pbLogo.Width / 2;
pbLogo.Top = Bounds.Height / 2 - pbLogo.Height / 2;lblCopyright.Font = new Font("Microsoft Sans Serif", 10F);
// Выравнивание текста в нижнем правом углу окна
lblCopyright.Left = Bounds.Right - lblCopyright.Width;
lblCopyright.Top = Bounds.Bottom-20;graph = this.CreateGraphics();
pen = new Pen(Color.Green); // Цвет границы окружности
figure = new SolidBrush(Color.Transparent); // Цвет окружности
bg = new SolidBrush(Color.White); // Фон вокруг окружностиrnd = new Random();
radius = rnd.Next(4, 18); // Радиус окружности
int x, y;for (int i = 0; i < 24; i++)
{
x = rnd.Next(this.Width);
y = rnd.Next(this.Height);// вызываем функцию для прорисовки круга случайным образом,
// выбрав перед этим координаты центра
DrawCircle(x, y);
}tmrTimer.Enabled = true;
}// Рисование окружности по координатам его центра
private void DrawCircle(int x, int y)
{
int xC, yC;
xC = x - radius;
yC = y - radius;
graph.FillEllipse(figure, xC, yC, radius, radius);
graph.DrawEllipse(pen, xC, yC, radius, radius);
}private void tmrTimer_Tick(object sender, EventArgs e)
{
int x, y;