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