C# 使用gdi绘制自定义控件,自适应分辨率。

录制_2023_08_06_14_12_28_725

C# 使用gdi绘制自定义控件


解决cpu占用高的问题以及闪烁问题的解决方案
视频演示

问题描述

GDI+的效率其实没有GDI的效率高的, 一般的提高效率的方法也就那么多:

  1. 减少不必要的绘制, 区域刷新效果比全部绘制效率高很多, 比如只是一个控件需要重绘,我们就不应该让整个窗口或者说大于次区域的界面无效
  2. 不在显示区域的绘制工作能省就省, 主要绘制可视界面, 就是平时说的 脏矩形技术
  3. 使用内存绘制, 然后直接贴图(或者截取贴图), 另外如果某些工作是不变的,比如说背景, 那么就在第一次的时候绘制到内存dc, 以后直接取用
  4. 如果有可能可以使用Directx或者openGL

主要分为3步,
1.添加自适应分辨率类
2.绘制GDI图形
3.调用

1.添加自适应分辨率类

 class AutoSizeFormClass
    {
        //(1).声明结构,只记录窗体和其控件的初始位置和大小。
        public struct controlRect
        {
            public int Left;
            public int Top;
            public int Width;
            public int Height;
            public float FontSize;
            public string name;
        }
        //(2).声明 1个对象
        //注意这里不能使用控件列表记录 List nCtrl;,因为控件的关联性,记录的始终是当前的大小。
        //      public List oldCtrl= new List();//这里将西文的大于小于号都过滤掉了,只能改为中文的,使用中要改回西文
        public List<controlRect> oldCtrl = new List<controlRect>();
        int ctrlNo = 0;//1;
        //(3). 创建两个函数
        //(3.1)记录窗体和其控件的初始位置和大小,
        public void controllInitializeSize(Control mForm)
        {
            controlRect cR;
            cR.Left = mForm.Left; cR.Top = mForm.Top; cR.Width = mForm.Width; cR.Height = mForm.Height;cR.name=mForm.Name ;cR.FontSize = mForm.Font.Size;
            oldCtrl.Add(cR);//第一个为"窗体本身",只加入一次即可
            AddControl(mForm);//窗体内其余控件还可能嵌套控件(比如panel),要单独抽出,因为要递归调用
                              //this.WindowState = (System.Windows.Forms.FormWindowState)(2);//记录完控件的初始位置和大小后,再最大化
                              //0 - Normalize , 1 - Minimize,2- Maximize
        }
        private void AddControl(Control ctl)
        {
            foreach (Control c in ctl.Controls)
            {  
                //**放在这里,是先记录控件的子控件,后记录控件本身              
                controlRect objCtrl;              
                objCtrl.Left = c.Left; objCtrl.Top = c.Top; objCtrl.Width = c.Width; objCtrl.Height = c.Height; objCtrl.FontSize = c.Font.Size; objCtrl.name = c.Name;               
                oldCtrl.Add(objCtrl);
                //**放在这里,是先记录控件本身,后记录控件的子控件
                if (c.Controls.Count > 0)
                    AddControl(c);//窗体内其余控件还可能嵌套控件(比如panel),要单独抽出,因为要递归调用
            }
        }
        //(3.2)控件自适应大小,
        public void controlAutoSize(Control mForm,FormWindowState windowState= FormWindowState.Normal)
        {
            if (windowState == FormWindowState.Minimized)
                return;           
            if (ctrlNo == 0)
            { //*如果在窗体的Form1_Load中,记录控件原始的大小和位置,正常没有问题,但要加入皮肤就会出现问题,因为有些控件如dataGridView的的子控件还没有完成,个数少
              //*要在窗体的Form1_SizeChanged中,第一次改变大小时,记录控件原始的大小和位置,这里所有控件的子控件都已经形成
                controlRect cR;
                //  cR.Left = mForm.Left; cR.Top = mForm.Top; cR.Width = mForm.Width; cR.Height = mForm.Height;
                cR.Left = 0; cR.Top = 0; cR.Width = mForm.PreferredSize.Width; cR.Height = mForm.PreferredSize.Height; cR.FontSize = mForm.Font.Size; cR.name = mForm.Name;

                oldCtrl.Add(cR);//第一个为"窗体本身",只加入一次即可                
                AddControl(mForm);//窗体内其余控件可能嵌套其它控件(比如panel),故单独抽出以便递归调用             
            }        
            float wScale = (float)mForm.Width / (float)oldCtrl[0].Width;//新旧窗体之间的比例,与最早的旧窗体
            float hScale = (float)mForm.Height / (float)oldCtrl[0].Height;//.Height;
            ctrlNo = 1;//进入=1,第0个为窗体本身,窗体内的控件,从序号1开始
            AutoScaleControl(mForm, wScale, hScale);//窗体内其余控件还可能嵌套控件(比如panel),要单独抽出,因为要递归调用
        }
        private void AutoScaleControl(Control ctl, float wScale, float hScale)
        {
            int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;
            float ctrfontsize;
            //int ctrlNo = 1;//第1个是窗体自身的 Left,Top,Width,Height,所以窗体控件从ctrlNo=1开始
            foreach (Control c in ctl.Controls)
            { 
                //**放在这里,是先缩放控件的子控件,后缩放控件本身                            
                ctrLeft0 = oldCtrl[ctrlNo].Left;
                ctrTop0 = oldCtrl[ctrlNo].Top;
                ctrWidth0 = oldCtrl[ctrlNo].Width;
                ctrHeight0 = oldCtrl[ctrlNo].Height;
                ctrfontsize = oldCtrl[ctrlNo].FontSize;
                //c.Left = (int)((ctrLeft0 - wLeft0) * wScale) + wLeft1;//新旧控件之间的线性比例
                //c.Top = (int)((ctrTop0 - wTop0) * h) + wTop1;
                c.Left = (int)((ctrLeft0) * wScale);//新旧控件之间的线性比例。控件位置只相对于窗体,所以不能加 + wLeft1
                c.Top = (int)((ctrTop0) * hScale);//
                c.Width = (int)(ctrWidth0 * wScale);//只与最初的大小相关,所以不能与现在的宽度相乘 (int)(c.Width * w);
                c.Height = (int)(ctrHeight0 * hScale);//
                // 创建一个新的字体,按比例缩放原始字体大小
                Font scaledFont = new Font(c.Font.FontFamily, hScale * ctrfontsize);
                // 设置 Label 的字体为新创建的字体
                c.Font = scaledFont;
               
                //窗体内其余控件还可能嵌套控件(比如panel),要单独抽出,因为要递归调用
                if (c.Controls.Count > 0)
                    AutoScaleControl(c, wScale, hScale);
                if (ctl is DataGridView)
                {
                    DataGridView dgv = ctl as DataGridView;
                    Cursor.Current = Cursors.WaitCursor;

                    int widths = 0;
                    for (int i = 0; i < dgv.Columns.Count; i++)
                    {
                        dgv.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells);  // 自动调整列宽  
                        widths += dgv.Columns[i].Width;   // 计算调整列后单元列的宽度和                       
                    }
                    if (widths >= ctl.Size.Width)  // 如果调整列的宽度大于设定列宽  
                        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;  // 调整列的模式 自动  
                    else
                        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;  // 如果小于 则填充  

                    Cursor.Current = Cursors.Default;
                }              
                ctrlNo++;//累加序号
            }
        }
    }

2.创建自定义控件并绘制图形添加事件

 public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.UserPaint |
                      ControlStyles.AllPaintingInWmPaint, true);    
        }      
        float size1  = 9;

        public bool IsPaint { get; set; } = false;

        private int _count_H = 9;
        /// <summary>
        /// Get or set the width of the border
        /// </summary>
        [System.ComponentModel.Browsable(true),
        System.ComponentModel.Category("Appearance"),
        System.ComponentModel.Description("Set the border style")]
        public int count_H 
        {
            set
            {
                if (_count_H == value)
                    return;
                if (_count_H >0)
                {
                    _count_H = value;
                    int MaxSize = pictureBox1.Height < pictureBox1.Width ? pictureBox1.Height : pictureBox1.Width;
                    int MaxCnt = count_H < count_W ? count_H : count_W;
                    size1 = MaxSize / MaxCnt;
                    Invalidate();
                }
                else
                {
                    throw new ArgumentException("This value should be between 0 and 5");
                }
            }
            get 
            {
                return _count_H;
            }
        }
        private int _count_W = 9;
        /// <summary>
        /// Get or set the width of the border
        /// </summary>
        [System.ComponentModel.Browsable(true),
        System.ComponentModel.Category("Appearance"),
        System.ComponentModel.Description("Set the border style")]
        public int count_W 
        {
            set
            {
                if (_count_W == value)
                    return;
                if (_count_W > 0)
                {
                    _count_W = value;
                    int MaxSize = pictureBox1.Height < pictureBox1.Width ? pictureBox1.Height : pictureBox1.Width;
                    int MaxCnt = count_H < count_W ? count_H : count_W;
                    size1 = MaxSize / MaxCnt;
                    Invalidate();
                }
                else
                {
                    throw new ArgumentException("This value should be between 0 and 5");
                }
            }
            get
            {
                return _count_W;
            }
        } 
     
        private string _text = "label1";
        /// <summary>
        /// Get or set the width of the border
        /// </summary>
        [System.ComponentModel.Browsable(true),
        System.ComponentModel.Category("Appearance"),
        System.ComponentModel.Description("Set the border style")]
        public string TextValue
        {
            get
            {
                return _text;
            }
            set
            {
                if (_text == value)
                    return;
                if (_text != "")
                {
                    _text = value;
                    int MaxSize = pictureBox1.Height < pictureBox1.Width ? pictureBox1.Height : pictureBox1.Width;
                    int MaxCnt = count_H < count_W ? count_H : count_W;
                    size1 = MaxSize / MaxCnt;                                     
                    Invalidate();
                }
                else
                {
                    throw new ArgumentException("This value should be between 0 and 5");
                }
            }
        }
        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            int MaxSize = pictureBox1.Height < pictureBox1.Width ? pictureBox1.Height : pictureBox1.Width;
            int MaxCnt = count_H < count_W ? count_H : count_W;
            size1 = MaxSize / MaxCnt*0.75f;

            for (int i = 0; i < count_H; i++)
            {
                for (int j = 0; j < count_W; j++)
                {
                    int index = count_W * i + j;
                    string strtemp = $"{_text}{index}";
                    Point point = new Point((int)(j * (_text.Length * size1) + 10), (int)(2 + i * (size1 * 1.25f)));
                    Size size = new Size((int)(size1 * (_text.Length) * 0.75), (int)(size1 * 1.15));
                    Rectangle rectangle = new Rectangle(point, size);
                    var path = new GraphicsPath();
                    path.AddEllipse(rectangle);
                    path.CloseFigure();
                    var region = new Region(path);
                    if (region.IsVisible(e.Location))
                    {
                        MessageBox.Show(strtemp);
                    }
                    else
                    {
                        //pointLast = Point.Empty;
                    }
                }
            }
        }
        
        Bitmap bmp;
        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            //创建内存位图,将背景及圆形绘制在此内存位图上
            if (IsPaint) 
                return;
            bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            var g = Graphics.FromImage(bmp);         
            
            int MaxSize = pictureBox1.Height < pictureBox1.Width ? pictureBox1.Height : pictureBox1.Width;
            int MaxCnt = count_H < count_W ? count_H : count_W;
            size1 = MaxSize / MaxCnt * 0.75f;
           

            for (int i = 0; i < count_H; i++)
            {
                for (int j = 0; j < count_W; j++)
                {
                    int index = count_W * i + j;
                    PointF point = new PointF(j * (_text.Length * size1) + 5, 2 + i * (size1*1.25f));                   
                    g.DrawString($"{index}", new Font("宋体", size1), new SolidBrush(Color.Black), point);
                }
            }
            for (int i = 0; i < count_H; i++)
            {
                for (int j = 0; j < count_W; j++)
                {
                    int index = count_W * i + j;                  
                    Point point = new Point((int)(j * (_text.Length * size1) + 6), (int)(2 + i * (size1 * 1.25f)));
                    Size size = new Size((int)(size1*(_text.Length)*0.75), (int)(size1*1.15));
                    Rectangle rectangle = new Rectangle(point, size);
                    g.DrawRectangle(new Pen(Color.Red), rectangle);
                }
            }
            pictureBox1.Image = bmp;
            //bmp.Dispose();
            g.Dispose();
            IsPaint = true;
        }
    }

3.调用,添加一个form窗体

 public partial class Form1 : Form
    {
       
        public Form1()
        {
            InitializeComponent();          
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.UserPaint |
                      ControlStyles.AllPaintingInWmPaint, true);
        }
      
        AutoSizeFormClass AutoSizeFormClass = new AutoSizeFormClass();

      

        private void Form1_Load(object sender, EventArgs e)
        {
            AutoSizeFormClass.controllInitializeSize(this);
        }

        private void Form1_Resize(object sender, EventArgs e)
        {           
            userControl11.IsPaint = false;
            AutoSizeFormClass.controlAutoSize(this, this.WindowState);         
        }           
    }

# 代码连接:本文的源代码