如果将窗体或容器控件(如Panel控件)的AutoScroll属性设置为True时,那么当窗体或Panel容不下其中的子控件时就会出现 滚动条,通过移动滚动条可以上下显示出窗体或Panel中的全部内容。但是默认情况下滚动条的移动只能通过鼠标直接拖动滚动条来实现,而不能通过鼠标的滚 轮来实现上下移动,因此需要手动添加代码来实现这个功能。
滚动鼠标的滚轮,触发的是窗体或控件上的 MouseWheel 事件。但是在VS.net2005中,窗体和控件的事件列表中却不包含 MouseWheel 事件,因此关于这个事件的全部代码必须手动输入。这里以添加一个Panel的 MouseWheel 事件为例。首先在窗体的构造函数或InitializeComponent函数中,添加对MouseWheel 事件的订阅:
<span class="kwd">this</span><span class="pun">.</span><span class="typ">Panel</span><span class="pun">.</span><span class="typ">MouseWheel</span><span class="pun">+=</span><span class="kwd">new</span><span class="pln"> </span><span class="typ">System</span><span class="pun">.</span><span class="typ">Windows</span><span class="pun">.</span><span class="typ">Forms</span><span class="pun">.</span><span class="typ">MouseEventHandler</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="typ">Panel_MouseWheel</span><span class="pun">);</span>
这样只是使Panel能接收鼠标的滚动事件,但还不能实现滚动条的上下移动,因此还需要添加以下代码:
<span class="kwd">private</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> </span><span class="typ">Panel_MouseWheel</span><span class="pun">(</span><span class="kwd">object</span><span class="pln"> sender</span><span class="pun">,</span><span class="pln"> </span><span class="typ">MouseEventArgs</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"></p><p></span><span class="pun">{</span><span class="pln"></p><p> </span><span class="typ">Panel</span><span class="pun">.</span><span class="typ">VerticalScroll</span><span class="pun">.</span><span class="typ">Value</span><span class="pun">+=</span><span class="lit">10</span><span class="pun">;</span><span class="pln"></p><p> </span><span class="typ">Panel</span><span class="pun">.</span><span class="typ">Refresh</span><span class="pun">();</span><span class="pln"></p><p> </span><span class="typ">Panel</span><span class="pun">.</span><span class="typ">Invalidate</span><span class="pun">();</span><span class="pln"></p><p> </span><span class="typ">Panel</span><span class="pun">.</span><span class="typ">Update</span><span class="pun">();</span><span class="pln"></p><p></span><span class="pun">}</span>
如果添加了以上代码,就认为万事大吉了,那就大错特错了。运行以上的程序,可以发现,Panel控件并不能对鼠标滚轮事件做出反应,原因是 Panel控件默认不能获得焦点,因此它就不能捕获鼠标,也就不能捕获鼠标的滚轮事件。解决方法是让Panel控件先获得焦点,可以通过Panel的 MouseClick事件或MouseEnter事件来执行this.Panel.Focus()以达到目的。这样当鼠标点击了Panel控件或移动到 Panel控件上时它就获得了焦点。因此还需要添加以下代码:
<span class="pln"> </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> </span><span class="typ">Panel_MouseClick</span><span class="pun">(</span><span class="kwd">object</span><span class="pln"> sender</span><span class="pun">,</span><span class="pln"> </span><span class="typ">MouseEventArgs</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"></p><p> </span><span class="pun">{</span><span class="pln"></p><p> </span><span class="kwd">this</span><span class="pun">.</span><span class="typ">Panel</span><span class="pun">.</span><span class="typ">Focus</span><span class="pun">();</span><span class="pln"></p><p> </span><span class="pun">}</span>
这样再执行程序,就可以达到通过鼠标滚动来移动滚动条的目的了。
除此之外还要说的一点是,触发鼠标的滚动事件后,处理事件的函数参数 MouseEventArgs e 中有个Delta属性,默认情况下向上滚动e.Delta=120,向下滚动e.Delta=-120。
以上的程序还不是特别完美,因为当Panel控件较大而没有显示滚动条时,或滚动条已在最上方而滚轮又是向上滚动,或滚动条已在最下方而滚轮又 是向下滚动时,同样会执行Panel.Refresh();Panel.Invalidate();Panel.Update();等窗体重绘代码,占用 较多资源。因此可以在执行这些代码前先对Panel的当前状况做判断。完整的程序代码如下:
代码
<span class="kwd">public</span><span class="pln"> </span><span class="kwd">partial</span><span class="pln"> </span><span class="kwd">class</span><span class="pln"> </span><span class="typ">PlotInfoForm</span><span class="pun">.</span><span class="pln"> </span><span class="typ">Form</span><span class="pln"></p><p></span><span class="pun">{</span><span class="pln"></p><p> </span><span class="kwd">int</span><span class="pln"> lastRightPanelVerticalScrollValue</span><span class="pun">=-</span><span class="lit">1</span><span class="pun">;</span><span class="com">//为鼠标滚动事件提供一个静态变量,用来存储上次滚动后的VerticalScroll.Value</span><span class="pln"></p><p> </span><span class="kwd">public</span><span class="pln"> </span><span class="typ">PlotInfoForm</span><span class="pun">()</span><span class="pln"></p><p> </span><span class="pun">{</span><span class="pln"></p><p> </span><span class="typ">InitializeComponent</span><span class="pun">();</span><span class="pln"></p><p> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">rightPanel</span><span class="pun">.</span><span class="typ">MouseWheel</span><span class="pun">+=</span><span class="pln">newSystem</span><span class="pun">.</span><span class="typ">Windows</span><span class="pun">.</span><span class="typ">Forms</span><span class="pun">.</span><span class="typ">MouseEventHandler</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">rightPanel_MouseWheel</span><span class="pun">);</span><span class="pln"></p><p> </span><span class="pun">}</span><span class="pln"></p><p> </span><span class="com">//当鼠标在右侧面板上单击时,激活其滚动事件</span><span class="pln"></p><p> </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> rightPanel_MouseClick</span><span class="pun">(</span><span class="kwd">object</span><span class="pln"> sender</span><span class="pun">,</span><span class="pln"> </span><span class="typ">MouseEventArgs</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"></p><p> </span><span class="pun">{</span><span class="pln"></p><p> </span><span class="kwd">this</span><span class="pun">.</span><span class="pln">rightPanel</span><span class="pun">.</span><span class="typ">Focus</span><span class="pun">();</span><span class="pln"></p><p> </span><span class="pun">}</span><span class="pln"></p><p> </span><span class="kwd">private</span><span class="pln"> </span><span class="kwd">void</span><span class="pln"> rightPanel_MouseWheel</span><span class="pun">(</span><span class="kwd">object</span><span class="pln"> sender</span><span class="pun">,</span><span class="pln"> </span><span class="typ">MouseEventArgs</span><span class="pln"> e</span><span class="pun">)</span><span class="pln"></p><p> </span><span class="pun">{</span><span class="pln"></p><p> </span><span class="kwd">if</span><span class="pun">(!(</span><span class="pln">rightPanel</span><span class="pun">.</span><span class="typ">VerticalScroll</span><span class="pun">.</span><span class="typ">Visible</span><span class="pun">==</span><span class="kwd">false</span><span class="pun">||(</span><span class="pln">rightPanel</span><span class="pun">.</span><span class="typ">VerticalScroll</span><span class="pun">.</span><span class="typ">Value</span><span class="pun">==</span><span class="lit">0</span><span class="pun">&&</span><span class="pln">e</span><span class="pun">.</span><span class="typ">Delta</span><span class="pun">></span><span class="lit">0</span><span class="pun">)||(</span><span class="pln">rightPanel</span><span class="pun">.</span><span class="typ">VerticalScroll</span><span class="pun">.</span><span class="typ">Value</span><span class="pun">==</span><span class="pln">lastRightPanelVerticalScrollValue</span><span class="pun">&&</span><span class="pln">e</span><span class="pun">.</span><span class="typ">Delta</span><span class="pun"><</span><span class="lit">0</span><span class="pun">)))</span><span class="pln"></p><p> </span><span class="pun">{</span><span class="pln"></p><p> rightPanel</span><span class="pun">.</span><span class="typ">VerticalScroll</span><span class="pun">.</span><span class="typ">Value</span><span class="pun">+=</span><span class="lit">10</span><span class="pun">;</span><span class="pln"></p><p> lastRightPanelVerticalScrollValue</span><span class="pun">=</span><span class="pln">rightPanel</span><span class="pun">.</span><span class="typ">VerticalScroll</span><span class="pun">.</span><span class="typ">Value</span><span class="pun">;</span><span class="pln"></p><p> rightPanel</span><span class="pun">.</span><span class="typ">Refresh</span><span class="pun">();</span><span class="pln"></p><p> rightPanel</span><span class="pun">.</span><span class="typ">Invalidate</span><span class="pun">();</span><span class="pln"></p><p> rightPanel</span><span class="pun">.</span><span class="typ">Update</span><span class="pun">();</span><span class="pln"></p><p> </span><span class="pun">}</span><span class="pln"></p><p> </span><span class="pun">}</span><span class="pln"></p><p></span><span class="pun">}</span>
来源URL:http://outofmemory.cn/code-snippet/3152/C-panel-mousewheel-shubiao-gunlun-event-trigger