Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a MDI Form as the main form in my application.
I would like to do something when the mouse clicks down on the main grey space in which the child forms appear.

However, when using the MouseDown event handler, the event does not seem to fire.
Is there something simple I'm missing here? Does the 'grey space' count as part of the MDI form?

Code sample below:
C#
public class MyMdiForm : Form
{
   
    public MyMdiForm
    {
       this.MouseDown += new System.Windows.Forms.MouseEventHandler(MyMdiForm_MouseDown);
    }
   
    public void MyMdiForm_MouseDown(object sender, MouseEventArgs e)
    {
        // This should fire when clicking on the grey space?
        // I want to do something here.
    }
}


Thanks in advance for any suggestions.
Posted
Updated 19-Nov-11 22:41pm
v2

I was also surprised to try and find that these events don't fire.

One option might be to dock (and fill) a panel control in the area which would be the workspace background and handle the mouse click on that. Add a panel and simply set ists colour to the default workspace background and you would never know it was there.

I'm sure there must be more elegant solutions, but you would need to dig around on the big G, or see what others come up with.
 
Share this answer
 
Comments
Santoshshrest 20-Nov-11 6:15am    
but this solution does not allow me to do the multiple task...
even though i do, there is still problem i.e, panel blocking the child form.
thank you...
DaveAuld 20-Nov-11 8:53am    
The panel sits behind the child forms.......
Sergey Alexandrovich Kryukov 20-Nov-11 19:13pm    
Oh yes, there are more elegant solutions... :-) Please see mine, for example.
--SA
Sergey Alexandrovich Kryukov 20-Nov-11 19:15pm    
Speaking of MDI: gray background belongs to another window which HWND is not exposed in .NET, called "MDI Client", it is an implicit child of MDIParent and holds all the MDIChildren. So, no wonder the event is not fired, as OP installs it on MDIParent, and "MDI Client" is not exposed. Using it, rendering any graphics on it, modification of its behavior is a known set of the problems. Why bothering?

Again, see my solution. :-)
--SA
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[^] — Wikipedia,
Question on using MDI windows in WPF[^],
MDIContainer giving error[^] — SA,
How to Create MDI Parent Window in WPF?[^] — Wayne Gaylard.

[EDIT]

Please see my comment to the Dave's answer where I try to explain why the event is not fired. Why bothering?

—SA
 
Share this answer
 
v3
Comments
Santoshshrest 21-Nov-11 3:07am    
if not using mdi then what shuld sdi...
what about the main form...
the other form may over lap it..
what can be the solution that using the sdi form we can do multifle task...
mdi can be the solution for this..
any way thanks for the response...
i know the mdi is old interface design.. but every one use this...
i dont have any choice...
its may be i am new to .net
I have found the solution for this event.... but it carries some error..

C#
protected override void WndProc(ref Message m)
       {
           Screen Src = Screen.PrimaryScreen;
           if ( m.Msg == (int)WM_LBUTTONDOWN)
           {
               int x = m.LParam.ToInt32() & Src.Bounds.Width;
               int y = m.LParam.ToInt32() & Src.Bounds.Height;
               OnMDIMouseLBtnDown(new MouseEventArgs(MouseButtons.Left, 1, x, y, 0));
               OnMDIMouseLBtnDown(new MouseEventArgs(System.Windows.Forms.MouseButtons.Left, 1, x, y, 0));
           }
           else if (m.Msg == (int)WM_NCMOUSEMOVE || m.Msg == WM_MOUSEMOVE)
           {
               int x = m.LParam.ToInt32() & Src.WorkingArea.X;
               int y = m.LParam.ToInt32() & Src.WorkingArea.Y;
               OnMDIMouseMove(new MouseEventArgs(MouseButtons.None, 0, m.LParam.ToInt32(), m.LParam.ToInt32(), 0));
           }
           base.WndProc(ref m);
       }

       protected virtual void OnMDIMouseLBtnDown(MouseEventArgs e)
       {
           if (e.X <=250 && e.Y >=200)
           {
               MessageBox.Show("X: " + (e.X).ToString() + Environment.NewLine +
                              "Y: " + (e.Y).ToString() + Environment.NewLine);
           }
       }

       protected virtual void OnMDIMouseMove(MouseEventArgs e)
       {
           MessageBox.Show("X: " + (e.X).ToString() + Environment.NewLine +
                           "Y: " + (e.Y).ToString());
       }


but mdi form does not notice wm_mousemove...
it only detects the wm_mouseactivate,
what can be the solution..
moreover i am unable to find the accurate mouse location...
 
Share this answer
 
v2

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