滚动时面板背景不刷新 (C#/VB.net) [英] Panel Background not refreshing during scrolling (C#/VB.net)
问题描述
我在 C# 和 VB.net 中有这个奇怪的滚动/背景错误.当我创建一个面板并使用 Autoscroll 时,滚动过程中背景不会更新.最后看起来真的很奇怪(视频:
VB.NET
公共类 PanelEx继承面板公共子新建()双缓冲 = 真结束子Protected Overrides Sub OnPaintBackground(e As PaintEventArgs)如果不是 BackgroundImage 什么都不是如果 BackgroundImageLayout = ImageLayout.Tile 那么将 x 调暗为整数,将 y 调为整数而 x <= DisplayRectangle.Widthy = 0当 y <= DisplayRectangle.Heighte.Graphics.DrawImage(背景图片,新点(x - HorizontalScroll.Value,y - VerticalScroll.Value))y += 背景图像.高度结束时间x += BackgroundImage.Width结束时间别的e.Graphics.DrawImage(BackgroundImage,新点(-HorizontalScroll.Value,-VerticalScroll.Value))万一别的MyBase.OnPaintBackground(e)万一结束子结束类
如何使用
- 右键单击您的项目,然后在菜单中选择
Add
,然后选择Class
.将类命名为PanelEx.vb
- 将上面的代码复制并粘贴到该类文件中.
- 在包含您要修改的面板的表单中,进入设计器文件(请看下图)
- 将
Panel
的所有实例更改为PanelEx
- 保存,重新构建并运行.
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!
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.
这篇关于滚动时面板背景不刷新 (C#/VB.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!