使用API​​隐藏程序标题栏

ped*_*des 5 c# winapi console-application

它可以使用 c# 和 windows api 删除窗口控制台标题栏,如果是的话如何?请。

sta*_*son 4

这个简单的应用程序隐藏并显示其所在控制台的标题栏。它会立即将控制台标题更改为 guid 以查找窗口句柄。然后,它使用 ToggleTitleBar 使用找到的句柄来显示或隐藏。

public class Program
{
    public static void ToggleTitleBar(long hwnd, bool showTitle)
    {
        long style = GetWindowLong(hwnd, -16L);
        if (showTitle)
            style |= 0xc00000L;
        else
            style &= -12582913L;
        SetWindowLong(hwnd, -16L, style);
        SetWindowPos(hwnd, 0L, 0L, 0L, 0L, 0L, 0x27L);
    }

    public static void Main()
    {
        Guid guid = Guid.NewGuid();
        string oldTitle = Console.Title;
        Console.Title = guid.ToString();
        int hwnd = FindWindow("ConsoleWindowClass", guid.ToString());
        Console.Title = oldTitle;

        Console.WriteLine("Press enter to hide title");
        Console.ReadLine();
        ToggleTitleBar(hwnd, false);
        Console.WriteLine("Press enter to show title");
        Console.ReadLine();
        ToggleTitleBar(hwnd, true);
        Console.WriteLine("Press enter to exit");
        Console.ReadLine();
    }

    [DllImport("user32", EntryPoint = "GetWindowLongA")]
    public static extern long GetWindowLong(long hwnd, long nIndex);

    [DllImport("user32", EntryPoint = "SetWindowLongA")]
    public static extern long SetWindowLong(long hwnd, long nIndex, long dwNewLong);

    [DllImport("user32")]
    public static extern long SetWindowPos(long hwnd, long hWndInsertAfter, long x, long y, long cx, long cy,
                                           long wFlags);

    [DllImport("User32.dll")]
    public static extern int FindWindow(string lpClassName, string lpWindowName);
}
Run Code Online (Sandbox Code Playgroud)