不使用 OnPaint 方法绘制椭圆

发布于 2024-08-06 14:00:29 字数 807 浏览 4 评论 0原文

我正在寻找一些 GDI 教程,但到目前为止我发现的所有内容都适用于 OnPaint 方法,该方法将 Paintarguments 传递给 Graphics。我还没有找到如何从头开始,我的意思是如何使用 Graphics 类本身? 这是我的 treid 的整个代码,但它对我不起作用:

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Pen pen= new Pen(Color.Red, 3);
            Graphics g;
            g = this.CreateGraphics();
    g.DrawEllipse(pen, 150, 150, 100, 100);
        }
    }
}

它只是不做任何事情。我尝试了新的形式,没有任何结果。

先感谢您!

I was looking about some GDI tutorial but everything I have found so far works with OnPaint method, which passes Paintarguments to Graphics. I have not found how to start from scratch, I mean how to use Graphics class itself?
This is the whole code I have treid that just doesnt work for me:

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Pen pen= new Pen(Color.Red, 3);
            Graphics g;
            g = this.CreateGraphics();
    g.DrawEllipse(pen, 150, 150, 100, 100);
        }
    }
}

It just doesnt do anything. I tried it in new form, nothing.

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

您的好友蓝忘机已上羡 2024-08-13 14:00:30

代码可能没问题,它正在绘制一个椭圆,正如您所希望的那样。然而,在 Load 事件之后,当表单显示时,将会有一个 PaintBackground 事件和一个 PaintEvent 事件。默认情况下,PaintBackground 将擦除控件的内容,从而有效地删除您刚刚绘制的椭圆。

绘制是一个两阶段的过程:

for each (region in set of regions that need updating)
{
  PaintBackground (region)
  Paint (region)
}

窗口管理器仅重绘控件中需要更新的部分,如果控件的内容未更改或没有用户操作更改控件的可见性,则不会完成绘制。

那么,为什么要在Load方法中绘制椭圆呢?通常,您只想在需要绘制某些内容时绘制某些内容,并且在 PaintBackgroundPaint 事件中告知您的表单何时需要绘制某些内容。

您担心闪烁吗?还是速度问题?椭圆可以快速绘制。然而,闪烁更难修复。您需要创建一个位图,绘制位图并在 Paint 事件期间将位图传输到控件。另外,让 PaintBackground 事件不执行任何操作 - 不擦除控件,擦除会导致闪烁。

编辑:举个例子,我在这里使用 DevStudio 2005。

  1. 创建一个新的 C# winform 应用程序。
  2. 在 Form1.cs 中添加以下内容:

    protected override void OnPaintBackground (PaintEventArgs e)
    {
      // 什么都不做!防止闪烁
    }
    
    protected override void OnPaint (PaintEventArgs e)
    {
      e.Graphics.FillRectangle(新SolidBrush(BackColor),e.ClipRectangle);
    
      观点
        鼠标=PointToClient(MousePosition);
    
      e.Graphics.DrawEllipse(新笔(前景色),新矩形(mouse.X - 20,mouse.Y - 10, 40, 20));
    }
    
    protected override void OnMouseMove (MouseEventArgs e)
    {
      base.OnMouseMove(e);
      无效();
    }
    
  3. 编译并运行。

The code is probably OK, it is drawing a ellipse as you are hoping for. However, after the Load event, there will be a PaintBackground event and a PaintEvent as the form is displayed. The PaintBackground will, by default, erase the contents of the control, effectively removing the ellipse you've just drawn.

Painting is a two stage process:

for each (region in set of regions that need updating)
{
  PaintBackground (region)
  Paint (region)
}

The window manager only redraws the parts of the control that require updating, if the contents of the control haven't changed or no user action has altered the control's visibility then no painting is done.

So, why do you want to draw the ellipse in the Load method? Usually, you only want to draw something when something needs to be drawn, and your form is told when something needs drawing in the PaintBackground and Paint events.

Are you worried about flickering? Or is it a speed issue? Ellipses are quick to draw. Flickering, however, is harder to fix. You need to create a bitmap, draw to the bitmap and blit the bitmap to the control during the Paint event. Also, make the PaintBackground event do nothing - no erasing the control, it's the erasing that causes the flicker.

EDIT: An example, I'm using DevStudio 2005 here.

  1. Create a new C# winform application.
  2. In Form1.cs add the following:

    protected override void OnPaintBackground (PaintEventArgs e)
    {
      // do nothing! prevents flicker
    }
    
    protected override void OnPaint (PaintEventArgs e)
    {
      e.Graphics.FillRectangle (new SolidBrush (BackColor), e.ClipRectangle);
    
      Point
        mouse = PointToClient (MousePosition);
    
      e.Graphics.DrawEllipse (new Pen (ForeColor), new Rectangle (mouse.X - 20, mouse.Y - 10, 40, 20));
    }
    
    protected override void OnMouseMove (MouseEventArgs e)
    {
      base.OnMouseMove (e);
      Invalidate ();
    }
    
  3. Compile and run.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文