- [POWER AUTOMATE] Hướng dẫn gởi tin nhắn zalo từ file Excel - No code
- [C#] Chia sẻ code lock và unlock user trong domain Window
- [SOFTWARE] Giới thiệu bộ phần mềm tính Kết Cấu Thép HatteSale, Mộng Đơn, Dầm, Sàn, Móng Cọc, Vách, Xà Gồ, Tính Tải Trọng
- [DEVEXPRESS] Vẽ Biểu Đồ Stock Chứng Khoán - Công Cụ Thiết Yếu Cho Nhà Đầu Tư trên Winform
- [C#] Hướng dẫn bảo mật ứng dụng 2FA (Multi-factor Authentication) trên Winform
- [C#] Hướng dẫn convert HTML code sang PDF File trên NetCore 7 Winform
- [C#] Hướng dẫn viết ứng dụng chat với Gemini AI Google Winform
- Hướng dẫn khóa file bằng nhiều process id, không cho xóa tập tin
- Hướng dẫn cách tạo Product Id cho ứng dụng phần mềm XXXXX-XXXXX-XXXXX-XXXXX
- [SQLSERVER] Hướng dẫn tạo script sql từ ứng dụng Sqlserver management Studio
- [C#] Hướng dẫn sử dụng thư viện AutoITx lấy id và password Ultraviewer trên winform
- [VB.NET] Hướng dẫn lấy thông tin tài khoản đăng nhập windows và khởi động lại ứng dụng ở chế độ Administrator
- [C#] Sử dụng thư viện Polly gửi lại request api khi request bị lỗi hay rớt mạng
- [DEVEXPRESS] Chia sẻ source code tạo báo cáo report in tem nhãn label trên C# winform
- [DEVEXPRESS] Hướng dẫn vẽ biểu đồ Bar Chart trên Winform
- [C#] Tạo form đăng nhập và đăng ký với hiệu ứng Sliding Animation Effect
- [C#] Hướng dẫn tạo thanh toán đơn hàng qua mã vạch VietQR sử dụng API PayOS hoàn toàn miễn phí
- [C#] Hướng dẫn ghi log ra RichTextBox giống Console trên Winform sử dụng thư viện Serilog
- [C#] Hướng dẫn cách tạo mã QR Code trên file Excel
- [VB.NET] Gởi tin nhắn và file đính kèm qua ứng dụng gởi tin nhắn Whats App
[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!