C#)如何通过GDI继续绘制图形 [英] C#) How to keep drawing graphics by GDI

查看:80
本文介绍了C#)如何通过GDI继续绘制图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些不安...


我想画来自

的线,点,矩形等...所以我编码像这样..


公开更新()

{

g = this.CreateGraphics();

g.FillRectangle(Brushes.White,x1,y1,x2,y2);

}


但是在窗口更新后删除矩形(最小化器)或者通过其他窗口获得


如何保持我的绘画?


有人建议在绘画事件中绘制一些东西但我有

问题...


我的问题是......我是否需要在某个班级保留所有抽奖信息

或由paint事件使用的变量???

例如..

如果我使用x1,x2,y1,y2绘制10个矩形,会绘制事件需要

重绘时所有10个数据?


我想做的是......

1.班级发送位置数据到B类

2.当B类接收数据时,B类通过GDI在表格上绘制

3.玻璃更新位置数据(转到1)


像1-2-3-1-2-3-2-2-3循环大约1000多次


在这种情况下我是否还需要在绘制事件中绘制一些内容?

并且B类需要保留所有数据以用于重绘时间??


请帮帮我..

解决方案

重新绘制矩形或你做的任何事情,你必须调用

Invalidate()方法。为了最小化或重叠...使用下面的方法

方法。


protected override void DefWndProc(ref message m)

{

if(m.Msg == 0x47)// WM_WINDOWPOSCHANGED

{

this.Invalidate();

}


base.DefWndProc(ref m);

}

正如你所听到的,你应该画上Paint事件。 (你将把图形输入到PaintEventArgs e属性中)。也许你可以使用UserControl而不是

表格...:)\

希望这有助于

< br>

6月8日,5月49日,eusebiu< MarcuEuse ... @ gmail.comwrote:


重新绘制矩形或任何你做的事情,你必须调用

Invalidate()方法。为了最小化或重叠...使用下面的方法

方法。


protected override void DefWndProc(ref message m)

{

if(m.Msg == 0x47)// WM_WINDOWPOSCHANGED

{

this.Invalidate();

}


base.DefWndProc(ref m);

}


正如你所听到的,你应该在Paint事件上绘画。 (你将把图形输入到PaintEventArgs e属性中)。也许代替

表格你应该使用UserControl ......:)\

希望这有助于



感谢您的回复..

现在我正在使用UserControl绘制一些东西......

但....

我真正想知道的是......看下面的代码..


命名空间testWindow

{

public partial class UserControl1:UserControl

{

public int x,y;

public UserControl1()

{

InitializeComponent();

x = 10;

y = 10;

}


public void update()

{

x = x + 2;

y = y + 2;

Invalidate();

}


private void UserControl1_Paint(对象发送者,PaintEventArgs

e)

{

Graphics g = e.Graphics;


Pen p = new Pen(Color.Black,3);

g.DrawRectangle(p,x,y,x,y);

}

}

}


名称空间testWindow

{

公共部分类Form1:表格

{


public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender,EventArgs e)

{

userControl11.update();

}

}

}


根据以上代码...

如果用户单击表单上的按钮,用户控制绘制矩形在

10,10,10,10

如果用户点击更多...则矩形是更改大小(+ 2,+ 2,
+2 ....)

此时,用户控件只绘制最后一个因为Paint事件

只绘制一个矩形..


这不是我想要的......

我需要所有的矩形,所以我绘制其他函数而不是绘制事件。

如果用户单击按钮4次,该功能绘制4个矩形...

对我有好处。

但是..问题是当窗口是upd时ated(尺寸变化,

minimalizer等......)图纸并未全部恢复


所以我想知道用户控制需要保留所有数据在某处重新绘制

..(可能是数组列表或某些...)

但这也是不可能的,因为绘图数据太多而无法保持
$ b在我的情况下是$ b ..


有什么好主意或解决方案吗?


为什么要打扰B类,因为A类有它的位置数据,并且大概是
知道要绘制什么,给它一个Draw例程,它将Graphics对象作为一个

参数并绘制正确位置所需的内容。制作一个类A对象的集合和表单中的事件处理程序

遍历集合调用Draw例程并传递

形成要绘制的图形对象。


" ct ***** @ gmail.com"写道:


我有一些静止...


我想画线,点,矩形等。 。来自

所以我的代码是这样的..


公开更新()

{

g = this.CreateGraphics();

g.FillRectangle(Brushes.White,x1,y1,x2,y2);

}

但是在窗口更新后移除矩形(最小化或重叠

通过其他窗口)

如何保持绘图?


有人建议在油漆事件中画出一些东西,但我有

问题...


我的问题是......我做需要将所有抽奖信息保存在某个班级

或变量以供油漆事件使用???

例如..

如果我画画使用x1,x2,y1,y2的10个矩形确实需要绘制事件需要

重绘时所有10个数据?


我想要做的是。 ..

1.班级向B班发送位置数据

2.当B级接收数据时,B级通过GDI在表格上绘制

3.玻璃更新位置数据(转到1)


循环像1- 2-3-1-2-3-2-3-3大约1000次


在这种情况下我是否还需要在油漆事件中画一些东西?

B类是否需要保留所有数据以便在重绘时使用?


请帮助我..


I have some quiestion...

I want to draw line,point,rectangles and etc... on the from
So I code like this..

public update()
{
g = this.CreateGraphics();
g.FillRectangle(Brushes.White, x1, y1, x2, y2);
}

But the rectangle is removed after window update (minimizer or overrap
by other window)
How can I keep my drawing??

Someone recommand to draw somethins in paint event but I have
question...

My question is... do I need to keep all draw information at some class
or variable to use by paint event???
For example..
If I draw 10 rectangles by using x1,x2, y1,y2, does paint event need
all 10 datas when redraw??

What I want to do is ...
1. A class send position data to B class
2. when B class receive data, B class draw on form by GDI
3. A glass update position data (goto 1)

looping like 1-2-3- 1-2-3- 1-2-3 about 1000 more times

in this case do I also need to draw something at paint event?
and does B class need to keep all data to use at redraw time??

please help me..

解决方案

To get redraw the rectangles or whatever your doing, you have to call
Invalidate() method. For minimizing or overrap... use the bellow
method.

protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x47)//WM_WINDOWPOSCHANGED
{
this.Invalidate();
}

base.DefWndProc(ref m);
}
As you''ve heard, you should paint on the Paint event. ( you''ll have
the graphics into PaintEventArgs e properties). And maybe instead of a
form you should use a UserControl... :)\
Hope this helps


On 6 8 , 5 49 , eusebiu <MarcuEuse...@gmail.comwrote:

To get redraw the rectangles or whatever your doing, you have to call
Invalidate() method. For minimizing or overrap... use the bellow
method.

protected override void DefWndProc(ref Message m)
{
if (m.Msg == 0x47)//WM_WINDOWPOSCHANGED
{
this.Invalidate();
}

base.DefWndProc(ref m);
}

As you''ve heard, you should paint on the Paint event. ( you''ll have
the graphics into PaintEventArgs e properties). And maybe instead of a
form you should use a UserControl... :)\
Hope this helps

Thanks for your reply..
Now I''m using UserControl to draw something...
but....
What I really really wonder is.... look at the below code..

namespace testWindow
{
public partial class UserControl1 : UserControl
{
public int x, y;
public UserControl1()
{
InitializeComponent();
x = 10;
y = 10;
}

public void update()
{
x = x + 2;
y = y + 2;
Invalidate();
}

private void UserControl1_Paint(object sender, PaintEventArgs
e)
{
Graphics g = e.Graphics;

Pen p = new Pen(Color.Black, 3);
g.DrawRectangle(p, x, y, x, y);
}
}
}

namespace testWindow
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
userControl11.update();
}
}
}

according to above code...
if user click button on form, user control draw rectangle in
10,10,10,10
if user click more more... then rectangle is change size (+2, +2,
+2....)
at this time, user control is draw only last one because Paint event
draw only one rectangles..

This is not what I wants..
I need all rectangles, so I draw on other function not paint event.
If user click button 4 times, the function draw 4 rectangles... that
good for me.
But.. the problem is that when window is updated (size change,
minimizer and etc...) the drawing is not all recovered

So I wonder that user control need to keep all data to redraw at
somewhere.. (maybe array list or some...)
But this also impossible because the drawing data is too much to keep
in my case..

Is there any good idea or solution?




Why bother with class B, since class A has its position data, and presumably
knows what to draw, give it a Draw routine that takes a Graphics object as an
argument and draws what is required at the correct position. Make a
collection of the class A objects and in the forms Paint event handler
iterate through the collection calling the Draw routines and passing the
forms Graphics object to draw on.

"ct*****@gmail.com" wrote:

I have some quiestion...

I want to draw line,point,rectangles and etc... on the from
So I code like this..

public update()
{
g = this.CreateGraphics();
g.FillRectangle(Brushes.White, x1, y1, x2, y2);
}

But the rectangle is removed after window update (minimizer or overrap
by other window)
How can I keep my drawing??

Someone recommand to draw somethins in paint event but I have
question...

My question is... do I need to keep all draw information at some class
or variable to use by paint event???
For example..
If I draw 10 rectangles by using x1,x2, y1,y2, does paint event need
all 10 datas when redraw??

What I want to do is ...
1. A class send position data to B class
2. when B class receive data, B class draw on form by GDI
3. A glass update position data (goto 1)

looping like 1-2-3- 1-2-3- 1-2-3 about 1000 more times

in this case do I also need to draw something at paint event?
and does B class need to keep all data to use at redraw time??

please help me..


这篇关于C#)如何通过GDI继续绘制图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆