【问题标题】:How can I resize a user control designer to fit control in it?如何调整用户控件设计器的大小以适应其中的控件?
【发布时间】:2021-12-23 03:39:43
【问题描述】:

我创建了一个新的用户控件并在它的设计器中添加了一个图片框。

这是带有图片框的用户控件设计器的屏幕截图

User Control designer with pictureBox in the middle

然后我将这段代码添加到用户控件中

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 Extract
{
    public partial class Slider : UserControl
    {
        public float Height;
        public float Min = 0.0f;
        public float Max = 1.0f;

        private float defaultValue = 0.1f;

        public Slider()
        {
            InitializeComponent();

            
        }

        private void sliderControl_Paint(object sender, PaintEventArgs e)
        {
            float bar_size = 0.45f;
            float x = Bar(defaultValue);
            int y = (int)(sliderControl.Height * bar_size);

            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.FillRectangle(Brushes.DimGray, 0, y, sliderControl.Width, y / 2);
        }

        private float Bar(float value)
        {
            return (sliderControl.Width - 24) * (value - Min) / (float)(Max - Min);
        }
    }
}

然后当我将控件从工具箱拖到 form1 设计器时:

The control in form1 designer

问题是当我尝试在 form1 设计器中调整控件的大小时,我只想看到 pictureBox 而没有它周围的控件。但是当我调整它的大小时,pictureBox 向下移动太多然后进入了:

Resized the control in form1 designer

如果我在用户控件设计器中调整控件大小以适应图片框大小,那么当在 form1 设计器中拖动它时,我根本看不到图片框。我需要调整它的大小才能看到图片框。

我认为没有动态调整大小。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    这个解决方案效果很好。

    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 Extract
    {
        public partial class Slider : UserControl
        {
            public float Height;
            public float Min = 0.0f;
            public float Max = 1.0f;
    
            private float defaultValue = 0.1f;
    
            public Slider()
            {
                InitializeComponent();            
            }
    
            private void sliderControl_Paint(object sender, PaintEventArgs e)
            {
                float bar_size = 0.45f;
                float x = Bar(defaultValue);
                int y = (int)(sliderControl.Height * bar_size);
    
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                e.Graphics.FillRectangle(Brushes.DimGray, 0, y, sliderControl.Width, y / 2);
            }
    
            private float Bar(float value)
            {
                return (sliderControl.Width - 24) * (value - Min) / (float)(Max - Min);
            }
    
            protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
    
                MaintainPictureBoxSize();
            }
    
            private void MaintainPictureBoxSize()
            {
                sliderControl.SizeMode = PictureBoxSizeMode.Normal;
    
                sliderControl.Location = new Point();
                sliderControl.Size = new Size();
    
                var clientSize = this.ClientSize;
    
                if (sliderControl.Image == null)
                    sliderControl.Size = clientSize;
                else
                {
                    Size s = sliderControl.Image.Size;
                    sliderControl.Size = new Size(
                        clientSize.Width > s.Width ? clientSize.Width : s.Width,
                        clientSize.Height > s.Height ? clientSize.Height : s.Height);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      相关资源
      最近更新 更多