Application stuck in full screen?

前端 未结 4 1838
醉酒成梦
醉酒成梦 2020-12-08 17:01

To reproduce my problem please do the following:

  1. Create a new Windows Form Application in C#.
  2. In the Properties window of Form1 set FormBorderSt
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-08 17:35

    Check this solution - it removes Maximize/Minimize/Titlebar/Border by API calls.

    public partial class Form1 : Form
    {
        // import necessary API functions to get and set Windows styles for P/Invoke
        [DllImport("user32.dll")]
        internal extern static int SetWindowLong(IntPtr hwnd, int index, int value);
    
        [DllImport("user32.dll")]
        internal extern static int GetWindowLong(IntPtr hwnd, int index);
    
        // define constants like they are named in SDK in order to make source more readable
        const int GWL_STYLE = -16;
        const int GWL_EXSTYLE = -20;
        const int WS_MINIMIZEBOX = 0x00020000;
        const int WS_MAXIMIZEBOX = 0x00010000;
        const int WS_CAPTION = 0x00C00000;
        const int WS_THICKFRAME = 0x00040000;
        const int WS_EX_DLGMODALFRAME = 0x00000001;
        const int WS_EX_CLIENTEDGE = 0x00000200;
        const int WS_EX_STATICEDGE = 0x00020000;
    
        // this replaces MinimizeBox=false and MaximizeBox=false
        void HideMinimizeAndMaximizeButtons()
        {
            // read current style
            int style = GetWindowLong(Handle, GWL_STYLE);
            Debug.WriteLine("0x{0:X}", style);
            // update style - remove flags for MinimizeBox and MaximizeBox
            style = style & ~WS_MINIMIZEBOX & ~WS_MAXIMIZEBOX;
            Debug.WriteLine("0x{0:X}", style);
            SetWindowLong(Handle, GWL_STYLE, style);
        }
    
        // part of removing the whole border
        void HideTitleBar()
        {
            // read current style
            int style = GetWindowLong(Handle, GWL_STYLE);
            Debug.WriteLine("0x{0:X}", style);
            // update style - remove flag for caption
            style = style & ~WS_CAPTION;
            Debug.WriteLine("0x{0:X}", style);
            SetWindowLong(Handle, GWL_STYLE, style);
        }
    
        // hide the border
        void HideBorder()
        {
            // read current style
            int style = GetWindowLong(Handle, GWL_STYLE);
            Debug.WriteLine("0x{0:X}", style);
            // update style - remove flag for border (could use WS_SIZEBOX which is the very same flag (see MSDN)
            style = style & ~WS_THICKFRAME;
            Debug.WriteLine("0x{0:X}", style);
            SetWindowLong(Handle, GWL_STYLE, style);
    
            // read current extended style
            style = GetWindowLong(Handle, GWL_EXSTYLE);
            Debug.WriteLine("0x{0:X}", style);
            // update style by removing some additional border styles -
            // may not be necessary, when current border style is not something exotic,
            // i.e. as long as it "normal"
            style = style & ~WS_EX_DLGMODALFRAME & ~WS_EX_CLIENTEDGE & ~WS_EX_STATICEDGE;
            Debug.WriteLine("0x{0:X}", style);
            SetWindowLong(Handle, GWL_EXSTYLE, style);
        }
    
        public Form1()
        {
            InitializeComponent();
    
            // hide those unwanted properties - you can try to leave out one or another to see what it does
            HideMinimizeAndMaximizeButtons();
            HideTitleBar();
            HideBorder();
        }
    }
    

    This works as intended. Maximizing/minimizing by setting WindowState works as well.

    One could analyze in sources what the framework does and what it does "wrong" (or not quite correct).

    Edit: I added debug output of the style values. Please try this sequence of commands in Form1 constructor:

    MaximizeBox = false;
    FormBorderStyle = FormBorderStyle.Sizable;
    HideMinimizeAndMaximizeButtons();
    FormBorderStyle = FormBorderStyle.None;
    MaximizeBox = true;
    MaximizeBox = false;
    HideMinimizeAndMaximizeButtons();
    FormBorderStyle = FormBorderStyle.None;
    HideMinimizeAndMaximizeButtons();
    

    You'll see, that setting FormBorderStyle.None enables the WS_MAXIMIZEBOX style. This cannot be "corrected" by another MaximizeBox = false. It seems it's necessary to call API functions.

提交回复
热议问题