Drawing onto various controls..

wyrd

Well-known member
Joined
Aug 23, 2002
Messages
1,408
Location
California
Theres an app Im programming that draws images on specific parts of the form. Im using labels to represent the areas and just drawing on to those by doing;

Graphics g = label.CreateGraphics();
g.DrawImage(...);

However whenever I minimize and then unminimize the form it leaves the labels blank as if clearing them. Or a menu simply drops down in front of a label then goes away, it leaves a cleared area of where that menu was.

Should I not be using labels or am I missing something? Im pretty sure I put code to refresh the label images in the Form_Paint method. *ponders* Maybe I should be putting it into the Label_Paint method..
 
If you are working with a label, you can have your own class extend label and then just override the OnPaint method.

This is what I did in a certain situation.

Example, i used a label as a canvas (like in Java) just something i can draw in.

Then i created methods that do things, like maybe draw a circle:

/****************BEGIN CODE*****************/
//note: on is a variable which represents whether or not to draw
//a circle (which is a property value that can be set true/false)

private void PaintCircle(Graphics g)
{
if (this.on == true)
{
g.FillEllipse(Brushes.Aquamarine, new Rectangle(0,0,this.Width, this.Height));
}
else
{
g.FillEllipse(new SolidBrush(this.BackColor), new Rectangle(0,0,this.Width, this.Height));
}
}
/*********************END CODE****************/

now I simply override the onpaint method:

/**************BEGIN CODE*****************/
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
this.PaintCircle(g);
}
/***************end code*******************/

I understand this may not help you in your situation, but I hope it does.
 
Oriolesboy:
Ill toy around and see what I come up with. BTW you can use cs tags to place c# code; <cs>code</cs> (replace < > with [ ])
 
Back
Top