问题
I have this weird scrolling/background bug in C# and VB.net. When I create a panel and I use Autoscroll the background isnt updating during scrolling. In the end it looks really weird (video: https://youtu.be/0vaO-zmWFmk) I tried the same with a TabControl and the background scrolled like it should. I tried external scrollbars and the same happened. And I tried VB.net too. I think this is a bug from Visual Studios and I would appreciate if someone could help me Thanks, LG!
回答1:
If you want the image to scroll with the scrollbar, you can do this easily by simply extending the Panel
and overriding OnPaintBackground
.
Keep in mind, if you do this, you must make the control DoubleBuffered
(this is done for you in the code below).
In this example, I added in a "Tiled" option. So you could use one big image, or use a seamless tile and tile it (set via BackgroundImageLayout
property. All other options will paint the same).
C#
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);
}
}
}
How to use this
- Right-click on your project and in the menu select
Add
thenClass
. Name the classPanelEx.cs
- Copy and paste the code above in to that class file.
- In the form that has the panel you want to modify, go in to the designer file (Look at image below)
- Change all instances of
System.Windows.Forms.Panel();
toPanelEx();
- Save, do a full rebuild and run.
VB.NET
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
How to use this
- Right-click on your project and in the menu select
Add
thenClass
. Name the classPanelEx.vb
- Copy and paste the code above in to that class file.
- In the form that has the panel you want to modify, go in to the designer file (Look at image below)
- Change all instances of
Panel
toPanelEx
- Save, do a full rebuild and run.
来源:https://stackoverflow.com/questions/63439473/panel-background-not-refreshing-during-scrolling-c-vb-net