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 ?
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)
{
if (! FormBounds.Contains(MousePosition)) return;
TitleBarShowing = true;
LockMethods = true;
this.SuspendLayout();
this.Text = shownText;
this.ControlBox = true;
this.FormBorderStyle = shownFormBorderStyle;
this.ResumeLayout();
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.