vb.net 如何在 PictureBox 控件上显示滚动条?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4676745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I show scrollbars on a PictureBox control?
提问by Voldemort
Sometimes, I have a picturebox lets say 100x100. But the image it will display is actually 100x400.
有时,我有一个图片框可以说是 100x100。但它将显示的图像实际上是 100x400。
I don't want to increase the size of the picturebox itself. Instead, I would like to create a vertical scrollbar (or horizontal if needed).
我不想增加图片框本身的大小。相反,我想创建一个垂直滚动条(或水平滚动条,如果需要)。
I could not find a scrollbar in the toolbox, so I guess I have to code it. But, how? And I still wonder if I didn't make a mistake and didn't see the scrollbar in the toolbox. My apologies then :(
我在工具箱中找不到滚动条,所以我想我必须对它进行编码。但是,怎么样?而且我还怀疑是不是我没有搞错,没有看到工具箱里的滚动条。那我很抱歉:(
回答by Cody Gray
I suppose you couldadd separate scrollbar controls and sync their Scroll
events up with the offset at which the picture in the PictureBox
is drawn, but that sounds like actual work. There's a better way.
我想您可以添加单独的滚动条控件并将它们的Scroll
事件与PictureBox
绘制图片的偏移量同步,但这听起来像是实际工作。有更好的方法。
Add a
Panel
control to your form, and set itsAutoScroll
propertyto "True".This will cause the control to automatically show scrollbars when it contains content that lies outside of its currently visible bounds. The .NET Framework will take care of everything for you under the covers, without you having to write a single line of code.Drag and drop your
PictureBox
control inside of thePanel
control that you just added.ThePanel
control will then detect that one of its child controls is larger than its visible area and show scrollbars, thanks to theAutoScroll
property. When the user moves the scrollbars, the portion of the image in yourPictureBox
that is visible will be automatically adjusted. Magic.
Panel
向您的表单添加一个控件,并将其AutoScroll
属性设置为“True”。这将导致控件在包含位于其当前可见边界之外的内容时自动显示滚动条。.NET Framework 将在幕后为您处理所有事情,而您无需编写任何代码。将
PictureBox
控件拖放Panel
到刚刚添加的控件内。Panel
由于该AutoScroll
属性,该控件将检测到其子控件之一大于其可见区域并显示滚动条。当用户移动滚动条时,图像中PictureBox
可见的部分将自动调整。魔法。
(The reason you have to use a Panel
control as a container is because PictureBox
does not inherit directly from the ScrollableControl
base class, which is what provides the AutoScroll
property.)
(您必须将Panel
控件用作容器的原因是因为PictureBox
它不直接从ScrollableControl
基类继承,而基类正是提供AutoScroll
属性的。)
回答by Gundark
I tried this and it worked well. But I noted that if the picturebox is docked in the panel, the picturebox is automatically set to the size of the parent panel, and can't be set larger (at least not in any way I could find). This defeats the purpose of the technique. So -- put the picturebox on the panel, but don't dock it, and it will work perfectly.
我试过了,效果很好。但是我注意到如果图片框停靠在面板中,图片框会自动设置为父面板的大小,并且不能设置得更大(至少我找不到任何方式)。这违背了该技术的目的。所以 - 将图片框放在面板上,但不要停靠它,它会完美地工作。
回答by xpda
There are no automatic scroll bars on a picture box, but you can add the VScrollBar (and HScrollBar) control to the form and handle the image scrolling manually by redrawing it at a different offset each time the Scroll event is fired.
图片框上没有自动滚动条,但您可以将 VScrollBar(和 HScrollBar)控件添加到窗体中,并通过在每次触发 Scroll 事件时以不同的偏移量重新绘制它来手动处理图像滚动。