简体   繁体   English

如何滚动在Windows窗体C#中增加图片框大小?

[英]How to scroll on increasing picturebox size in windows form c#?

I have a PictureBox control in windows form with a movable rectangle drawing in it, my PictureBox size will increase according to the position of the rectangle, when the PictureBox size is greater than a particular size scrollbar will appear. 我在Windows窗体中有一个PictureBox控件,其中有一个可移动的矩形绘图,当PictureBox的尺寸大于特定尺寸时,我的PictureBox的尺寸会根据矩形的位置而增加,滚动条会出现。

public void PicBox_MouseDown(object sender, MouseEventArgs e)
{

       if (rect.Contains(new Point(e.X, e.Y)))
       {
           mMove = true;
       }

       oldX = e.X;
       oldY = e.Y;
}

 public void Picbox_MouseUp(object sender , MouseEventArgs e)
 {
      mMove = false;
 }

 public void Picbox_MouseMove(object sender,MouseEventArgs e)
 {
      if (mMove)
      {
            picBox.Cursor = Cursors.Cross;
            rect.X = rect.X + e.X - oldX;
            rect.Y = rect.Y + e.Y - oldY;
      }
      oldX = e.X;
      oldY = e.Y;
      picBox.Invalidate();
 }

public void Picbox_MousePaint(object sender, PaintEventArgs e)
{
        picBox.Invalidate();
        picBox.Size = new Size((rect.Width + rect.X) + 10, (rect.Height + rect.Y) + 10);
        Draw(e.Graphics);  
}

在此处输入图片说明

On first display of form with pictureBox 第一次显示带有pictureBox的表单

在此处输入图片说明在此处输入图片说明

After dragging the rectangle the size of the pictureBox increases and scroll bar appears 拖动矩形后,pictureBox的大小增加,并出现滚动条

everything works fine here but my problems is no matter how much the size of the pictureBox is the scroll bar works fine when i drag it down,but when the rectangle is dragged up and if the edges of the pictureBox meets the form the whole scroll bar is reset to normal, by looking at the pictures below you will be able to understand what i want to say 一切都在这里正常工作,但是我的问题是无论图片盒的大小是多少,滚动条在我向下拖动时都可以正常工作,但是当矩形被向上拖动时,如果图片盒的边缘与整个滚动条相交,重置为正常,通过查看下面的图片,您将能够理解我想要说的

在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

How to solve this, is there another way to increase scroll according to the width of pictureBox ? 如何解决这个问题,是否有另一种方法可以根据pictureBox的宽度增加滚动? i am new at this. 我是新来的。

the rectangle is acting as if the the X and Y co-ordinates are tracked with respect to winform but not pictureBox. 矩形的行为就像是相对于winform而不是pictureBox跟踪X和Y坐标一样。

Can i make pictureBox as a parent for my Drawing? 我可以将pictureBox作为我的绘图的父级吗?

I'm afraid this isn't possible. 恐怕这是不可能的。

In Winforms Scrollbars will only bring things back into view that have overflown their container. Winforms Scrollbars只会将已经溢出其容器的内容带回视图。 If they 'underflow', ie if their Top and/or Left values get negative no ScrollBars show and even if they do (because of additional overflow) they won't bring them back. 如果它们“下溢”,即如果它们的Top和/或LeftScrollBars负值,则不会显示ScrollBars ,即使它们确实出现了(由于额外的溢出),也不会将它们带回。

The minimum scroll position is zero . 最小滚动位置zero

So you will have to make sure not to move things into the negative! 因此,您必须确保不要将事情拖到负面!

You can enforce ScrollBars by adding dummy controls but that will only allow you to completely scroll the PictureBox up or left out of sight, not back! 您可以通过添加虚拟控件来强制使用ScrollBars ,但这只会使您完全向上或不可PictureBox滚动PictureBox ,而不能向后滚动!

If you really really really really really really want to you can detect the situation and manually add ScrollBar controls and code them to move the PictureBox back down/right.. 如果您确实真的很想要,您可以检测到这种情况,然后手动添加ScrollBar控件并对其进行编码,以将PictureBox上下左右移动。

Since you were asking, here is an example to play with. 既然您在问,这里就是一个例子。 Note though that working with coded ScrollBars has numerous pitfalls.. 请注意,尽管使用编码的ScrollBars有很多陷阱。

  • One of them would be how to handle the situation when the real ScrollBar shows up as well? 其中之一是当真正的ScrollBar出现时如何处理这种情况? In the example I solve it by not adding the ScrollBar to the Panel but by overlaying it; 在该示例中,我不通过将ScrollBar添加到Panel而是通过将其覆盖来解决它。 thus the real ScrollBar is hidden while the coded one is showing. 因此,真正的ScrollBar会在显示编码的代码时被隐藏。
  • Another problem are the numbers: How many pixels should the content be moved? 另一个问题是数字:内容应移动多少像素? I don't let the uses scroll anywhere but up and when that happens I move the PictureBox all the way back and the coded ScrollBar disappears.. 我不让用途滚动到任何地方,而是向上滚动,当发生这种情况时,我将PictureBox一直向后移动,编码的ScrollBar消失了。
  • As a last point of warning it should be noted, that a coded ScrollBar lets you do things to the controls you have; 最后要警告的一点是,编码的ScrollBar允许您对所拥有的控件执行操作。 but it won't allow you to actually scroll into the negative realm! 它实际上不允许您滚动到负面领域! which I guess is what you would actually like. 我想这就是您真正想要的。 But I think this is impossible! 但是我认为这是不可能的!

So, I suggest you don't use the code below, unless you are OK with simply bringing the PictureBox.Top back to zero! 因此,我建议您不要使用下面的代码,除非您可以简单地将PictureBox.Top返回零即可。

// a variable at class level:
VScrollBar vScroll = null;

// move into negative for testing:
pictureBox1.Top = -15;
// now check to see if we need a VScrollBar
scrollCheck();

void scrollCheck()
{
    if (pictureBox1.Top < 0)
    {
        if (vScroll == null )
        { vScroll = new VScrollBar(); vScroll.Parent = panel1.Parent;
        vScroll.Scroll += vScroll_Scroll; vScroll.BringToFront();
        }
        vScroll.Location = new Point(panel1.Right - vScroll.Width - 2, panel1.Top + 1);
        vScroll.Height = panel1.ClientSize.Height - 2;
        vScroll.Value = vScroll.Maximum;
        vScroll.Show();
        }
    else
    { vScroll.Hide(); }
}

void vScroll_Scroll(object sender, ScrollEventArgs e)
{
    int delta = e.NewValue - e.OldValue;
    if (delta < 0)
    {
        pictureBox1.Top = 0;
        scrollCheck();
    }
}

All in all I really believe that avoiding the situation will be best.. 总而言之,我真的相信避免这种情况将是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM