- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 C#/.Net 开发一个简单的矢量绘图应用程序。绘图是在面板中完成的,但我并没有为所有绘图使用 OnPaint() 事件 - 事实上 OnPaint() 甚至只是调用另一个方法,该方法实际绘制文档中的所有内容。
我尝试添加双缓冲,但是当我将 DoubleBuffered 设置为 true 时,闪烁问题更加严重。为什么是这样?如果我想对控件进行双重缓冲,我是否必须使用提供的 Graphics 对象在 OnPaint() 事件中完成所有绘制,而不是使用 Panel.CreateGraphics() 然后绘制到它?
编辑:这是我正在使用的基本代码。
private void doc_Paint(object sender, PaintEventArgs e)
{
g = doc.CreateGraphics();
Render(ScaleFactor, Offset);
}
private void Render(float ScaleFactor, PointF offset)
{
foreach (Line X in Document.Lines) { DrawLine(X.PointA, X.PointB, X.Color, X.LineWidth); }
}
private void DrawLine(PointF A, PointF B, Color Color, float Width)
{
Pen p = new Pen(Color, Width);
PointF PA = new PointF(((A.X + Offset.X) * ScaleFactor), ((A.Y + Offset.Y) * ScaleFactor));
PointF PB = new PointF(((B.X + Offset.X) * ScaleFactor), ((B.Y + Offset.Y) * ScaleFactor));
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawLine(p, PA, PB);
}
总体思路是,ScaleFactor 和 Offset 这两个变量指的是 UI 中的缩放级别和平移级别。 g 是一个图形对象。
最佳答案
g = doc.CreateGraphics();
这是错误的。双缓冲只有在您绘制到缓冲区中时才能工作。 e.Graphics 引用的那个。修复:
g = e.Graphics;
请注意,默认情况下,Panel 没有启用双缓冲。你需要自己推导。将其粘贴到一个新类中:
using System;
using System.Windows.Forms;
class BufferedPanel : Panel {
public BufferedPanel() {
this.DoubleBuffered = true;
this.ResizeRedraw = true;
}
}
编译。将其从工具箱顶部放下。
关于c# - 不在 OnPaint() : why doesn't it work? 中绘制时的双缓冲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3113190/
我想知道在windows窗体程序中覆盖OnPaint时应该什么时候调用base.OnPaint? 我正在做的是: private void Form1_Paint(object sender, P
我有一个包含用户控件的 Windows 窗体。此用户控件具有以下代码: protected override void OnPaint(PaintEventArgs pe) { base.OnPa
我有一个包含用户控件的 Windows 窗体。此用户控件具有以下代码: protected override void OnPaint(PaintEventArgs pe) { base.OnPa
我正在尝试使用 FillPath 覆盖我自己的自定义控件中标签的 OnPaint 方法。 这是我的控件代码: public partial class GlassLabel : Label {
如果我重写 OnPaint 并在控件上绘制一个正方形,当我在设计器中预览它时,如何让 e.graphics.draw... 显示出来? 最佳答案 http://msdn.microsoft.com/e
我正在创建一个带有自定义边框的表单。我想使用 onPaint 重写来绘制自己的边框。不幸的是,当我只使用下面的代码时,我得到了图像的踪迹。 有人知道怎么解决吗?是的,我已将 DoubleBuffere
我在对话框中有一个图片控制框(CStatic)。当用户按下对话框中的按钮时,我需要 onPaint() 在其中绘制图像。问题是,图像是在对话框加载时绘制的。如何防止这种情况并仅在按下按钮时调用它。 我
我对 CFrameWnd 的 OnPaint 方法有问题,我似乎无法弄清楚发生了什么。 OnPaint 大约每 10 毫秒调用一次,这会导致计算机死机。检查 CPU 使用率,此应用占用 50%! 该应
我有一个 CWind 类的 child ,我在其中订阅 OnPaint 事件: BEGIN_MESSAGE_MAP(MyListBox, CWnd) ON_WM_PAINT() END_MES
我有一个 CWind 类的 child ,我在其中订阅 OnPaint 事件: BEGIN_MESSAGE_MAP(MyListBox, CWnd) ON_WM_PAINT() END_MES
我们有一个通常在 Windows XP 下运行的 .NET 2.0 软件(在 Windows“经典”模式下配置,没有附加功能)。现在,我在 Windows 7 上运行它的全部功能。我在 Windows
我正在尝试使命令模式适应具有撤消功能的简单绘图应用程序。我一直坚持使用 OnPaint 撤消操作事件。这是代码: [已解决] 帖子末尾的详细信息 interface ICommand { vo
大家好,我在编写自定义控件时遇到了问题。我的控件继承自 Windows.Forms.Control,我试图重写 OnPaint 方法。这个问题有点奇怪,因为只有当我在我的表单中包含一个控件时它才有效,
我有一个自定义绘画的用户控件。据我所知,构造函数正确设置样式。基本代码: public partial class LineChart2 : UserControl { public Line
我有以下自定义 Control: public class Line : Control { protected override void OnPaint(PaintEventArgs e)
我有一个 UserControl(WinForms,.net 2.0),我有这个: protected override void OnPaint(PaintEventArgs e) { ba
我正在尝试使用 winforms 和 gdi+ 实现一些动画,但我在控件中发现了很多撕裂的移动 Sprite 。 在托管 .net 中有没有办法让重写的 Control.OnPaint 方法等待监视器
在 WinForm 应用程序中,当订阅 OnPaint() 事件时,PaintEventArgs 提供一个 ClipRectangle 属性定义要绘制的区域。 当表单垂直或水平调整大小时,它会给出要绘
我有以下wx.Window: class SketchWindow(wx.Window): def __init__(self, parent): wx.Window.__init__(s
我有一个用 C++ 和 MFC 编写的基于对话框的应用程序。该对话框有一个 CStatic 图片控件。我在 PictureCtrl 的 OnPaint() 函数中绘制 BITMAP。相关代码如下: v
我是一名优秀的程序员,十分优秀!