PDA

Click to See Complete Forum and Search --> : Draw Common "Picture" on All Controls


jmcilhinney
Oct 30th, 2008, 07:53 AM
VB version here (http://www.vbforums.com/showthread.php?t=531022).

The following code uses a single Paint event handler to draw the same "picture" on every control on a form. Specifically it draws a line from the top, left corner of the form to the bottom, right corner and "over" and controls that happen to be on that diagonal.private void Form1_Load(object sender, EventArgs e)
{
Control ctl = this.GetNextControl(this, true);

while (ctl != null)
{
ctl.Paint += new PaintEventHandler(Form1_Paint);
ctl = this.GetNextControl(ctl, true);
}
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
// Get the points relative to the form.
Point start = new Point(0, 0);
Point end = new Point(this.ClientSize);

Control paintee = (Control)sender;

// Translate the points relative to the control being painted.
start = paintee.PointToClient(this.PointToScreen(start));
end = paintee.PointToClient(this.PointToScreen(end));

e.Graphics.DrawLine(Pens.Black, start, end);
}You can use a similar pattern to draw any "picture" you like on your form. The important part is the use of PointToScreen and PointToClient to convert coordinates from the form's frame of reference to that of the control currently being painted.