我在 C# 和 VB.net 中有这个奇怪的滚动/背景错误。当我创建一个面板并使用 Autoscroll 时,滚动过程中背景不会更新。最后,它看起来真的很奇怪(视频:https : //youtu.be/0vaO-zmWFmk)我用 TabControl 尝试了同样的方法,并且背景像它应该的那样滚动。我尝试了外部滚动条,也发生了同样的情况。我也试过 VB.net。我认为这是来自 Visual Studios 的错误,如果有人能帮助我,我将不胜感激,谢谢,LG!
如果您希望图像随滚动条一起滚动,您可以通过简单地扩展Panel
和覆盖OnPaintBackground
.
请记住,如果您这样做,您必须进行控制DoubleBuffered
(这在下面的代码中为您完成)。
在这个例子中,我添加了一个“平铺”选项。所以你可以使用一个大图像,或者使用一个无缝平铺并平铺它(通过BackgroundImageLayout
属性设置。所有其他选项将绘制相同)。
using System.Drawing;
using System.Windows.Forms;
public class PanelEx : Panel
{
public PanelEx()
{
DoubleBuffered = true;
}
protected override void OnPaintBackground(PaintEventArgs e)
{
if (BackgroundImage != null)
{
if (ImageLayout.Tile == BackgroundImageLayout)
{
for (int x = 0; x <= DisplayRectangle.Width;
x += BackgroundImage.Width)
{
for (int y = 0; y <= DisplayRectangle.Height;
y += BackgroundImage.Height)
{
e.Graphics.DrawImage(BackgroundImage,
new Point(x - HorizontalScroll.Value,
y - VerticalScroll.Value));
}
}
}
else
{
e.Graphics.DrawImage(BackgroundImage,
new Point(-HorizontalScroll.Value, -VerticalScroll.Value));
}
}
else
{
base.OnPaintBackground(e);
}
}
}
如何使用这个
Add
then Class
。为班级命名PanelEx.cs
System.Windows.Forms.Panel();
为PanelEx();
Public Class PanelEx
Inherits Panel
Public Sub New()
DoubleBuffered = True
End Sub
Protected Overrides Sub OnPaintBackground(e As PaintEventArgs)
If Not BackgroundImage Is Nothing Then
If BackgroundImageLayout = ImageLayout.Tile Then
Dim x As Integer, y As Integer
While x <= DisplayRectangle.Width
y = 0
While y <= DisplayRectangle.Height
e.Graphics.DrawImage(
BackgroundImage,
New Point(x - HorizontalScroll.Value,
y - VerticalScroll.Value))
y += BackgroundImage.Height
End While
x += BackgroundImage.Width
End While
Else
e.Graphics.DrawImage(BackgroundImage,
New Point(-HorizontalScroll.Value,
-VerticalScroll.Value))
End If
Else
MyBase.OnPaintBackground(e)
End If
End Sub
End Class
如何使用这个
Add
then Class
。为班级命名PanelEx.vb
Panel
为PanelEx
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句