龍五1618 2020-09-13 02:04 采纳率: 0%
浏览 185
已结题

WinForm自制控件会比微软自带的控件反应慢,请问该如何优化?

本人自制了个控件,基于微软原来的控件制作的,但是这个控件的CheckBox响应速度会比微软的控件慢很多,一直不明白是什么原理,请问各位大神,这是什么原因导致的?该如何优化下?

using System;
using System.Windows.Forms;

namespace Pro.Frm.Control
{
    internal class MyCheckListBox : CheckedListBox
    {
        public delegate void ItemBoxCheckedEventHanlder(object sender, Models.CheckListBoxItemCheckBoxEventArgs mouseEventArgs);
        public event ItemBoxCheckedEventHanlder ItemBoxChecked;
        private bool isUserCheck = false;
        public MyCheckListBox()
        {
            this.DoubleBuffered = true;
        }
        protected override void OnItemCheck(ItemCheckEventArgs ice)
        {
            if (this.isUserCheck)
            {
                this.isUserCheck = false;
                ice.NewValue = ice.CurrentValue;
            }
            else
            {
                base.OnItemCheck(ice);
            }
        }
        private void OnItemBoxChecked(Models.CheckListBoxItemCheckBoxEventArgs boxItemCheckBoxEventArgs)
        {
            switch (this.GetItemCheckState(boxItemCheckBoxEventArgs.CheckIndex))
            {
                case CheckState.Unchecked:
                    this.SetItemCheckState(boxItemCheckBoxEventArgs.CheckIndex, CheckState.Checked);
                    break;
                case CheckState.Checked:
                    this.SetItemCheckState(boxItemCheckBoxEventArgs.CheckIndex, CheckState.Unchecked);
                    break;
                case CheckState.Indeterminate:
                    this.SetItemCheckState(boxItemCheckBoxEventArgs.CheckIndex, CheckState.Indeterminate);
                    break;
                default:
                    break;
            }
        }
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x0201)///鼠标左键按下消息
            {
                this.SuspendLayout();
                uint lPararm = (uint)m.LParam;
                int xPoint = Convert.ToInt32(lPararm & 0xFFFF);///转换鼠标X坐标
                int yPoint = Convert.ToInt32(lPararm >> 16);///转换鼠标Y坐标
                int index = this.IndexFromPoint(xPoint, yPoint);
                if (xPoint < 15)
                {
                    Models.CheckListBoxItemCheckBoxEventArgs model = new Models.CheckListBoxItemCheckBoxEventArgs(MouseButtons.Left, xPoint, yPoint, index);
                    this.ItemBoxChecked?.Invoke(this, model);
                    this.OnItemBoxChecked(model);
                }
                else
                {
                    this.SelectedIndex = index;
                    this.isUserCheck = true;
                }
                this.ResumeLayout(false);
            }
            else
            {
                base.WndProc(ref m);
            }
        }
    }
}

  • 写回答

2条回答 默认 最新

  • threenewbee 2020-09-13 08:36
    关注

    你的代码有绘图的代码么?从你给出的代码看,并没有什么需要耗时操作的代码。

    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧