NEWS

[C#] Di chuyển control Runtime và lưu layout trên winform

[C#] Di chuyển control Runtime và lưu layout trên winform
Đăng bởi: Thảo Meo - Lượt xem: 4587 08:19:42, 05/01/2021EBOOK

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:

runtime_move_control_csharp

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!

DOWNLOAD SOURCE

Tags: move control runtime c#move control c# winform

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Di chuyển control Runtime và lưu layout trên winform
Đăng bởi: Thảo Meo - Lượt xem: 4587 08:19:42, 05/01/2021EBOOK