请问大伙儿如和让pictruebox里的图片随着滚动条滚动而滚动图片。(picturebox放在panel里面的),谢谢各位啊!

解决方案 »

  1.   

    PictureBox没有这个功能,你需要自已定义一个这样的控件,可以参考下面的代码:public class ImageCtr : ScrollableControl
    {
    private Image m_Img; public Image Img
    {
    get { return m_Img; }
    set
    {
    if (value != this.m_Img)
    {
    m_Img = value;
    if (this.m_Img != null)
    {
    this.AutoScrollMinSize = this.m_Img.Size;
    }
    else
    {
    this.AutoScrollMinSize = Size.Empty;
    }
    this.Invalidate();
    }
    }
    }
    public ImageCtr()
    {
    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
    base.OnPaint(e);
    if (this.m_Img != null)
    {
    e.Graphics.DrawImage(this.m_Img, this.AutoScrollPosition.X, this.AutoScrollPosition.Y, this.m_Img.Width, this.m_Img.Height);
    }
    }
    protected override void OnPrint(PaintEventArgs e)
    {
    if (this.m_Img != null)
    {
    e.Graphics.DrawImage(this.m_Img, 0, 0, this.m_Img.Width, this.m_Img.Height);
    }
    }
    }
      

  2.   

    panel的autoscroll属性设置成true
    picturebox的location设置成0,0
                size设置成1,1
                sizemode设置成autosize
      

  3.   

    if (是Windows应用程序)
    {
        我的意思是在显示一个大的图片需要使用滚动条的时候可以不必使用PictureBox,因为这
        个控件不能很好的支持滚动功能,可以按我上面的代码做一个显示图片的控件,这个控件
        是带自动滚动条的。试试便知。更没有必要放到一个Panel控件里来模拟这个效果。
    }
    else
    {
        算我没有说。
    }
      

  4.   

    hbxtlhx(平民百姓-自已动手,丰衣足食)
    但是你这个控件并不能达到实时滚动的效果啊
      

  5.   

    。OMG,原来这么简单~
    那弱弱的问下怎么通过代码来实现这个呢。。
      

  6.   

    是拖动时显示窗口内容这个功能吗?那只能是调用API了。不过最好不改,尊重客户原来的习惯。
      

  7.   

    可是为什么有的程序我没有将“拖动时显示窗口内容”选上它还是能拖动时显示呢,比如说Windows自带的画图程序。
      

  8.   

    自定义一个panel,重写WndProc方法public class MyPanel : System.Windows.Forms.Panel
    {
       protected override void WndProc(ref Message m)
    {
    System.Drawing.Point p = new Point();
    if ((m.Msg & 0x114) == 0x114)
    {
    int i = (m.WParam.ToInt32() >> 16);
    if ((m.WParam.ToInt32() & 0xffff) == 5)
    {
    if (m.Msg == 0x114)
    p.X = i;
    else
    p.Y = i;
    this.AutoScrollPosition = p;
    }
    }
    base.WndProc (ref m);
    }}在Form中拖一个panel控件,再拖一个picturebox控件放入panel中
    属性如下设置
    panel的autoscroll属性设置成true
    picturebox的location设置成0,0
                size设置成1,1
                sizemode设置成autosize将代码中的panel的定义和实例化代码改掉private System.Windows.Forms.Panel panel1;改为
    private MyPanel panel1;this.panel1 = new System.Windows.Forms.Panel();改为
    this.panel1 = new MyPanel();
      

  9.   

    如果要做成这样的,那么就要使用Scrollbar控件了,然后在滚动条的事件Scroll里编写代码。但是对于标准的Windows来说,默认的应该就是按用户的设置来的。
      

  10.   

    如果要自己添加ScrollBar控件,那么处理的问题可能很多,比如可能要滚动窗口,可能使用ScrollWindow这个API,要计算滚动的位置大小等等一系统问题。但对于默认的滚动条则简单的多了。
      

  11.   

    非常感谢 hbxtlhx 祝你工作愉快!