NEWS

[C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy

[C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
Đăng bởi: Thảo Meo - Lượt xem: 2467 15:54:14, 17/05/2023C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách di chuyển và thay đổi kích thước của một control trực tiếp khi ứng dụng đang chạy trên C#, Winform.

[C# - VB.NET] How to Move and Resize Control Run time in Winform

MOVE_RESIZE_THUMB

Khi các bạn muốn thiết kế một ứng dụng mà mong muốn người dùng cuối có thể setup layout chủ động thì bài viết này sẻ giúp ích cho các bạn.

Như các bạn muốn hiển thị layout bàn ghế của một quán cafe chẳng hạn, và khi triển khai ứng dụng người dùng có thể tự động sắp xếp layout.

Và sau đó, chúng ta sẽ lưu trữ layout và hiển thị lại khi load file layout vào.

Dưới đây là video demo ứng dụng:

Đầu tiên, các bạn tạo cho mình một component BoxWrapper.cs bằng C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MoveAndResizeRunTime
{
    public class BoxWrapper : Component
    {
        #region Fields

        IDesignerHost designerHost;
        Control rootDesignControl;
        DesignSurface surface;
        Control parentControl;
        Panel designPanel;

        #endregion

        #region Propeties
        private Control _SelectedControl;
        public Control SelectedControl
        {
            get { return _SelectedControl; }
            set { IsCustomization = false; _SelectedControl = value; OnChanged(); }
        }

        private Control _DesignContainer;
        public Control DesignContainer
        {
            get { return _DesignContainer; }
            set { _DesignContainer = value; OnChanged(); }
        }

        private bool _IsCustomization;
        public bool IsCustomization
        {
            get { return _IsCustomization; }
            set
            {
                if (value && !CanCustomize(value)) return;
                { _IsCustomization = value; OnChanged(); }
            }
        }


        #endregion

        #region Methods
        void CopyBounds(Control target, Control source)
        {
            Point p = source.PointToScreen(new Point(0, 0));
            if (target.Parent != null)
            {
                p = target.Parent.PointToClient(p);
            }
            target.Location = p;
            target.Width = source.Width;
            target.Height = source.Height;
        }

        static bool IsNull(object obj)
        {
            return obj == null;
        }

        void AddToList(IList list, Control c)
        {
            if (c.Dock != DockStyle.None) return;
            if (c.Parent == null) return;
            if (c.Name == string.Empty) return;
            list.Add(c);
        }

        void TraverseControls(IList list, Control c)
        {
            AddToList(list, c);
            foreach (Control control in c.Controls) TraverseControls(list, control);
        }

        public List<Control> GetAvailableControls()
        {
            List<Control> list = new List<Control>();
            TraverseControls(list, DesignContainer);
            return list;
        }


        bool NeedDestroyCustomization()
        {
            return !IsNull(rootDesignControl) && !IsNull(designPanel.Parent);
        }

        bool CanCustomize(bool isCutomization)
        {
            return !(IsNull(DesignContainer) || DesignMode || IsNull(SelectedControl) || (!isCutomization) || IsNull(SelectedControl.Parent));
        }

        Panel CreateDesignPanel()
        {
            Panel designPanel = designerHost.CreateComponent(typeof(Panel)) as Panel;
            designPanel.BackColor = Color.Transparent;
            designPanel.Padding = new Padding(2, 2, 2, 2);
            return designPanel;
        }

        void CreateDesignSurface()
        {
            surface = new DesignSurface();
            designerHost = surface.GetService(typeof(IDesignerHost)) as IDesignerHost;
            rootDesignControl = designerHost.CreateComponent(typeof(UserControl)) as Control;
            rootDesignControl.Dock = DockStyle.Fill;
            rootDesignControl.BackColor = ColorTranslator.FromHtml("#fff7e6");//Color.AliceBlue;
            Control c = surface.View as Control;
            c.Dock = DockStyle.Fill;
            c.BackColor = Color.White;
            c.Location = new Point(15, 25);
            c.Parent = DesignContainer;
        }

        void StartControlCustomization()
        {
            designPanel = CreateDesignPanel();
            rootDesignControl.Controls.Add(designPanel);
            CopyBounds(designPanel, SelectedControl);
            parentControl = SelectedControl.Parent;
            SelectedControl.Parent = designPanel;
            SelectedControl.Dock = DockStyle.Fill;
        }

        void StartCustomiztion()
        {
            if (!CanCustomize(IsCustomization)) return;
            CreateDesignSurface();
            StartControlCustomization();
        }

        void FinishControlCustomization()
        {
            SelectedControl.Parent = parentControl;
            SelectedControl.Dock = DockStyle.None;
            CopyBounds(SelectedControl, designPanel);
        }

        void EndCustomization()
        {
            if (!NeedDestroyCustomization()) return;
            FinishControlCustomization();
            DestroyDesignSurface();
        }


        void DestroyDesignSurface()
        {
            rootDesignControl.Dispose();
        }

        void OnChanged()
        {
            if (IsCustomization) StartCustomiztion();
            else EndCustomization();
        }
        #endregion
    }
}

Class BoxWrapper.vb bằng VB.NET

using System.Collections.Generic;
using System.ComponentModel;

using System.Drawing;

using System.Windows.Forms;
using System.ComponentModel.Design;

using System.Collections;

namespace WindowsApplication1
{
    public class Customizator : Component
    {
        #region Fields

        IDesignerHost designerHost;
        Control rootDesignControl;
        DesignSurface surface;
        Control parentControl;
        Panel designPanel;

        #endregion

        #region Propeties
        private Control _SelectedControl;
        public Control SelectedControl
        {
            get {  return _SelectedControl; }
            set { IsCustomization = false; _SelectedControl = value; OnChanged(); }
        }

        private Control _DesignContainer;
        public Control DesignContainer
        {
            get { return _DesignContainer; }
            set { _DesignContainer = value; OnChanged(); }
        }

        private bool _IsCustomization;
        public bool IsCustomization
        {
            get { return _IsCustomization; }
            set
            {
                if (value && !CanCustomize(value)) return;
                { _IsCustomization = value; OnChanged(); }
            }
        }


        #endregion

        #region Methods
        void CopyBounds(Control target, Control source)
        {
            Point p = source.PointToScreen(new Point(0, 0));
            if (target.Parent != null)
            {
                p = target.Parent.PointToClient(p);
            }
            target.Location = p;
            target.Width = source.Width;
            target.Height = source.Height;
        }

        static bool IsNull(object obj)
        {
            return obj == null;
        }

        void AddToList(IList list,  Control c)
        {
            if (c.Dock != DockStyle.None) return;
            if (c.Parent == null) return;
            if (c.Name == string.Empty) return;
            list.Add(c);
        }

        void TraverseControls(IList list, Control c)
        {
            AddToList(list, c);
            foreach (Control control in c.Controls) TraverseControls(list, control);
        }

       public List<Control> GetAvailableControls()
        {
            List<Control> list = new List<Control>();
            TraverseControls(list, DesignContainer);
            return list;
        }

        
        bool NeedDestroyCustomization()
        {
            return !IsNull(rootDesignControl) && !IsNull(designPanel.Parent);
        }

        bool CanCustomize(bool isCutomization)
        {
            return !(IsNull(DesignContainer) || DesignMode || IsNull(SelectedControl) || (!isCutomization) || IsNull(SelectedControl.Parent));
        }

        Panel CreateDesignPanel()
        {
            Panel designPanel = designerHost.CreateComponent(typeof(Panel)) as Panel;
            designPanel.BackColor =Color.Transparent;
            designPanel.Padding = new Padding(2, 2, 2, 2);
            return designPanel;
        }

        void CreateDesignSurface()
        {
            surface = new DesignSurface();
            designerHost = surface.GetService(typeof(IDesignerHost)) as IDesignerHost;
            rootDesignControl = designerHost.CreateComponent(typeof(UserControl)) as Control;
            rootDesignControl.Dock = DockStyle.Fill;
            rootDesignControl.BackColor = Color.AliceBlue;
            Control c = surface.View as Control;
            c.Dock = DockStyle.Fill;
            c.BackColor = Color.White;
            c.Location = new Point(15, 25);
            c.Parent = DesignContainer;
        }

        void StartControlCustomization()
        {
            designPanel = CreateDesignPanel();
            rootDesignControl.Controls.Add(designPanel);
            CopyBounds(designPanel, SelectedControl);
            parentControl = SelectedControl.Parent;
            SelectedControl.Parent = designPanel;
            SelectedControl.Dock = DockStyle.Fill;
        }

        void StartCustomiztion()
        {
            if (!CanCustomize(IsCustomization)) return;
            CreateDesignSurface();
            StartControlCustomization();
        }

        void FinishControlCustomization()
        {
            SelectedControl.Parent = parentControl;
            SelectedControl.Dock = DockStyle.None;
            CopyBounds(SelectedControl, designPanel);
        }

        void EndCustomization()
        {
            if (!NeedDestroyCustomization()) return;
            FinishControlCustomization();
            DestroyDesignSurface();
        }


        void DestroyDesignSurface()
        {
            rootDesignControl.Dispose();
        }

        void OnChanged()
        {
            if (IsCustomization) StartCustomiztion();
            else EndCustomization();
        }
        #endregion
    }
}

Và cách sử dụng chúng ta chỉ cần SelectedControl Target vào  control nào muốn thay đổi và set IsCustomization = true là ok

Code C# trên Form1.cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MoveAndResizeRunTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEnable_Click(object sender, EventArgs e)
        {
            boxWrapper1.SelectedControl = dataGridView1;
            boxWrapper1.IsCustomization = true;
          
        }

        private void btnDisable_Click(object sender, EventArgs e)
        {
           boxWrapper1.IsCustomization = false;
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Di chuyển và thay đổi kích thước Control Winform khi ứng dụng đang chạy
Đăng bởi: Thảo Meo - Lượt xem: 2467 15:54:14, 17/05/2023C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.