OnPaint 无法正确绘制
Visual Studio 2008 SP1 C# Windows 应用程序
我直接在主窗体上编写和绘图,但在重新绘制屏幕时遇到问题。程序启动时,屏幕绘制正确。 3-4 秒后又出现两条绘制消息(屏幕上没有动作或运动),并且屏幕是使用屏幕坐标(我认为)而不是客户端坐标绘制的。原始字符串不会被删除。
为了将问题简化为最简单的形式,我启动了一个新的 C# Windows 应用程序。除了在主窗体上绘制字符串之外,它什么也不做。 (参见下面的代码片段)如果启动程序,将出现该字符串,然后第二个字符串将出现在上方和左侧。如果重新启动程序并将表单移向屏幕的左上角,则两个字符串将几乎重合。这就是为什么我认为第二个绘制是使用屏幕坐标。
这是代码 - 提前感谢您提供的任何帮助。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Junk
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs eventArgs)
{
using (Font myFont = new System.Drawing.Font("Helvetica", 40, FontStyle.Italic))
{
eventArgs.Graphics.TranslateTransform(0, 0);
Point p;
eventArgs.Graphics.DrawString("Hello C#", myFont, System.Drawing.Brushes.Red, 200, 200);
} //myFont is automatically disposed here, even if an exception was thrown
}
}
}
Visual Studio 2008 SP1 C# Windows application
I am writing and drawing directly to the main form and am having a problem repainting the screen. On program startup, the screen paints correctly. Two more paint messages follow in 3-4 seconds (with no action or motion on the screen) and the screen is painted using screen coordinates (I think) instead of client coordinates. The original string is not erased.
In order to reduce the problem to its simplest form, I started a new C# windows app. It does nothing except draw a string on the main form. (See code snippet below) If you start the program, the string will appear, then a second string will appear above and to the left. If you restart the program and move the form toward the upper left corner of the screen, the two strings will nearly coincide. This is why I think the second paint is using screen coordinates.
Here is the code - Thank you in advance for any help you can give.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Junk
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs eventArgs)
{
using (Font myFont = new System.Drawing.Font("Helvetica", 40, FontStyle.Italic))
{
eventArgs.Graphics.TranslateTransform(0, 0);
Point p;
eventArgs.Graphics.DrawString("Hello C#", myFont, System.Drawing.Brushes.Red, 200, 200);
} //myFont is automatically disposed here, even if an exception was thrown
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信该方法旨在绘制屏幕坐标,正如您所观察到的。获得您想要的结果的一种方法是将您拥有的客户端坐标转换为该方法期望的屏幕坐标。
I believe that method is intended to draw screen coordinates, as you are observing. One method to get the results you want to achieve is to convert the client coordinates you have to the screen coordinates that the method is expecting.