![](/img/trans.png)
[英]Can't use websocket with SignalR when hosting service on azure windows VM (C#/.NET5.0)
[英]In C#/NET5.0 Windows Forms - How can I get a panel to NOT clear itself with the BackColor before drawing in the paint event?
在我的应用程序中更新面板的图形内容时,我试图消除闪烁效果。 简化后,Mainform.cs 看起来像这样:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsAppRefreshTest
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
var g = panel1.CreateGraphics();
g.FillRectangle(Brushes.Red, 20, 20, panel1.Width - 40, panel1.Height - 40);
if(panel1.Width>100 && panel1.Height>100) g.FillRectangle(Brushes.Blue, 40, 40, panel1.Width - 80, panel1.Height - 80);
}
private void panel1_SizeChanged(object sender, EventArgs e)
{
panel1.Refresh();
}
}
}
如果我在 panel1_Paint 的第一行设置断点,则在调用事件时面板将被清除为 BackColor。 这不是我想要的 - 我希望面板仅由 panel1_Paint 方法绘制,因为我每次都使用在我的实际应用程序中准备好的 bitmap 绘制整个区域。
我如何获得 Windows Forms 在代码可以绘制之前不使用 BackColor 清除面板?
添加一个答案,因为我对这个问题的编辑在一个小时前被删除了:
使用 Panel,我能够使用 Mark 的建议解决这个问题,为 Panel 创建一个子类,如下所示:
public class NoBackgroundPanel : Panel
{
public NoBackgroundPanel() { }
protected override void OnPaintBackground(PaintEventArgs e) { }
}
并在 MainForm.Designer.cs 中使用它代替 System.Windows.Forms.Panel - InitializeComponent(): this.panel1 = new NoBackgroundPanel();
这对我来说是理想的解决方案,因为我在实际应用程序中手动更新用作双缓冲区的 Bitmap,然后在必须重绘时将其绘制到面板的整个区域。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.