-4

I would like to know how can I get a borderless C#.NET Console Application. My application works fine but I don't want my application to look like a normal form with the minimize, maximize and close buttons and the icon and text in the upperleft.

So, I would like to know how I can achieve this.

horgh
  • 17,918
  • 22
  • 68
  • 123
Dego
  • 1
  • 1
  • 2
  • A picture of what you have and a mock up of what you want, would add 2,000 words to the description. – Mesh Oct 03 '12 at 09:06

4 Answers4

6

You can't and it doesn't even make sense. The console represented by its input and output streams is a system-backed resources which need not be represented by a console window at all. For example, your application's input and output can be redirected.

Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
6

As far as I am aware, you will need to use Win32 to change the appearance of the console window. This would imply DllImport and a lot of complexity that is quite unnecessary given your alternative:

If you recreate your application as a WinForms application, you can set these properties on the main window. Drop then a textbox in the middle, make it dock the window, and you're emulating a console.

Leonard
  • 3,012
  • 2
  • 31
  • 52
3

Let's say you (for some reason) cannot use a borderless WinForm, and you absolutely must use a console window. Well, you can kinda make it work.

Using most of the code from this similar problem over here, we can put together a solution that will work.

Here is an example borderless console window:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ConsoleBorderTest
{
    class Program
    {
        [DllImport("USER32.DLL")]
        public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

        [DllImport("user32.dll")]
        static extern bool DrawMenuBar(IntPtr hWnd);

        [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
        public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);

        [DllImport("user32.dll", SetLastError = true)]
        static extern bool GetWindowRect(IntPtr hwnd, out RECT lpRect);

        [DllImport("user32", ExactSpelling = true, SetLastError = true)]
        internal static extern int MapWindowPoints(IntPtr hWndFrom, IntPtr hWndTo, [In, Out] ref RECT rect, [MarshalAs(UnmanagedType.U4)] int cPoints);

        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetDesktopWindow();

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int left, top, bottom, right;
        }

        private static readonly string WINDOW_NAME = "TestTitle";  //name of the window
        private const int GWL_STYLE = -16;              //hex constant for style changing
        private const int WS_BORDER = 0x00800000;       //window with border
        private const int WS_CAPTION = 0x00C00000;      //window with a title bar
        private const int WS_SYSMENU = 0x00080000;      //window with no borders etc.
        private const int WS_MINIMIZEBOX = 0x00020000;  //window with minimizebox

        static void makeBorderless()
        {
            // Get the handle of self
            IntPtr window = FindWindowByCaption(IntPtr.Zero, WINDOW_NAME);
            RECT rect;
            // Get the rectangle of self (Size)
            GetWindowRect(window, out rect);
            // Get the handle of the desktop
            IntPtr HWND_DESKTOP = GetDesktopWindow();
            // Attempt to get the location of self compared to desktop
            MapWindowPoints(HWND_DESKTOP, window, ref rect, 2);
            // update self
            SetWindowLong(window, GWL_STYLE, WS_SYSMENU);
            // rect.left rect.top should work but they're returning negative values for me. I probably messed up
            SetWindowPos(window, -2, 100, 75, rect.bottom, rect.right, 0x0040);
            DrawMenuBar(window);
        }

        static void Main(string[] args)
        {
            Console.Title = WINDOW_NAME;
            makeBorderless();
            Console.WriteLine("Can you see this?");
            Console.ReadLine();
        }
    }
}

This is pretty much a direct copy from the referenced link, here, and here. I tried getting the location of the form but I could not do it. I did manage to get the size, but the location was returning negative values for me.

Although it's not great, it's a borderless console window. I really recommend just docking a textbox into a normal form. Here's a picture: "You need 10 reputation to post images"...

Community
  • 1
  • 1
AlbinoDrought
  • 986
  • 17
  • 24
0

I would design a Windows Forms application with the desired appearance (borderless) and even black with the console appearance, and then make it movable

URL: Make a borderless form movable?

Community
  • 1
  • 1
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82