The PictureBox control provided by. Net does not have a scroll bar when the image is larger than the display area. However, this function can be easily implemented through Panel. We need to use a Panel to install PictureBox, set AutoScroll of the Panel to True, and set SizeMode of PictureBox to AutoSize. In this way, the size of PictureBox is determined by the image. When the size of PictureBox exceeds that of Panel, a scroll bar appears (from Panel ). But it is worth noting that do not Dock the PictureBox control on the Panel. Otherwise, no scroll bar will appear. You just need to set the Location of PictureBox to 0 or 0. The width and height of the Dock on the parent container are determined by the parent container. This is contrary to the AutoSize we want. In fact, some properties of the Microsoft control are inconsistent, but it is not reflected in the designer but in the runtime, which brings us a lot of confusion and needs to be summarized slowly.
Now you can view the large image and the scroll bar will appear, but you will find that the scroll wheel is useless. It seems that we need to add some code to support the scroll wheel. We can use the MouseWheel event on Form to handle this problem. We only need to make a regional judgment. If the mouse is in the Panel area, we can change the vertical scroll value of the Panel.
private void mainForm_MouseWheel(object sender, MouseEventArgs e){ // get screen point Point mousePoint = _mainForm.PointToScreen(e.Location); // whether in panel if (this.pnlImage.RectangleToScreen(this.pnlImage.ClientRectangle).Contains(mousePoint)) { this.pnlImage.AutoScrollPosition = new Point(this.pnlImage.HorizontalScroll.Value, pnlImage.VerticalScroll.Value - e.Delta); }}
E. Location is the position of the mouse in the form. It is calculated based on the value 0, 0 in the upper left corner of the window. The PointToScreen method can be used to convert the screen coordinates to 0 in the upper left corner of the screen. Similarly, the coordinates of the display area of the Panel are also converted to screen coordinates. Next, you can determine whether the current mouse is in the display area of the Panel.
But the scroll wheel can only change the vertical scroll bar, if you want to be more perfect, you can drag the mouse to change the horizontal and vertical scroll bars at the same time. You also need to do something on the MouseDown, MouseMove, and MouseUp of PictureBox. The general idea is to remember a coordinate when MouseDown. When MouseMove, compare the current coordinate with the recorded coordinate to know the direction of the mouse and change the position of the scroll bar.
private Point _startLocation;private void pbImage_MouseDown(object sender, MouseEventArgs e){ if (e.Button == MouseButtons.Left) { _startLocation = e.Location; Cursor = Cursors.SizeAll; }}private void pbImage_MouseMove(object sender, MouseEventArgs e){ if (e.Button == MouseButtons.Left) { int xOffset = _startLocation.X - e.X; int yOffset = _startLocation.Y - e.Y; this.pnlImage.AutoScrollPosition = new Point(this.pnlImage.HorizontalScroll.Value + xOffset, pnlImage.VerticalScroll.Value + yOffset); }}private void pbImage_MouseUp(object sender, MouseEventArgs e){ Cursor = Cursors.Default;}
If you have a better solution, please leave a message. 650) this. width = 650; "style =" border-bottom-style: none; border-left-style: none; border-top-style: none; border-right-style: none "class =" wlEmoticon-smile "alt =" Smile "src =" http://www.bkjia.com/uploads/allimg/131228/2005593a7-0.png "/>
This article from the "only text cut through time and space" blog, please be sure to keep this source http://arthurshayne.blog.51cto.com/3491820/1277392