自定义控件常继承于 forms.panel 或 forms.control类,因为此类控件默认是不可获得焦点的,即canfous属性为false,所以无法获得mousewheel鼠标滚轮消息,解决方法有两种
1> 通过createcontrol()一系列操作使控件能够接受焦点。(麻烦)
2> 创建一个能接受焦点的长宽为0的子控件, 如textbox。(简单)
方法2中注意:
2.1> 不能把textbox,visible=false 或 disable掉,否则它不能获得焦点。
2.2> 如果子控件充满整个控件,那么控件的mousemove,mouseenter等事件都不能被触发,无法让控件自动获取焦点。
接下来一步很重要,因为textbox小到看不见,为了让它获得焦点的比较可靠一个方法是:当控件本身和所有子控件mousemove事件中增加this.focus();
例程
using System;
using System.Collections.Generic;
usingSystem.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
/// <summary>
///一个支持mousewheel滚轮的panel继承控件
///
hankersyan@msn.com ///</summary>
class Class2 : Panel
{
TextBox_txtGetFocus = new TextBox();
PictureBox _pbox = newPictureBox();
public Class2()
{
// 允许滚动条
this.AutoScroll = true;
this.BackColor =System.Drawing.Color.White;
//
//为了让控件具有获取焦点的能力,增加一个长宽为0的子控件textbox
_txtGetFocus.BorderStyle =BorderStyle.None;
_txtGetFocus.Top = 0;
_txtGetFocus.Left = 0;
_txtGetFocus.Width = 0;
_txtGetFocus.Height = 0;
_txtGetFocus.Text = "
hankersyan@msn.com";
this.Controls.Add(_txtGetFocus);
//
//演示控件,调整其长宽让class2出现scrollbar
_pbox.SizeMode =PictureBoxSizeMode.Zoom;
_pbox.Load(@"c:\test2.bmp");
_pbox.Top = 0;
_pbox.Left = 0;
_pbox.Width = 200;
_pbox.Height =200;
this.Controls.Add(_pbox);
//
// 让控件“自动”获取焦点
this.MouseMove+= delegate { this.Focus(); };
foreach (Control ctrl inthis.Controls)
{
ctrl.MouseMove += delegate {this.Focus(); };
}
}
}
http://www.overtaken.org/Doc157.aspx
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemwindowsformsmouseeventargsclassdeltatopic.asp
如何在xwindow下使用滚轮鼠标 修改XF86Config配置文件(Redhat的文件在/etc/X11下),在关于鼠标的配置上面必须有ZAxisMapping的定义。 如果用户使用XFree 3.3.x,那么配置文件中要有类似内容: Section "Pointer" Protocol "IMPS/2" Buttons 3 ZAxisMapping 4 5 EndSection
如果是XFree 4.x,那么: Section "InputDevice" Identifier "Mouse0" Driver "mouse" Option "Protocol" "IMPS/2" Option "Device" "/dev/mouse" Option "ZAxisMapping" "4 5" EndSection |
如何设置xwindow下鼠标速度 使用xset m n命令,下面是速度列表: "Very Fast" xset m 7 10 & "Normal(Fast)" xset m 3 10 & "System Default" xset m default & "Glacial" xset m 0 10 & |