- [DEVEXPRESS] Chia sẻ code các tạo report in nhiều hóa đơn trên XtraReport C#
- [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
[C#] Hướng dẫn tạo Particle Generator trên winform
Xin chào các bạn, bài viết hôm nay mình tiếp tục chia sẻ đến các bạn source code cách tạo Particle Generator in winform C#.
C# Particle Generator in winform
Giao diện demo ứng dụng:
Video demo:
Ứng dụng sẽ tự động tạo các điểm chấm ngẫu nhiên trên màn hình.
Các bạn có thể sử dụng code này để chạy giống Screen Saver trên Window.
Source code c#:
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace ParticleGenerator
{
public partial class MainForm : Form
{
private readonly Random randomNum = new Random();
private Graphics graphics;
public MainForm()
{
InitializeComponent();
graphics = CreateGraphics();
Size = SystemInformation.PrimaryMonitorSize;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
graphics = CreateGraphics();
}
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
Application.Exit();
}
}
private void Render()
{
if (checkBoxClear.Checked)
graphics.Clear(BackColor);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.High;
int xMinPos = 0, xMaxPos = 0, yMinPos = 0, yMaxPos = 0;
SetParticleBoundarys(ref xMinPos, ref xMaxPos, ref yMinPos, ref yMaxPos);
float particleWidth = (float)numberBoxParticleSize.Value;
using (var brush = new SolidBrush(panelColor.BackColor))
{
//Generate particles according to the max/min pos, count, and particle width.
for (int i = 0; i < numberBoxParticles.Value; i++)
{
var xPos = randomNum.Next(xMinPos, xMaxPos);
var yPos = randomNum.Next(yMinPos, yMaxPos);
graphics.FillEllipse(brush, xPos, yPos, particleWidth, particleWidth);
}
}
}
private void SetParticleBoundarys(ref int xMinPos, ref int xMaxPos, ref int yMinPos, ref int yMaxPos)
{
// Apply multiplier to the width and height to get smaller bounds.
xMaxPos = (int)(Width * numberBoxDensity.Value + 0.5m);
yMaxPos = (int)(Height * numberBoxDensity.Value + 0.5m);
// Get the difference between the size of the form and the new bounds.
// Then do some wacky calculations to center the particles.
xMinPos = ((Width - xMaxPos) / 4) + ((Width - xMaxPos) / 2);
yMinPos = ((Height - yMaxPos) / 4) + ((Height - yMaxPos) / 2);
}
private void buttonClose_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void buttonSave_Click(object sender, EventArgs e)
{
if (BackgroundImage == null)
{
MessageBox.Show("Image not generated.", Application.ProductName,
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
using (SaveFileDialog dialog = new SaveFileDialog())
{
dialog.Filter = "Bitmap Format|*.bmp";
dialog.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
if (dialog.ShowDialog() == DialogResult.OK)
{
BackgroundImage.Save(dialog.FileName);
}
}
}
private void buttonColor_Click(object sender, EventArgs e)
{
using (ColorDialog dialog = new ColorDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
panelColor.BackColor = dialog.Color;
}
}
}
private void buttonGenerate_Click(object sender, EventArgs e)
{
Render();
}
}
}
Thanks for watching!