Search code examples
c#winformswndproc

Capture mouse touch the title bar of win form


i got some trick to capture mouse touch title bar but the routine fire repeatedly when i place mouse on title bar. here is the routine

protected override void WndProc(ref Message m)
{
            if (m.Msg == 0xA0) // WM_NCMOUSEMOVE
            {
        listBox1.Items.Add("mouse move on title bar");
            }
            else if (m.Msg == 0x2A2) // WM_NCMOUSELEAVE
            {
        listBox1.Items.Add("mouse leave from title bar");            
            }

            base.WndProc(ref m);
}

can any one tell me any trick as a result WndProc fire only once when mouse touch the title bar instead of repeatedly firing. thanks


Solution

  • Are you looking for this?I am controlling addition of items to listbox using flag.

    private bool insideTitleBar = false;
    private bool outsideTitleBar = false;
    
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0xA0) // WM_NCMOUSEMOVE
        {        
               if(!insideTitleBar)
               {
                listBox1.Items.Add("mouse move on title bar");
                insideTitleBar = true;
                outsideTitleBar = false;
               }
        } 
        else if (m.Msg == 0x2A2) // WM_NCMOUSELEAVE
        {
               if(!outsideTitleBar)
               {      
                listBox1.Items.Add("mouse leave from title bar");            
                outsideTitleBar = true;
                insideTitleBar = false;
               }
        }
        base.WndProc(ref m);
    }