有難度的問題: 不改變Control的Enable,如何避免Control產生點擊事件?
請注意: 避免Control產生點擊事件不是指我們添加的點擊事件不執行﹐而是根本不讓Control得到鼠標點擊的消息,從而避免它發生任何變化.
類似C 中接獲和消毀消息.
請高手們相助~~~
jimh(jimmy) 于 2005-6-13 16:34:32在form_load事件加上Application.AddMessageFilter(IMessageFilter)就可以了,
参数是一个实现了IMessageFilter的类的实例。
public class ControlMouseDown: IMessageFilter
{
public intptr ControlHandler;
public bool PreFilterMessage(ref Message m) //实现接口方法
{
if (m.Msg == WM_MouseDown && m.HWnd) return true; //过滤
return false;
}
}
fengfangfang 于 2005-6-13 16:34:47在鼠标或key的事件中,移去焦点
jimh(jimmy) 于 2005-6-13 16:35:46在form_load事件加上Application.AddMessageFilter(IMessageFilter)就可以了,
参数是一个实现了IMessageFilter的类的实例。
public class ControlMouseDown: IMessageFilter
{
public intptr ControlHandler;
public bool PreFilterMessage(ref Message m) //实现接口方法
{
if (m.Msg == WM_MouseDown && m.HWnd == ControlHandler) return true; //过滤
return false;
}
}
stonegoldaustin(特醇中南海) 于 2005-6-13 16:46:52以BUTTON为例,先建立一个集成BUTTON的类
using System;
using System.Windows.Forms;
namespace WndProc
{
public class ClsButton : System.Windows.Forms.Button
{
private const int WM_LBUTTONDOWN = 0x201;
public ClsButton()
{
}
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case WM_LBUTTONDOWN:
MessageBox.Show("Not Avalible");
break;
default:
base.WndProc(ref m);
break;
}
}
}
}
然后再FORM中加入该类,左键已经被屏蔽了(可通过添加CLICK事件察看)
private WndProc.ClsButton Command;
private void InitializeComponent()
{
this.Command = new WndProc.ClsButton();
this.SuspendLayout();
this.Command.Location = new System.Drawing.Point(0, 0);
this.Command.Name = "Command";
this.Command.Size = new System.Drawing.Size(100, 100);
this.Command.TabIndex = 0;
this.Command.Click = new System.EventHandler(this.Command_Click);
this.Controls.Add(this.Command);
}
private void Command_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Click");
}
sqfeiyu(流星雨) 于 2005-6-13 16:52:54沒話可說, 服了!!!
明天早上結貼.
mathsword(梦在流浪) 于 2005-6-13 17:36:48mark
xamaizi(ecogiser) 于 2005-07-01 17:38:00this.button1.Click = new System.EventHandler(this.button1_Click);
进入问吧