Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
How read mouse coordinates from an existing text file and draw line between each of the points(coodinates) onto a child MDI Form? Please help
Posted
Updated 25-Mar-12 5:23am
v3
Comments
[no name] 25-Mar-12 11:16am    
Help with what? Opening a file? Reading a file? Drawing lines? What have you tried?
Tom Novy 25-Mar-12 14:42pm    
private void OnMouseClick(object sender, MouseEventArgs mouseEv)
{

point1 = new Point(mouseEv.X);
point2 = new Point(mouseEv.Y);

System.Drawing.Pen myPen;
myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
System.Drawing.Graphics formGraphics = this.CreateGraphics();

formGraphics.DrawLine(myPen, point1, point2);

myPen.Dispose();
formGraphics.Dispose();
}
Shahin Khorshidnia 25-Mar-12 11:22am    
I didn't fully understand.
How read mouse coordinates from an existing text file?!!
Please explain.
Sergey Alexandrovich Kryukov 25-Mar-12 14:11pm    
System.Windows.Forms? Tag "Forms". MDI is irrelevant.
--SA

1 solution

It has nothing to do with MDE. You can do it with any control, including System.Windows.Form.Form.

First of all, to get mouse coordinates, handle appropriate mouse events; please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx[^].

Alternatively, subclass this class to make your own control class and override appropriate virtual methods like OnMouseUp, OnMouseDown, OnMouseMove. This is better, because most likely, you will need to subclass it to eliminate flicker. Using double buffering feature needs subclassing.

Now, the problem is: where do you draw on? If you try to draw just on the control, it won't work. You will need to draw "on data model". Really. This is something where the beginner have trouble understanding. Graphics rendering in System.Windows.Forms happens not when you put something on the control, but on the windows message WM_PAINT. It is send on control invalidation. As a result, when you draw something on control, then put some other window on top in z order and then show your window again, all your graphics disappears. Therefore, you should do drawing in the handle of the event Paint or, better yes, in the overridden virtual method OnPaint (better, again, because you will most likely subclass the control anyway, and if this is a form, you always subclass).

To process the event probably, you need to keep a data model in the fields of your control, and in the response of the event, re-render it every time, from model to the instance of System.Drawing.Graphics. You should never create this instance, but always take one from the event arguments of the event handler (or OnPaint method). Use the instance for drawing, see:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^].

Every time you change the drawing data (for example, via your mouse events), you should call Graphics.Invalidate.

For further detail, please see my past answers:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^].

Now, to eliminate flicker, you will need to use double buffering through Control.SetStyle. This method is protected. That's why I told above that you will need subclassing anyway (you don't need this with forms as it has a public property Form.DoubleBuffered, but a form is always subclasses, practically!). You will need to add the following styles:
ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer.

That's it.

Now, (sigh) — about MDI.

Here is the idea: who needs MDI, ever? Why torturing yourself and scaring off your users?
Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. I can explain what to do instead.

Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^].

In my past answers:
Question on using MDI windows in WPF[^],
MDIContainer giving error[^].

See also:
How to Create MDI Parent Window in WPF?[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900