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.
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
After dragging the rectangle the size of the pictureBox increases and scroll bar appears
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 ? 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.
Can i make pictureBox as a parent for my Drawing?
答案 0 :(得分:0)
我担心这是不可能的。
在Winforms Scrollbars
中,只会将带有溢出其容器的内容重新置于视图中。如果他们“下溢”,即如果他们的Top
和/或Left
值为负值,则不显示ScrollBars
,即使他们这样做(因为额外的溢出)他们也不会带回来
最小滚动位置为zero
。
所以你必须确保不要把事情变成负面的!
您可以通过添加虚拟控件来强制ScrollBars
,但这样只会让您完全滚动PictureBox
向上或向左看不见,而不是回来!
如果你真的真的真的非常想要,你可以检测到这种情况并手动添加ScrollBar
控件并对其进行编码以将PictureBox
向下/向右移动..
既然你在问,这是一个可以玩的例子。请注意,使用coded ScrollBars有很多陷阱..
ScrollBar
出现的情况?在示例中,我通过不将ScrollBar添加到Panel但通过覆盖它来解决它;因此,当编码的ScrollBar显示时,它将被隐藏。 ScrollBar
可以让你对你拥有的控件做些事情;但是它不会允许你实际滚动到负面区域。我想你真正想要的是什么。但我认为这是不可能的! 所以,我建议您不要使用下面的代码,除非您只需将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();
}
}
总而言之,我真的相信避免这种情况会是最好的。