本人自制了个控件,基于微软原来的控件制作的,但是这个控件的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);
}
}
}
}
你的代码有绘图的代码么?从你给出的代码看,并没有什么需要耗时操作的代码。
你没有讲清楚重写CheckedListBox的目的是什么,要实现什么功能
卡的问题出在WndProc方法
Form1.Designer.cs
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.myCheckListBox1 = new WindowsFormsApp45.MyCheckListBox();
this.SuspendLayout();
//
// checkedListBox1
//
this.checkedListBox1.CheckOnClick = true;
this.checkedListBox1.FormattingEnabled = true;
this.checkedListBox1.Location = new System.Drawing.Point(288, 51);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(209, 180);
this.checkedListBox1.TabIndex = 1;
//
// myCheckListBox1
//
this.myCheckListBox1.CheckOnClick = true;
this.myCheckListBox1.FormattingEnabled = true;
this.myCheckListBox1.Location = new System.Drawing.Point(34, 51);
this.myCheckListBox1.Name = "myCheckListBox1";
this.myCheckListBox1.Size = new System.Drawing.Size(213, 180);
this.myCheckListBox1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(559, 267);
this.Controls.Add(this.checkedListBox1);
this.Controls.Add(this.myCheckListBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private MyCheckListBox myCheckListBox1;
private System.Windows.Forms.CheckedListBox checkedListBox1;
}
Form1.cs
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.myCheckListBox1.Items.AddRange(new string[3] { "aa", "bb", "cc" });
this.checkedListBox1.Items.AddRange(new string[3] { "aa", "bb", "cc" });
}
MyCheckListBox.cs
internal class MyCheckListBox : CheckedListBox
{
public delegate void ItemBoxCheckedEventHanlder(object sender, 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(CheckListBoxItemCheckBoxEventArgs boxItemCheckBoxEventArgs)
{
Console.WriteLine(DateTime.Now);
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)
{
CheckListBoxItemCheckBoxEventArgs model = new 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);
}
}
}
class CheckListBoxItemCheckBoxEventArgs
{
public CheckListBoxItemCheckBoxEventArgs(MouseButtons mb, int x, int y, int index)
{
this.CheckIndex = index;
}
public int CheckIndex { get; set; }
}