Clicking on title bar in an mdichild form c#

I have a mdiparent with mdichild forms within it.

I found some code in this forum to help me handle when a form title bar is clicked (Reference A).

The mdiparent dynamically loads various forms (possible up to 100) and assigns them as mdi childs and manually assigning Reference A code in each would be inconvient.

I want to assign the code to each form when they load somehow of the mdichild, so they can detect when the titlebar is clicked.

How can achieve this.

Thanks for any assistance.


Reference A:
        protected override void WndProc(ref Message m)
        {
            const Int32 WM_NCLBUTTONCLK = 161;
            const Int32 WM_NCLBUTTONDBLCLK = 163;
            if (m.Msg == WM_NCLBUTTONCLK)
            {
                TitleBarClicked(); // Implement this function and do what you need to do in this function
            }
            if (m.Msg == WM_NCLBUTTONDBLCLK)
            {
                 TitlebarDoubleClicked(); // Implement this function and do what you need to do in this function
            }
            base.WndProc(ref m);
        }

(Credited to k_swapnil)


ray_codeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonMiddle School Assistant TeacherCommented:
Do TitleBarClicked() and TitlebarDoubleClicked() need to run in the MdiParent or the MdiChild?  What do those methods need to do?
0
ray_codeAuthor Commented:
Mdichild so , basically when they click on the mdichild title bar, I can handle those whatever methods.

I only need the WM_NCLBUTTONCLK singleclick.

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
(not sure how this saves you any coding if every MdiChild will have its own version of this method)

Here's one approach...Form1 is the MdiParent and Form2 is the MdiChild.

Note that Form2 has a PUBLIC method called "TitleBarClicked":

    public partial class Form2 : Form
    {

        public Form2()
        {
            InitializeComponent();
        }

        public void TitleBarClicked()
        {
            this.Text = "TitlebarClicked";
        }

    }

Here's the MdiParent (Form1) code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.MdiParent = this;
            f2.Show();

            TitleBarClicker tbc = new TitleBarClicker(f2);
            tbc.TitleBarClicked += f2.TitleBarClicked;
            f2.Tag = tbc; // keep a reference somewhere
        }

    }

    public class TitleBarClicker : NativeWindow
    {

        public delegate void TitleBarClick();
        public event TitleBarClick TitleBarClicked;
        private const Int32 WM_NCLBUTTONDOWN = 0xA1;

        public TitleBarClicker(Form frm)
        {
            if (frm != null)
            {
                base.AssignHandle(frm.Handle);
            }
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_NCLBUTTONDOWN:
                    TitleBarClicked();           
                    break;

            }
            base.WndProc(ref m);
        }

    }

}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
Can you get away with just knowing when the active MdiChild has changed?

The MdiChild (Form2):

    public partial class Form2 : Form
    {
        private int counter = 0;

        public Form2()
        {
            InitializeComponent();
        }

        public void MdiChildActivated()
        {
            counter += 1;
            this.Text = "MdiChildActivated: " + counter.ToString();
        }

    }

The MdiParent (Form1):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.MdiParent = this;
            f2.Show();
        }

        private void Form1_MdiChildActivate(object sender, EventArgs e)
        {
            Form frm = this.ActiveMdiChild;
            if (frm != null)
            {
                System.Reflection.MethodInfo mi = frm.GetType().GetMethod("MdiChildActivated");
                if (mi != null)
                {
                    mi.Invoke(frm, new Object[] { });
                }
                else
                {
                    Console.WriteLine("MdiChildActivated() not found in MdiChild Form");
                }
            }
        }

    }

}

Open in new window

0
ray_codeAuthor Commented:
Thanks Idle Mind you demonstrated what I wanted to achieve.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.