OnPaint 无法正确绘制

发布于 2024-12-26 04:16:18 字数 1150 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半边脸i 2025-01-02 04:16:19

我相信该方法旨在绘制屏幕坐标,正如您所观察到的。获得您想要的结果的一种方法是将您拥有的客户端坐标转换为该方法期望的屏幕坐标。

protected override void OnPaint(PaintEventArgs eventArgs)
{
    using (Font myFont = new System.Drawing.Font("Helvetica", 40,  FontStyle.Italic))
        {
        eventArgs.Graphics.TranslateTransform(0, 0);
        Point p = this.PointToScreen(new Point(200, 200));
        eventArgs.Graphics.DrawString("Hello C#", myFont, System.Drawing.Brushes.Red, p);
        } //myFont is automatically disposed here, even if an exception was thrown            
}

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.

protected override void OnPaint(PaintEventArgs eventArgs)
{
    using (Font myFont = new System.Drawing.Font("Helvetica", 40,  FontStyle.Italic))
        {
        eventArgs.Graphics.TranslateTransform(0, 0);
        Point p = this.PointToScreen(new Point(200, 200));
        eventArgs.Graphics.DrawString("Hello C#", myFont, System.Drawing.Brushes.Red, p);
        } //myFont is automatically disposed here, even if an exception was thrown            
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文