C# 设置窗体最大化,以及窗体最大化时的坐标

reference:

https://www.cnblogs.com/adandelion/archive/2008/04/03/1136198.html

        protected override void WndProc(ref Message m)
        {            
            if (m.Msg == (int)WM.WM_GETMINMAXINFO)
            {
                MINMAXINFO mmi = (MINMAXINFO)m.GetLParam(typeof(MINMAXINFO));
                //mmi.ptMinTrackSize.x = this.MinimumSize.Width;
                //mmi.ptMinTrackSize.y = this.MinimumSize.Height;
                //if (this.MaximumSize.Width != 0 || this.MaximumSize.Height != 0)
                //{
                //    mmi.ptMaxTrackSize.x = this.MaximumSize.Width;
                //    mmi.ptMaxTrackSize.y = this.MaximumSize.Height;
                //}
                mmi.ptMaxPosition.x = _location.X;
                mmi.ptMaxPosition.y = _location.Y;

                System.Runtime.InteropServices.Marshal.StructureToPtr(mmi, m.LParam, true);
            }

            base.WndProc(ref m);
        }
        
        private void _UpdateMaxSizeInfo()
        {
            try
            {               
                // 获取当前窗体所在的屏幕
                Screen screen = Screen.FromControl(this);
                // 获取屏幕的工作区大小(排除了任务栏和停靠窗口的空间)
                Rectangle workingArea = screen.WorkingArea;
                //MessageBox.Show($"工作区大小: 宽度={workingArea.Width}, 高度={workingArea.Height}");
                // 获取整个屏幕的大小
                //Rectangle bounds = screen.Bounds;
                //MessageBox.Show($"屏幕总大小: 宽度={bounds.Width}, 高度={bounds.Height}");

                var x = workingArea.Location.X + index * workingArea.Width / count;
                x = index * workingArea.Width / count;
                _location =  new Point(x, 0);
                this.MaximumSize = new Size(workingArea.Width/count, workingArea.Height);
            }
            catch (Exception ex)
            {
                ex.ToErrorObject().Show();                
            }                     
        }
        private void BarControl_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if(this.WindowState==FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                _UpdateMaxSizeInfo();
                this.WindowState = FormWindowState.Maximized;
            }
        }              

注意WndProc中在处理WM_GETMINMAXINFO后,可以继续运行 base.WndProc(ref m)
不会影响,可能系统默认不修改其中的内容

posted on   norsd  阅读(11)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示