Windows Develop Bookmark and Share   
 index > Windows Forms General > Drawing on[/underneath!] Controls
 

Drawing on[/underneath!] Controls

Is it possible to draw a line as shown in the pic on a single form? The line in the image is drawn on a second form which is transparent so form1 is visible.

OmidQRose  Wednesday, December 27, 2006 5:06 AM
Try this instead:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Line : Control {
protected override CreateParams CreateParams {
get {
// Turn on the WS_EX_TRANSPARENT style
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent) {
// Do *not* paint the background
}
protected override void OnPaint(PaintEventArgs e) {
// Draw the line, just a demo here:
e.Graphics.DrawLine(Pens.Black, 0, 0, this.Width, this.Height);
base.OnPaint(e);
}
}

If you animate the line in any way, you'll need to invalidate the control's Parent so the background is redrawn correctly.
nobugz  Thursday, December 28, 2006 6:01 PM
No. You've got two distinct windows here. One for the form, another for the control. You can't draw on both.
nobugz  Wednesday, December 27, 2006 1:03 PM
Can we then create the line as a control and set its background to transparent so the list box is visible through?
OmidQRose  Thursday, December 28, 2006 7:01 AM
Yes, that would work. Very similar to what you did with the transparent form.
nobugz  Thursday, December 28, 2006 2:06 PM

So why does my line control cover the listbox?

class Line : Control
{
 Pen _Pen;

 Line()
 {
   this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
   this.BackColor = Color.Transparent; 

 }

 public void SetBounds(int Width)
 {
   _Pen = new Pen(new SolidBrush(Color.Red));
   _Pen.Width = Width;
   this.Location = new Point(0, 0);
   this.Height = Form1.ClientRectangle.Height;
   this.Width = Form1.ClientRectangle.Width;
   Invalidate();
 }

 protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
 {
   base.OnPaint(e);
   _Pen.DashCap = DashCap.Flat;
   _Pen.DashStyle = DashStyle.DashDotDot;
   _Pen.StartCap = LineCap.Flat;
   _Pen.EndCap = LineCap.ArrowAnchor;
   Graphics g = this.CreateGraphics;
   g.DrawBezier(_Pen, 20, 20, 500, 300, 550, 320, 510, 60);
 }
}

OmidQRose  Thursday, December 28, 2006 5:44 PM
Because this.BackColor = Color.Transparent; doesn't realy work as expected.
JRQ  Thursday, December 28, 2006 5:48 PM
So any alternatives?
OmidQRose  Thursday, December 28, 2006 5:53 PM
Try this instead:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Line : Control {
protected override CreateParams CreateParams {
get {
// Turn on the WS_EX_TRANSPARENT style
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs pevent) {
// Do *not* paint the background
}
protected override void OnPaint(PaintEventArgs e) {
// Draw the line, just a demo here:
e.Graphics.DrawLine(Pens.Black, 0, 0, this.Width, this.Height);
base.OnPaint(e);
}
}

If you animate the line in any way, you'll need to invalidate the control's Parent so the background is redrawn correctly.
nobugz  Thursday, December 28, 2006 6:01 PM
Thats a really working one thanks. BTW, could you please explain what it does in the CreateParams?
OmidQRose  Thursday, December 28, 2006 6:29 PM
As indicated, it turns on the WS_EX_TRANSPARENT window style. With this style set, Windows ensures that all windows "below" the window get painted first and the transparent window last.
nobugz  Thursday, December 28, 2006 7:09 PM

You can use google to search for other answers

Custom Search

More Threads

• synchronize time?!
• Winfoms Multithreading Issue - Moving from 1.1 - 2.0
• How accessing a form1 from a form2...
• Write to process??
• calculating the date on friday of every third week of a month
• Stop Ctrl+Tab in the tabcontrol
• Closing mainform by childforms button
• Split string to datatable
• How do i close all threads when the exit button is clicked?
• Needed Changes