Spook
Spook

Reputation: 25927

How to keep border on a fixed form?

I want my Windows Forms form to keep the window border, while having no title bar and being non-resizable (fixed) (similarly to window previews, when one hovers mouse over button on the taskbar):

enter image description here

Setting ControlBox to false and Text to "" removes the title bar and keeps the border as I want to, but the border is visible only if the form is sizeable. When I set the FormBorderStyle to one of the Fixed* styles, the border disappears:

enter image description here

How may I achieve the described behavior?

Upvotes: 1

Views: 1691

Answers (2)

m0sa
m0sa

Reputation: 10940

You can pinvoke SetWindowsLong and adjust window styles:

// run in LINQpad
private const int GWL_STYLE = -16;
private const int WS_SIZEBOX = 0x040000;
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
void Main()
{
    var form = new Form();
    form.ControlBox = false;
    form.FormBorderStyle = FormBorderStyle.FixedDialog;
    form.Show();
    SetWindowLong(form.Handle, GWL_STYLE, GetWindowLong(form.Handle, GWL_STYLE) | WS_SIZEBOX);
}

After that you would have to prevent resizing manually though.

Upvotes: 1

ZeroPhase
ZeroPhase

Reputation: 647

I just played around with a project of mine and set FormBorderStyle to FixedSingle through the Design view, and the window seems to keep the border for Windows 8. I initially had text in the title, which was forcing the border to render. I removed the text and the border no longer rendered, so as a hacky solution I just input an empty string, by hitting backspace a few times. This made the border show up and remain fixed.

Upvotes: 0

Related Questions