2011-07-06 160 views
0

我已经在我的form.I实现了一个picturebox我甚至实现了滚动条,使图像适合it.so现在的问题是,当我尝试向下滚动按钮,它向下滚动,并立即当我离开鼠标按钮向上滚动..here是implemnted请给一些建议的代码..滚动问题!

 public void DisplayScrollBars() 
    { 
     // If the image is wider than the PictureBox, show the HScrollBar. 
     if (pictureBox1.Width > pictureBox1.Image.Width - this.vScrollBar1.Width) 
     { 
      hScrollBar1.Visible = false; 
     } 
     else 
     { 
      hScrollBar1.Visible = true; 
     } 

     // If the image is taller than the PictureBox, show the VScrollBar. 
     if (pictureBox1.Height > 
      pictureBox1.Image.Height - this.hScrollBar1.Height) 
     { 
      vScrollBar1.Visible = false; 
     } 
     else 
     { 
      vScrollBar1.Visible = true; 
     } 
    } 

    private void HandleScroll(Object sender, ScrollEventArgs se) 
    { 
     /* Create a graphics object and draw a portion 
      of the image in the PictureBox. */ 
     Graphics g = pictureBox1.CreateGraphics(); 

     g.DrawImage(pictureBox1.Image, 
      new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width, 
      pictureBox1.Bottom - hScrollBar1.Height), 
      new Rectangle(hScrollBar1.Value, vScrollBar1.Value, 
      pictureBox1.Right - vScrollBar1.Width, 
      pictureBox1.Bottom - hScrollBar1.Height), 
      GraphicsUnit.Pixel); 


     pictureBox1.Update(); 
    } 


    public void SetScrollBarValues() 
    { 
     // Set the Maximum, Minimum, LargeChange and SmallChange properties. 
     this.vScrollBar1.Minimum = 0; 
     this.hScrollBar1.Minimum = 0; 


     // If the offset does not make the Maximum less than zero, set its value. 
     if ((this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width) > 0) 
     { 
      this.hScrollBar1.Maximum = 
       this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width; 
     } 
     // If the VScrollBar is visible, adjust the Maximum of the 
     // HSCrollBar to account for the width of the VScrollBar. 
     if (this.vScrollBar1.Visible) 
     { 
      this.hScrollBar1.Maximum += this.vScrollBar1.Width; 
     } 
     this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum/10; 
     this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum/20; 


     // Adjust the Maximum value to make the raw Maximum value 
     // attainable by user interaction. 
     this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange; 

     // If the offset does not make the Maximum less than zero, set its value.  
     if ((this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height) > 0) 
     { 
      this.vScrollBar1.Maximum = 
       this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height; 
     } 


     // If the HScrollBar is visible, adjust the Maximum of the 
     // VSCrollBar to account for the width of the HScrollBar. 
     if (this.hScrollBar1.Visible) 
     { 
      this.vScrollBar1.Maximum += this.hScrollBar1.Height; 
     } 
     this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum/10; 
     this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum/20; 

     // Adjust the Maximum value to make the raw Maximum value 
     // attainable by user interaction. 
     this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange; 
    } 

    private void pictureBox1_Resize(object sender, EventArgs e) 
    { 
     // If the PictureBox has an image, see if it needs 
     // scrollbars and refresh the image. 
     if (pictureBox1.Image != null) 
     { 
      this.DisplayScrollBars(); 
      this.SetScrollBarValues(); 
      this.Refresh(); 
     } 
    } 
+2

你不能只把你的'pictureBox'放在'panel'中并设置'panel.AutoScroll = true'吗? – Bolu

+0

雅每一个说,但加载图片时,它不能够滚动,它只显示图像的一部分,所以,我实现了滚动条。 – raghu

+0

您需要将'pictureBox.SizeMode'更改为'AutoSize'。检查我的答案.. – Bolu

回答

0

正如我上面的评论,这样做的正确的方法是把你的panelpictureBox并设置panel.AutoScroll=true。您还需要设置pictureBox.SizeMode=AutoSize,以便它的大小等于其包含的图像的大小。检查:PictureBoxSizeMode Enumeration

+0

它不工作,这种方式!我试了很多次,甚至现在!!问题是图片无法滚动。只有部分图像加载(即取决于图片框的大小)。 – raghu

+0

@raghu,我怕你做错了什么,因为它对我(和其他人)有用。你可以从一个新项目开始,并且1.放一个“面板”; 2.设置'panel.AutoScroll = true'; 3.在'panel'内放置一个'pictureBox'; 4.设置'pictureBox.SizeMode = AutoSize'; 5.运行程序 – Bolu