Alex RD
Alex RD

Reputation: 1

Why doesn't this c# function properly hide the titlebar?

The function does not hide the titlebar, instead it only hides the minimize/maximize/close buttons.

I'm trying to make the window completely borderless, so I can make an application that darkens everything around a game running in windowed mode.

These are the constants:

const uint SWP_FRAMECHANGED = 0x0020;
const uint SWP_NOMOVE = 0x0002;
const uint SWP_NOSIZE = 0x0001;
const uint SWP_NOZORDER = 0x0004;

const int GWL_STYLE = -16;
const long WS_BORDER = 0x00800000L;
const long WS_CAPTION = 0x00C00000L;
const long WS_SYSMENU = 0x00080000L;

This is the function:

IntPtr hWnd = CurrentWindowID;

long currentStyle = GetWindowLong(hWnd, GWL_STYLE);

// Remove WS_BORDER, WS_CAPTION, and WS_SYSMENU styles to make the window borderless
long newStyle = currentStyle & ~(WS_BORDER | WS_CAPTION | WS_SYSMENU);

// Update the window style
SetWindowLong(hWnd, GWL_STYLE, newStyle);

// Update the window position and size to apply the style change
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);

Upvotes: -2

Views: 88

Answers (1)

Mahdi Jamal
Mahdi Jamal

Reputation: 33

The following code can completely remove the TitleBar

public class WinTools
{
    private const int GWL_STYLE = -16;
    private const int WS_CAPTION = 0x00C00000; // Window with a title bar
    private const int SWP_NOMOVE = 0x0002;
    private const int SWP_NOSIZE = 0x0001;
    private const int SWP_FRAMECHANGED = 0x0020;

    public static void HideWindowTitleBarByHandle(IntPtr hWnd)
    {
        int style = GetWindowLong(hWnd, GWL_STYLE);
        SetWindowLong(hWnd, GWL_STYLE, style & ~WS_CAPTION);
        SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
    }

    [DllImport("user32.dll")]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll")]
    private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
}

enter image description here

To

enter image description here

Upvotes: 0

Related Questions