visual-studiowinforms

Taskbar is overlapping the maximized window form #c#


I need to keep my maximized window on top of the taskbar keeping the entire window above the taskbar.

I have tried adding,

this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
this.MaximumSize = Screen.PrimaryScreen.WorkingArea.Size;

Also tried changing TopMost to true. But it ended up showing the form behind the task bar as well like this.

enter image description here

Could you please help me?


Solution

  • According to your situation, I have conducted a test. I created a StatusStrip control at the bottom of the form and added a StatusLabel in it to check whether the form is maximized and at the top of the taskbar after running: enter image description here

    The code examples used in the test are as follows:

    using System;
    using System.Drawing;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                // Maximize the window when the form loads
                this.WindowState = FormWindowState.Maximized;
                // Set the borders of the maximized window to avoid covering the taskbar
                this.MaximizedBounds = Screen.FromHandle(this.Handle).WorkingArea;
            }
    
            protected override void WndProc(ref Message m)
            {
                const int WM_GETMINMAXINFO = 0x0024;
    
                if (m.Msg == WM_GETMINMAXINFO)
                {
                    IntPtr monitor = MonitorFromWindow(this.Handle, MONITOR_DEFAULTTONEAREST);
    
                    if (monitor != IntPtr.Zero)
                    {
                        // Get the MINMAXINFO structure
                        var mmi = Marshal.PtrToStructure<MINMAXINFO>(m.LParam);
    
                        // Get monitor information
                        var monitorInfo = new MONITORINFO { cbSize = Marshal.SizeOf<MONITORINFO>() };
                        GetMonitorInfo(monitor, ref monitorInfo);
    
                        var rcWorkArea = monitorInfo.rcWork;
                        var rcMonitorArea = monitorInfo.rcMonitor;
    
                        // Set the size and position of the maximized window to avoid covering the taskbar
                        mmi.ptMaxPosition.x = rcWorkArea.Left - rcMonitorArea.Left;
                        mmi.ptMaxPosition.y = rcWorkArea.Top - rcMonitorArea.Top;
                        mmi.ptMaxSize.x = rcWorkArea.Width;
                        mmi.ptMaxSize.y = rcWorkArea.Height;
    
                        //Copy the modified MINMAXINFO structure back to memory
                        Marshal.StructureToPtr(mmi, m.LParam, true);
                    }
                }
    
                base.WndProc(ref m);
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct POINT
            {
                public int x;
                public int y;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct MINMAXINFO
            {
                public POINT ptReserved;
                public POINT ptMaxSize;
                public POINT ptMaxPosition;
                public POINT ptMinTrackSize;
                public POINT ptMaxTrackSize;
            }
    
            private const int MONITOR_DEFAULTTONEAREST = 0x00000002;
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
    
            [DllImport("user32.dll", SetLastError = true)]
            private static extern IntPtr MonitorFromWindow(IntPtr hwnd, int dwFlags);
    
            [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
            private struct MONITORINFO
            {
                public int cbSize;
                public Rectangle rcMonitor;
                public Rectangle rcWork;
                public int dwFlags;
            }
        }
    }
    

    What it looks like after running: enter image description here

    You can create a new Windows Forms application and use the above code to test it to see if it meets your needs. If applicable, you can modify your code accordingly based on the examples above.