If you set the AutoScroll attribute of a form or container control (such as a Plael Control) to True, a scroll bar appears when the form or Panel cannot accommodate its child control, you can move the scroll bar to display all content in the form or Panel. However, by default, the scroll bar can only be moved by dragging the scroll bar with the mouse, but not by moving the scroll wheel with the mouse. Therefore, you need to manually add code to implement this function. Scroll the scroll wheel of the mouse to trigger the MouseWheel event on the form or control. However, in VS. net2005, the event list of the forms and controls does not contain the MouseWheel event. Therefore, all the code for this event must be manually entered. The following uses the MouseWheel event for adding a Panel as an example. First, add a subscription to the MouseWheel event in the form constructor or InitializeComponent function:
this.Panel.MouseWheel += new System.Windows.Forms.MouseEventHandler(this.Panel_MouseWheel);
This only enables the Panel to receive scroll events of the mouse, but cannot move the scroll bar up or down. Therefore, you need to add the following code:
private void Panel_MouseWheel(object sender, MouseEventArgs e)
{
Panel.VerticalScroll.Value += 10;
Panel.Refresh();
Panel.Invalidate();
Panel.Update();
}
If you have added the above Code, you will think everything is fine, and that's a big mistake. After running the above program, you can find that the Panel control cannot respond to the scroll wheel event because the Panel control cannot obtain the focus by default, so it cannot capture the mouse, the scroll wheel event cannot be captured. The solution is to get the Focus of the Panel control first. You can execute this. Panel. Focus () through the MouseClick event or MouseEnter event of the Panel to achieve the goal. In this way, when the mouse clicks the Panel control or moves to the Panel control, it gets the focus. Therefore, you need to add the following code:
private void Panel_MouseClick(object sender, MouseEventArgs e){this.Panel.Focus();}
In this way, you can move the scroll bar by scrolling the mouse. In addition, after a mouse scroll event is triggered, the function parameter MouseEventArgs e for processing the event has a Delta attribute. By default, the event is rolled up. delta = 120, scroll down e. delta =-120. The above program is not perfect, because when the Panel control is large and the scroll bar is not displayed, or the scroll bar is already at the top and the scroll wheel is rolling up, or when the scroll bar is already at the bottom and the scroll wheel is scroll down, the Panel is also executed. refresh (); Panel. invalidate (); Panel. update (); and other window weight painting codes, occupying a large amount of resources. Therefore, you can determine the current status of the Panel before executing the code. The complete program code is as follows:
Public partial class PlotInfoForm: Form
{
Int lastRightPanelVerticalScrollValue =-1; // provides a static variable for the mouse scroll event to store VerticalScroll. Value after the last scroll
Public PlotInfoForm ()
{
InitializeComponent ();
This. rightPanel. MouseWheel + = new System. Windows. Forms. MouseEventHandler (this. rightPanel_MouseWheel );
}
// When you click on the right panel, the scroll event is activated.
Private void rightPanel_MouseClick (object sender, MouseEventArgs e)
{
This. rightPanel. Focus ();
}
Private void rightPanel_MouseWheel (object sender, MouseEventArgs e)
{
If (! (RightPanel. verticalScroll. visible = false | (rightPanel. verticalScroll. value = 0 & e. delta> 0) | (rightPanel. verticalScroll. value = lastRightPanelVerticalScrollValue & e. delta <0 )))
{
RightPanel. VerticalScroll. Value + = 10;
LastRightPanelVerticalScrollValue = rightPanel. VerticalScroll. Value;
RightPanel. Refresh ();
RightPanel. Invalidate ();
RightPanel. Update ();
}
}
}