Click here to Skip to main content
15,311,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do i show title bar when mouse over it and hide when mouse away from it in windows form C# application.
Posted

I'll show you one way to do this, but, I think this is a rather ugly hack, and I recommend you don't do this :) I think a much better strategy, more consistent with Windows Program Guidelines, is to implement some way for the User to choose whether a form displays its TitleBar or hides it: Context Menu ? Button ? Dropdown Choice ?, etc.

You do realize that if you hide the TitleBar, you will also hide any border around the right, left, bottom, sides of the Form ?
C#
using System;
using System.Drawing;
using System.Windows.Forms;

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

            shownText = this.Text;
            shownFormBorderStyle = this.FormBorderStyle;
        }

        private Rectangle FormBounds;

        private bool TitleBarShowing = true;
        private bool LockMethods = false;

        private FormBorderStyle shownFormBorderStyle = FormBorderStyle.Sizable;
        private FormBorderStyle hiddenFormBorderStyle = FormBorderStyle.FixedToolWindow;
        
        private string shownText;

        private void Form1_Load(object sender, EventArgs e)
        {
            FormBounds = this.Bounds;
        }

        private void UpdateTitleBarVisibility(bool isvisible)
        {
            if (LockMethods) return;

            if (isvisible)
            {
                if (!TitleBarShowing)
                {
                    // see explanation in comment #1
                    if (! FormBounds.Contains(MousePosition)) return;

                    TitleBarShowing = true;

                    LockMethods = true;

                    this.SuspendLayout();
                        this.Text = shownText;
                        this.ControlBox = true;
                        this.FormBorderStyle = shownFormBorderStyle;
                    this.ResumeLayout();

                    // see explanation in comment #2
                    Application.DoEvents();
                    this.Visible = true;
                    this.BringToFront();
                    this.Refresh();

                    LockMethods = false;
                }
            }
            else if (TitleBarShowing)
            {
                if (FormBounds.Contains(MousePosition)) return;

                TitleBarShowing = false;

                LockMethods = true;

                this.SuspendLayout();
                    this.Text = "";
                    this.ControlBox = false;
                    this.FormBorderStyle = hiddenFormBorderStyle;
                this.ResumeLayout();

                LockMethods = false;
            }
        }

        private void Form1_Enter(object sender, EventArgs e)
        {
            UpdateTitleBarVisibility(true);
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            UpdateTitleBarVisibility(true);
        }

        private void Form1_MouseEnter(object sender, EventArgs e)
        {
            UpdateTitleBarVisibility(true);
        }

        private void Form1_Leave(object sender, EventArgs e)
        {
            UpdateTitleBarVisibility(false);
        }

        private void Form1_MouseLeave(object sender, EventArgs e)
        {
            UpdateTitleBarVisibility(false);
        }
    }
}
Comments:

1. A Form's Bounds property ... assuming it is "placed" on the Screen (which is where it should be placed) ... gives a rectangle in screen co-ordinates which includes non-client areas, including TitleBar and Form Border. By intercepting a move in that area, and preventing an update Event, we can (hopefully) reduce flashing.

2. The use of Application.DoEvents, Visible = true, Refresh, BringToFront here is a work-around for a "quirk" in WinForms : without this "incantation," the Form may disappear in the case the Form has lost focus with TitleBar area hidden and you attempt to restore the TitleBar by moving the Mouse onto the visible Form. You want to know "why ?:" ask Microsoft :)

3. Summary: a hackish technique is demonstrated which relies on the fact that you can move your mouse outside the Form's boundaries faster than the Form's move detection handler can catch. Use at your own risk.
   
v4
Comments
Shambhoo kumar 29-May-15 9:42am
   
sound like expert. :) thanks for these kind of solution.
You have to use the MouseMove-Event from your Form.
If the MouseCursor Comes in the Region where you want to show your "Titlebar" you make it visible or give it a location in your form.

To make it unvisible you use the MouseLeave-Event from your Titelbar.
   
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