- [C#] Giới thiệu Component WebView2 của Microsoft
- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
- [DATABASE] Hướng dẫn sử dụng Hàm ASCII trong sqlserver
- [C#] Hướng dẫn fix lỗi Visual Studio 2022 not Support Target Net Framework 4.5.2
- [C#] Giới thiệu thư viện Sunny UI thiết kế giao diện trên Winform
- [DATABASE] Hướng dẫn thêm và cập nhật Extended Property Column trong Table Sqlserver
- [DEVEXPRESS] Hướng dẫn sử dụng Vertical Gridview để hiển thị thông tin sản phẩm
- [C#] Hướng dẫn sử dụng Json Schema để Validate chuỗi string có phải json
- [C#] Hướng dẫn sử dụng công cụ Clean Code trên Visual Studio
- [C#] Hướng dẫn Drag and Drop File vào RichTextBox
- [C#] Hướng dẫn tạo hiệu ứng Karaoke Text Effect Winform
- [C#] Sử dụng thư viện ZedGraph vẽ biểu đồ Line, Bar, Pie trên Winform
- [DATABASE] Hướng dẫn sort sắp xếp địa chỉ IP trên sqlserver sử dụng hàm PARSENAME
- [C#] Theo dõi sử kiện process Start hay Stop trên Winform
- [ASP.NET] Chia sẻ source code chụp hình sử dụng camera trên website
- [C#] Chạy ứng dụng trên Virtual Desktop (màn hình ảo) Winform
- [C#] Mã hóa và giải mã Data Protection API trên winform
- [C#] Hướng dẫn tạo Gradient Background trên Winform
- [DATABASE] Hướng dẫn convert Epoch to DateTime trong sqlserver
[C#] Di chuyển control Runtime và lưu layout trên winform
Xin chào các bạn, bài viết hôm nay mình sẽ tiếp tục hướng dẫn các bạn cách di chuyển vị trí Location của các control runtime trên C# winform.
[C#] Mouse Move Control Runtime Winform
Khi các bạn thiết kế giao diện ứng dụng lên, mình sẽ cho phép người dùng di chuyển vị trí từng control theo mong muốn của người dùng.
Và lưu lại vị trí cấu hình của từng control trên Winform thành một file layout .json, và 1 file base layout dùng để Reset cấu hình vị trí của từng control trên winform c#.
Giao diện demo ứng dụng di chuyển control trực tiếp đang chạy C# Winform:
Cách thực hiện chúng ta sẽ quét tất cả các control trên Winform, và thêm vào cho chúng sự kiện event MouseMove, để di chuyển các control Winform.
Full source code Move Control Runtime C#:
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MoveControlRunTime
{
public partial class Form1 : Form
{
List<Control> m_MovebleControls;
List<LayoutControl> layoutControls;
public Form1()
{
InitializeComponent();
}
public void SaveBaseLayout()
{
var layoutControls = new List<LayoutControl>();
foreach (Control control in this.Controls)
{
var layoutControl = new LayoutControl();
layoutControl.Name = control.Name;
layoutControl.Location = control.Location;
layoutControls.Add(layoutControl);
}
var jData = JsonConvert.SerializeObject(layoutControls, Formatting.Indented);
File.WriteAllText("reset.json", jData);
}
private void Form1_Load(object sender, EventArgs e)
{
SaveBaseLayout();
m_MovebleControls = new List<Control>();
foreach (Control control in this.Controls)
{
m_MovebleControls.Add(control);
}
foreach (Control control in m_MovebleControls)
{
control.MouseMove += new MouseEventHandler(Control_MouseMove);
}
if (File.Exists("layout.json"))
{
var listControlLayout = JsonConvert.DeserializeObject<List<LayoutControl>>(File.ReadAllText("layout.json"));
foreach (Control control in this.Controls)
{
LayoutControl item = (LayoutControl)listControlLayout.Where(x => x.Name == control.Name).FirstOrDefault();
control.Location = item.Location;
}
}
}
private void Control_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
Control controlToMove = (Control)sender;
controlToMove.BringToFront();
controlToMove.Location = new Point(controlToMove.Location.X + e.Location.X - 10,
controlToMove.Location.Y + e.Location.Y - 10);
}
}
private void btnSavelayout_Click(object sender, EventArgs e)
{
layoutControls = new List<LayoutControl>();
foreach (Control control in this.Controls)
{
var layoutControl = new LayoutControl();
layoutControl.Name = control.Name;
layoutControl.Location = control.Location;
layoutControls.Add(layoutControl);
}
var jData = JsonConvert.SerializeObject(layoutControls, Formatting.Indented);
File.WriteAllText("layout.json", jData);
MessageBox.Show("Save Layout Success!");
}
private void button2_Click(object sender, EventArgs e)
{
if (File.Exists("reset.json"))
{
var listControlLayout = JsonConvert.DeserializeObject<List<LayoutControl>>(File.ReadAllText("reset.json"));
foreach (Control control in this.Controls)
{
LayoutControl item = (LayoutControl)listControlLayout.Where(x => x.Name == control.Name).FirstOrDefault();
control.Location = item.Location;
}
}
}
}
public class LayoutControl
{
public string Name { set; get; }
public Point Location { set; get; }
}
}
Thanks for watching!