【问题标题】:Hide Title Bar of Program using API使用 API 隐藏程序的标题栏
【发布时间】:2011-01-02 02:37:59
【问题描述】:

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

【问题讨论】:

  • 既然您将其标记为 c#,并且您显然在 Windows 中遇到了问题,为什么不直接将其设为 Windows 窗体或 WPF 应用程序并使用已经提供的解决方案之一呢?我看不出它必须是控制台应用程序的任何原因。或者有吗?

标签: c# winapi console-application


【解决方案1】:

这个简单的应用程序隐藏和显示它所在控制台的标题栏。它会立即将控制台标题更改为 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);
}

【讨论】:

    【解决方案2】:

    编辑:抱歉,我看到您正在寻找控制台应用程序的解决方案。不,我不知道做你想做的事。在 WinForms 应用程序中托管控制台也并不简单。

    但是,如果您使用的是 WinForms 应用程序或 WPF,请考虑以下事项。

    this.ControlBox = false;
    this.Text = string.Empty;
    

    否则,您可以将FormBorderStyle 设置为None

    如果需要,您还可以在任务栏中隐藏程序。

    this.ShowInTaskBar = false;
    

    【讨论】:

      【解决方案3】:

      这可能行不通。理论上你可以使用这样的东西:

          HWND handle = FindWindow(L"ConsoleWindowClass", NULL);
          LONG style = GetWindowLong(handle, GWL_STYLE);
          style = style & ~WS_CAPTION;
          SetWindowLong(handle, GWL_STYLE, style);
      

      这将适用于除控制台窗口之外的每个窗口。 SetWindowLong 返回 0,GetLastError 返回 5(拒绝访问),即使您以管理员身份运行应用程序也是如此。

      【讨论】:

        【解决方案4】:

        我有一些(非常)旧的代码,我认为有些相关;我想在 winform 应用程序中显示 Microsoft Excel:

        [DllImport("user32.dll")]
        public static extern int FindWindow(string lpClassName, string lpWindowName);
        
        [DllImport("user32.dll")]
        public static extern int SetParent(int hWndChild, int hWndNewParent);
        
        [DllImport("user32.dll")]
        public static extern int MoveWindow(
            int hWnd, int x, int y, 
            int nWidth, int nHeight, int bRepaint);
        
        //
        private static int hwnExcel = 0;
        private System.Windows.Forms.PictureBox picContainer;
        // ...
        private void Principal_Resize(object sender, EventArgs e)
        {
            picContainer.Width = this.Width - 8;
            picContainer.Height = this.Height - 45;
            User32.SetParent(hwnExcel, 0);
            User32.MoveWindow(
                hwnExcel, 0, 0, 
                picContainer.Width, picContainer.Height, 1);
        }
        

        【讨论】:

          【解决方案5】:

          使用 FindWindow 获取控制台窗口的句柄,然后使用 SetWindowLong 修改他的属性

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-12-08
            • 2021-03-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-01-31
            相关资源
            最近更新 更多