Application stuck in full screen?

前端 未结 4 1846
醉酒成梦
醉酒成梦 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:58

    Windows does this by calling SetWindowPos() to change the position and size of the window. A window can be notified about this by listening for the WM_WINDOWPOSCHANGING message and override the settings. Lots of things you can do, like still giving the operation a meaning by adjusting the size and position to your liking. You completely prevent it by turning on the NOSIZE and NOMOVE flags.

    Paste this code into your form:

        private bool AllowWindowChange;
    
        private struct WINDOWPOS {
            public IntPtr hwnd, hwndInsertAfter;
            public int x, y, cx, cy;
            public int flags;
        }
    
        protected override void WndProc(ref Message m) {
            // Trap WM_WINDOWPOSCHANGING
            if (m.Msg == 0x46 && !AllowWindowChange) {
                var wpos = (WINDOWPOS)System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, typeof(WINDOWPOS));
                wpos.flags |= 0x03; // Turn on SWP_NOSIZE | SWP_NOMOVE
                System.Runtime.InteropServices.Marshal.StructureToPtr(wpos, m.LParam, false);
            }
            base.WndProc(ref m);
        }
    

    When you want to change the window yourself, simply set the AllowWindowChange field temporarily to true.

提交回复
热议问题