2011-07-06 210 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