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

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

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

编辑:对不起,我看到你正在寻找一个控制台应用程序的解决方案。 不,我不知道要做你想做的事情。 在WinForms应用程序中托管控制台也不是一件容易的事。

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

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

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

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

 this.ShowInTaskBar = false; 

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

  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(拒绝访问)。

我有一些(非常)旧的代码我认为有点相关; 我想在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); } 

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