M
Martin.Hallerdal
Hello,
I have a form that I don't want to display the title bar for. The way
I've implemented this is to to remove the WS_DLGFRAME style flag.
However, when this form is minimized and then restored, it is restored
to the wrong size. The height of the form increases 19 pixels (height
of a title bar on my computer) with every minimize/restore.
Can someone shead any light on this?
I've attached an example program. To reproduce, just create a standard
windows forms project, paste the code, and add a button to the form
whose click handler you connect to the button1_Click method.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Attach this to a button click event for a button on the form
private void button1_Click(object sender, EventArgs e)
{
if (IsStyleFlagSet(this.Handle, WS_DLGFRAME))
{
// Remove caption bar
SetStyleFlag(this.Handle, WS_DLGFRAME , false);
// Repaint NC area
SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
}
// Get my current client size
Size prevSize = this.ClientSize;
// Minimize and restore me
this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Normal;
// Get my client size now
Size currSize = this.ClientSize;
if (currSize == prevSize)
MessageBox.Show("Size was equal! ");
else
MessageBox.Show(string.Format("Size was not equal! :-(. Before:
{0}, After: {1}", prevSize, currSize));
}
// Style constants
internal const int GWL_STYLE = (-16);
internal const int WS_DLGFRAME = 0x00400000;
internal const int WS_BORDER = 0x00800000;
internal const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;
// SetWindowPos Flags
internal const int SWP_NOSIZE = 0x0001;
internal const int SWP_NOMOVE = 0x0002;
internal const int SWP_NOZORDER = 0x0004;
internal const int SWP_NOACTIVATE = 0x0010;
internal const int SWP_FRAMECHANGED = 0x0020;
internal const int SWP_NOOWNERZORDER = 0x0200;
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
internal static extern int SetWindowPos(IntPtr hWnd, IntPtr
hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
internal static bool IsStyleFlagSet(IntPtr handle, int styleFlag)
{
return (GetWindowLong(handle, GWL_STYLE) & styleFlag) == styleFlag;
}
internal static void SetStyleFlag(IntPtr handle, int dwNewLong, bool
set)
{
int style = GetWindowLong(handle, GWL_STYLE);
if (set)
style |= dwNewLong;
else
style &= ~dwNewLong;
SetWindowLong(handle, GWL_STYLE, style);
}
}
I have a form that I don't want to display the title bar for. The way
I've implemented this is to to remove the WS_DLGFRAME style flag.
However, when this form is minimized and then restored, it is restored
to the wrong size. The height of the form increases 19 pixels (height
of a title bar on my computer) with every minimize/restore.
Can someone shead any light on this?
I've attached an example program. To reproduce, just create a standard
windows forms project, paste the code, and add a button to the form
whose click handler you connect to the button1_Click method.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Attach this to a button click event for a button on the form
private void button1_Click(object sender, EventArgs e)
{
if (IsStyleFlagSet(this.Handle, WS_DLGFRAME))
{
// Remove caption bar
SetStyleFlag(this.Handle, WS_DLGFRAME , false);
// Repaint NC area
SetWindowPos(this.Handle, IntPtr.Zero, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER);
}
// Get my current client size
Size prevSize = this.ClientSize;
// Minimize and restore me
this.WindowState = FormWindowState.Minimized;
this.WindowState = FormWindowState.Normal;
// Get my client size now
Size currSize = this.ClientSize;
if (currSize == prevSize)
MessageBox.Show("Size was equal! ");
else
MessageBox.Show(string.Format("Size was not equal! :-(. Before:
{0}, After: {1}", prevSize, currSize));
}
// Style constants
internal const int GWL_STYLE = (-16);
internal const int WS_DLGFRAME = 0x00400000;
internal const int WS_BORDER = 0x00800000;
internal const int WS_CAPTION = WS_BORDER | WS_DLGFRAME;
// SetWindowPos Flags
internal const int SWP_NOSIZE = 0x0001;
internal const int SWP_NOMOVE = 0x0002;
internal const int SWP_NOZORDER = 0x0004;
internal const int SWP_NOACTIVATE = 0x0010;
internal const int SWP_FRAMECHANGED = 0x0020;
internal const int SWP_NOOWNERZORDER = 0x0200;
[DllImport("user32.dll")]
static extern int SetWindowLong(IntPtr hWnd, int nIndex, int
dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32")]
internal static extern int SetWindowPos(IntPtr hWnd, IntPtr
hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
internal static bool IsStyleFlagSet(IntPtr handle, int styleFlag)
{
return (GetWindowLong(handle, GWL_STYLE) & styleFlag) == styleFlag;
}
internal static void SetStyleFlag(IntPtr handle, int dwNewLong, bool
set)
{
int style = GetWindowLong(handle, GWL_STYLE);
if (set)
style |= dwNewLong;
else
style &= ~dwNewLong;
SetWindowLong(handle, GWL_STYLE, style);
}
}