不使用 OnPaint 方法绘制椭圆
我正在寻找一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码可能没问题,它正在绘制一个椭圆,正如您所希望的那样。然而,在 Load 事件之后,当表单显示时,将会有一个
PaintBackground
事件和一个PaintEvent
事件。默认情况下,PaintBackground
将擦除控件的内容,从而有效地删除您刚刚绘制的椭圆。绘制是一个两阶段的过程:
窗口管理器仅重绘控件中需要更新的部分,如果控件的内容未更改或没有用户操作更改控件的可见性,则不会完成绘制。
那么,为什么要在Load方法中绘制椭圆呢?通常,您只想在需要绘制某些内容时绘制某些内容,并且在
PaintBackground
和Paint
事件中告知您的表单何时需要绘制某些内容。您担心闪烁吗?还是速度问题?椭圆可以快速绘制。然而,闪烁更难修复。您需要创建一个位图,绘制位图并在 Paint 事件期间将位图传输到控件。另外,让
PaintBackground
事件不执行任何操作 - 不擦除控件,擦除会导致闪烁。编辑:举个例子,我在这里使用 DevStudio 2005。
在 Form1.cs 中添加以下内容:
编译并运行。
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 aPaintEvent
as the form is displayed. ThePaintBackground
will, by default, erase the contents of the control, effectively removing the ellipse you've just drawn.Painting is a two stage process:
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
andPaint
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.
In Form1.cs add the following:
Compile and run.