开发者

Hide Title Bar of Program using API

Its possible remove a window console title b开发者_开发问答ar using c# and windows api, if yes howto? Please.


This simple app hides and shows the title bar of the console that it's in. It changes the console title to a guid momentarily to find the window handle. Afterwards it uses ToggleTitleBar to show or hide using the found handle.

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);
}


Edit: Sorry, I see that you're looking for a solution for a console application. No, there is no way I know of to do what you're trying to do. It is also not simple to host a console in a WinForms application.

However, if you ARE using a WinForms application or WPF, consider the following.

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

Otherwise, you could set FormBorderStyle to None.

You can also hide the program from the task bar if you need to.

this.ShowInTaskBar = false;


This will probably not work. In theory you could use something like this:

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

This will work for every window except console windows. SetWindowLong returns 0, and GetLastError returns 5 (Access denied), even if you run the application as administrator.


I have some (very) old code I think somehow related; I'd to display Microsoft Excel inside a winform application:

[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);
}


use FindWindow to get the handle of the console window then SetWindowLong to modify his properties

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜