
Signature
Happy Coding!
Morten Wennevik [C# MVP]
Morten,
This isn't completely true.
The drawing will be lost when a repaint is forced. This can be due to
many factors, not just having the window covered. You can resize the
window, you can have it covered, a call to invalidate the window could be
made, the display settings could be changed.
Also, I am curious, what side effects are you getting by using the DC of
a control? When you override the OnPaint method, or attach to the Paint
event, the DC of the control is exactly what you are getting.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
Hi Delme,
Basically, all draing is done using a Graphics object, which can point to
a control, your main form (which is also a control), or a Bitmap image and
so on.
The quick and dirty way to draw using a button event is something like this
// constructor
public Form1()
{
Button b = new Button();
this.Controls.Add(b);
b.Click += new EventHandler(b_Click);
}
// click event method
void b_Click(object sender, EventArgs e)
{
using (Graphics g = this.CreateGraphics())
{
g.DrawEllipse(Pens.Black, this.ClientRectangle);
}
}
But!!!, you should avoid drawing inside your button event for several
reasons.
1) Any drawing will be lost if someting covers the window.
2) Using the graphics object of a control directly may cause unwanted side
effects
Instead do something like this instead
bool flag = false;
void b_Click(object sender, EventArgs e)
{
flag = true;
this.Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
if (flag)
e.Graphics.DrawEllipse(Pens.Black, this.ClientRectangle);
}
OnPaint will be called each time the window needs to be repainted, like
when it is uncovered.
If you swap 'flag = true' with 'flag = !flag' you show and hide the circle.
Good luck!
On Wed, 23 Aug 2006 14:27:56 +0200, Delme Greening
<this_part_is_rubbish@delme@i-byte.co.uk> wrote:
> Thank you very much, having not done anything with graphics I thought I
> would start somewhere simple.
[quoted text clipped - 17 lines]
>> Regards, Vadym Stetsyak
>> www: http://vadmyst.blogspot.com

Signature
Happy Coding!
Morten Wennevik [C# MVP]
Morten Wennevik - 25 Aug 2006 09:40 GMT
Nicholas,
Yes, but isn't it completely true that it will be lost when the control is
covered?
As for unwanted side effects:
[How to use the CreateGraphics method.]
http://www.bobpowell.net/creategraphics.htm
> Morten,
>
[quoted text clipped - 10 lines]
> a control? When you override the OnPaint method, or attach to the Paint
> event, the DC of the control is exactly what you are getting.

Signature
Happy Coding!
Morten Wennevik [C# MVP]