- [C#] Hướng dẫn giải nén file *.rar với tiến trình progress bar winform
- [C#] Chia sẻ source code make Crazy Error Message Effect Bomb Windows
- [C#] Lập trình ứng dụng theo mô hình MVP Model-View-Presenter Pattern Winform
- [C#] Giới thiệu và những thứ hay ho từ thư viện System.Reactive của Microsoft
- [C#] Hướng dẫn tạo ứng dụng Chat với GPT sử dụng Open AI API
- [DEVEXPRESS] Tạo month picker trên DateEdit Winform C#
- [DATABASE] Cách sử dụng và lưu ý khi sử dụng khóa ngoại (Foreign Key) trong Sqlserver
- [C#] Garbage Collector (GC) là gì? Cách giải phóng bộ nhớ trên ứng dụng Winform khi các đối tượng không còn sử dụng
- [C#] Cách tính độ tương phản màu sắc Contrast Color mà con người có thể nhìn thấy được
- [C#] Hướng dẫn mã hóa mật khẩu tài khoản ứng dụng đúng chuẩn Men
- [C#] Sử dụng Open AI Chat GPT viết ứng dụng Count down timer có hiệu ứng trên winform
- [DATABASE] Chia sẻ dữ liệu Pantone Color sql và json api
- [SQLSERVER] Tạo mã sản phẩm tự động như: SP0001, SP0002, SP0003... sử dụng Trigger
- [C#] Hướng dẫn kiểm tra phiên bản NET Framework cài đặt ở máy tính
- [C#] Hướng dẫn đọc file excel đơn giản sử dụng thư viện Epplus
- [C#] ConcurrentBag là gì và cách sử dụng nó trong lập trình bất đồng bộ
- [C#] AutoResetEvent là gì và cách sử dụng
- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
[C#] Viết ứng dụng Screen Saver đơn giản trong Winform
Xin chào các bạn, bài viết hôm nay mình sẽ demo ứng dụng Screen Saver đơn giản bằng ngôn ngữ lập trình C# winform.
[C#] Viết ứng dụng Screen Saver đơn giản trong Winform
Screen Saver là một trình ứng dụng bảo vệ màn hình, khi người dùng không thao tác một thời gian thì sẽ hiển thị.
File Screen saver có phần mở rộng tập tin là *.scr, và được lưu trữ ở thư mục C:\Windows\SysWOW64
Khi các bạn build ứng dụng demo Screen saver xong, các bạn chỉ cần đuôi tập tin *.exe => *.scr
Sau đó, các bạn có thể right click chuột vào file scr và bấm cài đặt để tiến hình cài đặt tập tin screen saver của mình.
Và dưới đây là form cấu hình screen saver, chúng ta nhập text để thay đổi và nó sẽ lưu trữ và Regedit của Windows.
Trong bài viết này sẽ có 3 file:
Đầu tiên source code cho file program.cs
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace ScreenSaver
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length > 0)
{
string firstArgument = args[0].ToLower().Trim();
string secondArgument = null;
if (firstArgument.Length > 2)
{
secondArgument = firstArgument.Substring(3).Trim();
firstArgument = firstArgument.Substring(0, 2);
}
else if (args.Length > 1)
secondArgument = args[1];
if (firstArgument == "/c")
{
Application.Run(new SettingsForm());
}
else if (firstArgument == "/p")
{
if (secondArgument == null)
{
MessageBox.Show("Sorry, but the expected window handle was not provided.",
"ScreenSaver", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
IntPtr previewWndHandle = new IntPtr(long.Parse(secondArgument));
Application.Run(new ScreenSaverForm(previewWndHandle));
}
else if (firstArgument == "/s")
{
ShowScreenSaver();
Application.Run();
}
else
{
MessageBox.Show("Sorry, but the command line argument \"" + firstArgument +
"\" is not valid.", "ScreenSaver",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
else
{
Application.Run(new SettingsForm());
}
}
static void ShowScreenSaver()
{
foreach (Screen screen in Screen.AllScreens)
{
ScreenSaverForm screensaver = new ScreenSaverForm(screen.Bounds);
screensaver.Show();
}
}
}
}
File ScreenSaverForm.cs
, file này để hiển thị Screen Saver
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace ScreenSaver
{
public partial class ScreenSaverForm : Form
{
#region Win32 API functions
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[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);
#endregion
private Point mouseLocation;
private bool previewMode = false;
private Random rand = new Random();
public ScreenSaverForm()
{
InitializeComponent();
}
public ScreenSaverForm(Rectangle Bounds)
{
InitializeComponent();
this.Bounds = Bounds;
}
public ScreenSaverForm(IntPtr PreviewWndHandle)
{
InitializeComponent();
SetParent(this.Handle, PreviewWndHandle);
SetWindowLong(this.Handle, -16, new IntPtr(GetWindowLong(this.Handle, -16) | 0x40000000));
Rectangle ParentRect;
GetClientRect(PreviewWndHandle, out ParentRect);
Size = ParentRect.Size;
Location = new Point(0, 0);
textLabel.Font = new System.Drawing.Font("Arial", 6);
previewMode = true;
}
private void ScreenSaverForm_Load(object sender, EventArgs e)
{
LoadSettings();
Cursor.Hide();
TopMost = true;
moveTimer.Interval = 1000;
moveTimer.Tick += new EventHandler(moveTimer_Tick);
moveTimer.Start();
}
private Random rnd = new Random();
private void moveTimer_Tick(object sender, System.EventArgs e)
{
textLabel.Left = rand.Next(Math.Max(1, Bounds.Width - textLabel.Width));
textLabel.Top = rand.Next(Math.Max(1, Bounds.Height - textLabel.Height));
Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
textLabel.ForeColor = randomColor;
}
private void LoadSettings()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Demo_ScreenSaver");
if (key == null)
textLabel.Text = "C# Screen Saver";
else
textLabel.Text = (string)key.GetValue("text");
}
private void ScreenSaverForm_MouseMove(object sender, MouseEventArgs e)
{
if (!previewMode)
{
if (!mouseLocation.IsEmpty)
{
if (Math.Abs(mouseLocation.X - e.X) > 5 ||
Math.Abs(mouseLocation.Y - e.Y) > 5)
Application.Exit();
}
mouseLocation = e.Location;
}
}
private void ScreenSaverForm_KeyPress(object sender, KeyPressEventArgs e)
{
if (!previewMode)
Application.Exit();
}
private void ScreenSaverForm_MouseClick(object sender, MouseEventArgs e)
{
if (!previewMode)
Application.Exit();
}
}
}
Và cuối cùng là file cấu hình screen saver, SettingsForm.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Security.Permissions;
namespace ScreenSaver
{
public partial class SettingsForm : Form
{
public SettingsForm()
{
InitializeComponent();
LoadSettings();
}
private void LoadSettings()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Demo_ScreenSaver");
if (key == null)
textBox.Text = "C# Screen Saver";
else
textBox.Text = (string)key.GetValue("text");
}
private void SaveSettings()
{
RegistryKey key = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Demo_ScreenSaver");
key.SetValue("text", textBox.Text);
}
private void okButton_Click(object sender, EventArgs e)
{
SaveSettings();
Close();
}
private void cancelButton_Click(object sender, EventArgs e)
{
Close();
}
}
}
Thanks for watching!