簡體   English   中英

c#如何通過vScrollbar移動圖片框?

[英]c# How can I move a picturebox by a vScrollbar?

我在c#中有一個pictureBox ,必須通過vScrollBar來移動它。

應該是這樣的:(偽代碼!)

        if (scrollbar.ScrollUp)
        {
            int i = 0;
            i += +1 per scroll
            pictureBox.Location = new Point(0, i);
        }

        if (scrollbar.ScrollDown)
        {
            int k = 0;
            k += -1 per scroll
            pictureBox.Location = new Point(0, k);
        }

在此處輸入圖片說明

我希望有人能理解我的問題。 謝謝

MSDN ScrollBar.Scroll事件的示例實際上顯示了如何滾動PictureBox

來自MSDN的代碼:

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

    int xWidth = pictureBox1.Width;
    int yHeight = pictureBox1.Height;

    int x;
    int y;

    if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
    {
        x = e.NewValue;
        y = vScrollBar1.Value;
    }
    else //e.ScrollOrientation == ScrollOrientation.VerticalScroll
    {
        y = e.NewValue;
        x = hScrollBar1.Value;
    }

    g.DrawImage(pictureBox1.Image,
      new Rectangle(0, 0, xWidth, yHeight),  //where to draw the image
      new Rectangle(x, y, xWidth, yHeight),  //the portion of the image to draw
      GraphicsUnit.Pixel);

    pictureBox1.Update();
}

其中HandleScrollScrollBarScroll事件的事件處理程序。

scrollBar1.Scroll += HandleScroll;

這是假設您嘗試滾動PictureBox 如果您真的只想移動它,可以嘗試以下操作:

private void HandleScroll(Object sender, ScrollEventArgs e)
{   
    var diff = scrollBar1.Value - e.NewValue;

    if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
    {
        pictureBox1.Location = new Point(diff, pictureBox1.Location.Y);
    }
    else //e.ScrollOrientation == ScrollOrientation.VerticalScroll
    {
        pictureBox1.Location = new Point(pictureBox1.Location.X, diff);
    }
}

警告,此代碼尚未經過測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM