本人自制了个控件,基于微软原来的控件制作的,但是这个控件的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);
}
}
}
}