By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,997 Members | 1,260 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,997 IT Pros & Developers. It's quick & easy.

Is it possible to draw lines between two froms?

P: n/a
Hi, All
Greetings!
I want to develop as appllication that requires a line-drawing function
in the blank area between two forms. I have looked up the MSDN, it says that
a graphics object need a reference to a control or a form. I guess it means
that lines can't be draw on blank area between two forms. Can anybody
guarantee this for me? Is there any method can realize this function? I
mainly want to draw a line from a button in form1 to current position of the
mouse. The line should keep changing when the mouse is moving and the mouse
will sure move outside the form1 to the form2.
Thank you in advance for your time and patient.
Feb 25 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Hi,

If Form1 and Form2 both reside inside a parent form (MDIParent) have the parent draw the line. Form1 and Form2 needs to be MDIChildren. The parent can then subscribe to the Move event, SizeChanged event or any other event that may cause a need to redraw the line.

On Sun, 25 Feb 2007 16:19:04 +0100, zhaow <nu*****@hotmail.comwrote:
Hi, All
Greetings!
I want to develop as appllication that requires a line-drawing function
in the blank area between two forms. I have looked up the MSDN, it says that
a graphics object need a reference to a control or a form. I guess it means
that lines can't be draw on blank area between two forms. Can anybody
guarantee this for me? Is there any method can realize this function? I
mainly want to draw a line from a button in form1 to current position of the
mouse. The line should keep changing when the mouse is moving and the mouse
will sure move outside the form1 to the form2.
Thank you in advance for your time and patient.


--
Happy coding!
Morten Wennevik [C# MVP]
Feb 25 '07 #2

P: n/a
Thank you very much! I will try it later. However, is there any other
methods for drawing a line outside a form? Thanks!

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tobbosdrdj93y5@stone...
Hi,

If Form1 and Form2 both reside inside a parent form (MDIParent) have the
parent draw the line. Form1 and Form2 needs to be MDIChildren. The
parent can then subscribe to the Move event, SizeChanged event or any
other event that may cause a need to redraw the line.

On Sun, 25 Feb 2007 16:19:04 +0100, zhaow <nu*****@hotmail.comwrote:
>Hi, All
Greetings!
I want to develop as appllication that requires a line-drawing
function
in the blank area between two forms. I have looked up the MSDN, it says
that
a graphics object need a reference to a control or a form. I guess it
means
that lines can't be draw on blank area between two forms. Can anybody
guarantee this for me? Is there any method can realize this function? I
mainly want to draw a line from a button in form1 to current position of
the
mouse. The line should keep changing when the mouse is moving and the
mouse
will sure move outside the form1 to the form2.
Thank you in advance for your time and patient.

--
Happy coding!
Morten Wennevik [C# MVP]

Feb 25 '07 #3

P: n/a
You can create a graphics object of the desktop and draw directly on thedesktop. To do this, use the Win32 method GetDC(hWnd). If you pass IntPtr.Zero you will get the handle to the desktop DC (device context). From this create a Graphics object using Graphics.FromHdc.

To use the Win32 method add

[DllImport("User32.dll")]
public extern static IntPtr GetDC(IntPtr hWnd);

[DllImport("User32.dll")]
public extern static IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

To draw on the desktop use

IntPtr hDC = GetDC(IntPtr.Zero);
using(Graphics g = Graphics.FromHdc(hDC))
{
Graphics.DrawLine(Pens.Black, form1.Location, form2.Location);
}
ReleaseDC(IntPtr.Zero, hDC);

However, I would advice against drawing on the desktop and instead consider a MDI solution.
On Sun, 25 Feb 2007 17:42:34 +0100, nuber19 <nu*****@hotmail.comwrote:
Thank you very much! I will try it later. However, is there any other
methods for drawing a line outside a form? Thanks!

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tobbosdrdj93y5@stone...
>Hi,

If Form1 and Form2 both reside inside a parent form (MDIParent) have the
parent draw the line. Form1 and Form2 needs to be MDIChildren. The
parent can then subscribe to the Move event, SizeChanged event or any
other event that may cause a need to redraw the line.

On Sun, 25 Feb 2007 16:19:04 +0100, zhaow <nu*****@hotmail.comwrote:
>>Hi, All
Greetings!
I want to develop as appllication that requires a line-drawing
function
in the blank area between two forms. I have looked up the MSDN, it says
that
a graphics object need a reference to a control or a form. I guess it
means
that lines can't be draw on blank area between two forms. Can anybody
guarantee this for me? Is there any method can realize this function? I
mainly want to draw a line from a button in form1 to current position of
the
mouse. The line should keep changing when the mouse is moving and the
mouse
will sure move outside the form1 to the form2.
Thank you in advance for your time and patient.

--
Happy coding!
Morten Wennevik [C# MVP]




--
Happy coding!
Morten Wennevik [C# MVP]
Feb 25 '07 #4

P: n/a
Got it. Thank you very much!
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tobf80vjdj93y5@stone...
You can create a graphics object of the desktop and draw directly on the
desktop. To do this, use the Win32 method GetDC(hWnd). If you pass
IntPtr.Zero you will get the handle to the desktop DC (device context).
From this create a Graphics object using Graphics.FromHdc.

To use the Win32 method add

[DllImport("User32.dll")]
public extern static IntPtr GetDC(IntPtr hWnd);

[DllImport("User32.dll")]
public extern static IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);

To draw on the desktop use

IntPtr hDC = GetDC(IntPtr.Zero);
using(Graphics g = Graphics.FromHdc(hDC))
{
Graphics.DrawLine(Pens.Black, form1.Location, form2.Location);
}
ReleaseDC(IntPtr.Zero, hDC);

However, I would advice against drawing on the desktop and instead consider
a MDI solution.
On Sun, 25 Feb 2007 17:42:34 +0100, nuber19 <nu*****@hotmail.comwrote:
Thank you very much! I will try it later. However, is there any other
methods for drawing a line outside a form? Thanks!

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tobbosdrdj93y5@stone...
>Hi,

If Form1 and Form2 both reside inside a parent form (MDIParent) have the
parent draw the line. Form1 and Form2 needs to be MDIChildren. The
parent can then subscribe to the Move event, SizeChanged event or any
other event that may cause a need to redraw the line.

On Sun, 25 Feb 2007 16:19:04 +0100, zhaow <nu*****@hotmail.comwrote:
>>Hi, All
Greetings!
I want to develop as appllication that requires a line-drawing
function
in the blank area between two forms. I have looked up the MSDN, it says
that
a graphics object need a reference to a control or a form. I guess it
means
that lines can't be draw on blank area between two forms. Can anybody
guarantee this for me? Is there any method can realize this function? I
mainly want to draw a line from a button in form1 to current position of
the
mouse. The line should keep changing when the mouse is moving and the
mouse
will sure move outside the form1 to the form2.
Thank you in advance for your time and patient.

--
Happy coding!
Morten Wennevik [C# MVP]




--
Happy coding!
Morten Wennevik [C# MVP]
Feb 25 '07 #5

P: n/a
Hi, Morten,
I declare 3 forms, and set the mainform as

mainform.IsMdiContainer=true;

to make it can contain subform1 and subform2.However, It looks like a line
can not be drawn in a form which is a MDI container.I can only draw lines in
subform1 and subform2 by using Form_Paint event. Do you have any comments
about this? Thank you !
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tobbosdrdj93y5@stone...
Hi,

If Form1 and Form2 both reside inside a parent form (MDIParent) have the
parent draw the line. Form1 and Form2 needs to be MDIChildren. The
parent can then subscribe to the Move event, SizeChanged event or any
other event that may cause a need to redraw the line.

On Sun, 25 Feb 2007 16:19:04 +0100, zhaow <nu*****@hotmail.comwrote:
>Hi, All
Greetings!
I want to develop as appllication that requires a line-drawing
function
in the blank area between two forms. I have looked up the MSDN, it says
that
a graphics object need a reference to a control or a form. I guess it
means
that lines can't be draw on blank area between two forms. Can anybody
guarantee this for me? Is there any method can realize this function? I
mainly want to draw a line from a button in form1 to current position of
the
mouse. The line should keep changing when the mouse is moving and the
mouse
will sure move outside the form1 to the form2.
Thank you in advance for your time and patient.

--
Happy coding!
Morten Wennevik [C# MVP]

Feb 25 '07 #6

P: n/a
Sorry about that,

When you set a Form to IsMdiContainer = true you also create an MdiClient control which is added to the form. This control is the gray background you see. You need to subscribe to the MdiClient's Paint event and draw in the client surface.

The code below will draw a line between the upper left corner of the MdiParent and an MdiChild

MdiClient client = null;
Form1 f = new Form1();

public MDIParent()
{
InitializeComponent();

foreach (Control c in this.Controls)
{
if (c is MdiClient)
client = (MdiClient)c;
}

client.Paint += new PaintEventHandler(client_Paint);

f.MdiParent = this;
f.Show();

f.Move += new EventHandler(f_Move);
}

void client_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black, new Point(0, 0), f.Location);
}

void f_Move(object sender, EventArgs e)
{
client.Invalidate();
}
On Sun, 25 Feb 2007 23:14:08 +0100, Joe Gallagher <nu*****@hotmail.comwrote:
Hi, Morten,
I declare 3 forms, and set the mainform as

mainform.IsMdiContainer=true;

to make it can contain subform1 and subform2.However, It looks like a line
can not be drawn in a form which is a MDI container.I can only draw lines in
subform1 and subform2 by using Form_Paint event. Do you have any comments
about this? Thank you !
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tobbosdrdj93y5@stone...
>Hi,

If Form1 and Form2 both reside inside a parent form (MDIParent) have the
parent draw the line. Form1 and Form2 needs to be MDIChildren. The
parent can then subscribe to the Move event, SizeChanged event or any
other event that may cause a need to redraw the line.

On Sun, 25 Feb 2007 16:19:04 +0100, zhaow <nu*****@hotmail.comwrote:
>>Hi, All
Greetings!
I want to develop as appllication that requires a line-drawing
function
in the blank area between two forms. I have looked up the MSDN, it says
that
a graphics object need a reference to a control or a form. I guess it
means
that lines can't be draw on blank area between two forms. Can anybody
guarantee this for me? Is there any method can realize this function? I
mainly want to draw a line from a button in form1 to current position of
the
mouse. The line should keep changing when the mouse is moving and the
mouse
will sure move outside the form1 to the form2.
Thank you in advance for your time and patient.

--
Happy coding!
Morten Wennevik [C# MVP]




--
Happy coding!
Morten Wennevik [C# MVP]
Feb 26 '07 #7

P: n/a
Thank you very much for you reply!
According to you reply, I modified my codes, and it can draw line. However,
I want to make it draw line dynamically when the mouse is moving. But the
application can only draw a static line when it starts. I paste my related
code as below, please have a look at it if you have a second. I do not know
how to use the MDIParent Function in you code. I think it may be the reason.
Thank you in advance.

class MyApplicationContext : ApplicationContext
{

private int formCount;
private Form1 mainform, subform1,subform2;
private var tvar = new var();
MdiClient client = null;
private int currx=0;
private int curry=0;

public MyApplicationContext()
{
formCount = 0;
// Handle the ApplicationExit event to know when the application
is exiting.
mainform = new Form1();
mainform.SetBounds(0, 0, 1280, 800);
mainform.IsMdiContainer=true;
foreach (Control c in mainform.Controls)
{
if (c is MdiClient)
client = (MdiClient)c;
}
client.Paint += new PaintEventHandler(client_Paint);

subform1 = new Form1();
subform1.Closed += new EventHandler(OnFormClosed);
subform1.SetDesktopLocation(50, 100);

subform1.changeText("Start");
subform1.buttonShow();
formCount++;
subform2 = new Form1();
subform2.Closed += new EventHandler(OnFormClosed);
subform2.SetDesktopLocation(600,100);
subform2.changeText("Stop");
subform2.buttonShow();
formCount++;
subform1.MdiParent = mainform;
subform2.MdiParent = mainform;

// Show both forms.
mainform.Show();
mainform.MouseMove += new MouseEventHandler(Form_Move);
mainform.MouseHover += new EventHandler(Form_Move2);
subform1.Show();
subform2.Show();
}
void client_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red,200,300,currx,curry);
}

void Form_Move(object sender, MouseEventArgs e)
{
currx = Control.MousePosition.X;
curry = Control.MousePosition.Y;
client.Invalidate();
}
void Form_Move2(object sender, EventArgs e)
{
currx = Control.MousePosition.X;
curry = Control.MousePosition.Y;
client.Invalidate();
}

private void OnFormClosed(object sender, EventArgs e)
{
// When a form is closed, decrement the count of open forms.

// When the count gets to 0, exit the app by calling
// ExitThread().
formCount--;
if (formCount == 0)
{
tvar.close();
ExitThread();
}
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
MyApplicationContext context = new MyApplicationContext();
Application.Run(context);
}
}

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.todbhrk4dj93y5@stone...
Sorry about that,

When you set a Form to IsMdiContainer = true you also create an MdiClient
control which is added to the form. This control is the gray background you
see. You need to subscribe to the MdiClient's Paint event and draw in the
client surface.

The code below will draw a line between the upper left corner of the
MdiParent and an MdiChild

MdiClient client = null;
Form1 f = new Form1();

public MDIParent()
{
InitializeComponent();

foreach (Control c in this.Controls)
{
if (c is MdiClient)
client = (MdiClient)c;
}

client.Paint += new PaintEventHandler(client_Paint);

f.MdiParent = this;
f.Show();

f.Move += new EventHandler(f_Move);
}

void client_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black, new Point(0, 0), f.Location);
}

void f_Move(object sender, EventArgs e)
{
client.Invalidate();
}
On Sun, 25 Feb 2007 23:14:08 +0100, Joe Gallagher <nu*****@hotmail.com>
wrote:
Hi, Morten,
I declare 3 forms, and set the mainform as

mainform.IsMdiContainer=true;

to make it can contain subform1 and subform2.However, It looks like a line
can not be drawn in a form which is a MDI container.I can only draw lines
in
subform1 and subform2 by using Form_Paint event. Do you have any comments
about this? Thank you !
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tobbosdrdj93y5@stone...
>Hi,

If Form1 and Form2 both reside inside a parent form (MDIParent) have the
parent draw the line. Form1 and Form2 needs to be MDIChildren. The
parent can then subscribe to the Move event, SizeChanged event or any
other event that may cause a need to redraw the line.

On Sun, 25 Feb 2007 16:19:04 +0100, zhaow <nu*****@hotmail.comwrote:
>>Hi, All
Greetings!
I want to develop as appllication that requires a line-drawing
function
in the blank area between two forms. I have looked up the MSDN, it says
that
a graphics object need a reference to a control or a form. I guess it
means
that lines can't be draw on blank area between two forms. Can anybody
guarantee this for me? Is there any method can realize this function? I
mainly want to draw a line from a button in form1 to current position of
the
mouse. The line should keep changing when the mouse is moving and the
mouse
will sure move outside the form1 to the form2.
Thank you in advance for your time and patient.

--
Happy coding!
Morten Wennevik [C# MVP]




--
Happy coding!
Morten Wennevik [C# MVP]
Feb 27 '07 #8

P: n/a
I checked it again and think that It may because subform1 and subform2 are
above the mainform, then the main form can not get focus..So it can only
draw a line at the beginning.
"Joe Gallagher" <nu*****@hotmail.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Thank you very much for you reply!
According to you reply, I modified my codes, and it can draw line.
However, I want to make it draw line dynamically when the mouse is moving.
But the application can only draw a static line when it starts. I paste my
related code as below, please have a look at it if you have a second. I do
not know how to use the MDIParent Function in you code. I think it may be
the reason. Thank you in advance.

class MyApplicationContext : ApplicationContext
{

private int formCount;
private Form1 mainform, subform1,subform2;
private var tvar = new var();
MdiClient client = null;
private int currx=0;
private int curry=0;

public MyApplicationContext()
{
formCount = 0;
// Handle the ApplicationExit event to know when the
application is exiting.
mainform = new Form1();
mainform.SetBounds(0, 0, 1280, 800);
mainform.IsMdiContainer=true;
foreach (Control c in mainform.Controls)
{
if (c is MdiClient)
client = (MdiClient)c;
}
client.Paint += new PaintEventHandler(client_Paint);

subform1 = new Form1();
subform1.Closed += new EventHandler(OnFormClosed);
subform1.SetDesktopLocation(50, 100);

subform1.changeText("Start");
subform1.buttonShow();
formCount++;
subform2 = new Form1();
subform2.Closed += new EventHandler(OnFormClosed);
subform2.SetDesktopLocation(600,100);
subform2.changeText("Stop");
subform2.buttonShow();
formCount++;
subform1.MdiParent = mainform;
subform2.MdiParent = mainform;

// Show both forms.
mainform.Show();
mainform.MouseMove += new MouseEventHandler(Form_Move);
mainform.MouseHover += new EventHandler(Form_Move2);
subform1.Show();
subform2.Show();
}
void client_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red,200,300,currx,curry);
}

void Form_Move(object sender, MouseEventArgs e)
{
currx = Control.MousePosition.X;
curry = Control.MousePosition.Y;
client.Invalidate();
}
void Form_Move2(object sender, EventArgs e)
{
currx = Control.MousePosition.X;
curry = Control.MousePosition.Y;
client.Invalidate();
}

private void OnFormClosed(object sender, EventArgs e)
{
// When a form is closed, decrement the count of open forms.

// When the count gets to 0, exit the app by calling
// ExitThread().
formCount--;
if (formCount == 0)
{
tvar.close();
ExitThread();
}
}
}
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(fals e);
MyApplicationContext context = new MyApplicationContext();
Application.Run(context);
}
}

"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.todbhrk4dj93y5@stone...
Sorry about that,

When you set a Form to IsMdiContainer = true you also create an MdiClient
control which is added to the form. This control is the gray background
you see. You need to subscribe to the MdiClient's Paint event and draw in
the client surface.

The code below will draw a line between the upper left corner of the
MdiParent and an MdiChild

MdiClient client = null;
Form1 f = new Form1();

public MDIParent()
{
InitializeComponent();

foreach (Control c in this.Controls)
{
if (c is MdiClient)
client = (MdiClient)c;
}

client.Paint += new PaintEventHandler(client_Paint);

f.MdiParent = this;
f.Show();

f.Move += new EventHandler(f_Move);
}

void client_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Black, new Point(0, 0), f.Location);
}

void f_Move(object sender, EventArgs e)
{
client.Invalidate();
}
On Sun, 25 Feb 2007 23:14:08 +0100, Joe Gallagher <nu*****@hotmail.com>
wrote:
>Hi, Morten,
I declare 3 forms, and set the mainform as

mainform.IsMdiContainer=true;

to make it can contain subform1 and subform2.However, It looks like a
line
can not be drawn in a form which is a MDI container.I can only draw lines
in
subform1 and subform2 by using Form_Paint event. Do you have any comments
about this? Thank you !
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op.tobbosdrdj93y5@stone...
>>Hi,

If Form1 and Form2 both reside inside a parent form (MDIParent) have the
parent draw the line. Form1 and Form2 needs to be MDIChildren. The
parent can then subscribe to the Move event, SizeChanged event or any
other event that may cause a need to redraw the line.

On Sun, 25 Feb 2007 16:19:04 +0100, zhaow <nu*****@hotmail.comwrote:

Hi, All
Greetings!
I want to develop as appllication that requires a line-drawing
function
in the blank area between two forms. I have looked up the MSDN, it says
that
a graphics object need a reference to a control or a form. I guess it
means
that lines can't be draw on blank area between two forms. Can anybody
guarantee this for me? Is there any method can realize this function? I
mainly want to draw a line from a button in form1 to current position
of
the
mouse. The line should keep changing when the mouse is moving and the
mouse
will sure move outside the form1 to the form2.
Thank you in advance for your time and patient.


--
Happy coding!
Morten Wennevik [C# MVP]



--
Happy coding!
Morten Wennevik [C# MVP]

Feb 27 '07 #9

P: n/a
It is not so interesting when the mouse moves as when the forms move. Ihave changed your code to draw a point between the upper left corner ofsubform1 and subform2 based on subform1.Move and subform2.Move.

class MyApplicationContext : ApplicationContext
{

private int formCount;
private Form1 mainform, subform1, subform2;
//private var tvar = new var();
MdiClient client = null;
Point p1 = Point.Empty;
Point p2 = Point.Empty;

public MyApplicationContext()
{
formCount = 0;
// Handle the ApplicationExit event to know when the application is exiting.
mainform = new Form1();
mainform.SetBounds(0, 0, 1280, 800);
mainform.IsMdiContainer=true;
foreach (Control c in mainform.Controls)
{
if (c is MdiClient)
client = (MdiClient)c;
}
client.Paint += new PaintEventHandler(client_Paint);

subform1 = new Form1();
subform1.Closed += new EventHandler(OnFormClosed);
subform1.SetDesktopLocation(50, 100);

//subform1.changeText("Start");
//subform1.buttonShow();
formCount++;
subform2 = new Form1();
subform2.Closed += new EventHandler(OnFormClosed);
subform2.SetDesktopLocation(600,100);
//subform2.changeText("Stop");
//subform2.buttonShow();
formCount++;
subform1.MdiParent = mainform;
subform2.MdiParent = mainform;

// Show both forms.
mainform.Show();
subform1.Move += new EventHandler(Form_Move);
subform2.Move += new EventHandler(Form_Move);
subform1.Show();
subform2.Show();
}
void client_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawLine(Pens.Red, p1, p2);
}

void Form_Move(object sender, EventArgs e)
{
p1 = subform1.Location;
p2 = subform2.Location;
client.Invalidate();
}

private void OnFormClosed(object sender, EventArgs e)
{
// When a form is closed, decrement the count of open forms..

// When the count gets to 0, exit the app by calling
// ExitThread().
formCount--;
if (formCount == 0)
{
//tvar.close();
ExitThread();
}
}
}
--
Happy coding!
Morten Wennevik [C# MVP]
Feb 28 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.