1。继承控件,屏蔽消息
private const int WM_GETTEXT = 0x000d;
private const int WM_COPY = 0x0301;
private const int WM_PASTE = 0x0302;
private const int WM_CONTEXTMENU = 0x007B;
private const int WM_RBUTTONDOWN = 0x0204;
class TextBoxCtrl : TextBox//继承控件屏蔽消息
{
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_RBUTTONDOWN || m.Msg == WM_GETTEXT || m.Msg == WM_COPY)
return;
base.WndProc(ref m);
}
}
2. 设置键盘和鼠标事件
private void TextBoxCtrl_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.V)
e.Handled = true;
}
private void TextBoxCtrl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
TextBoxCtrl.ContextMenu = new ContextMenu();
}
}
3. 用HOOK勾住textbox的消息,比较复杂 。