窗体最大化的时候,如何指定窗体的位置、大小(C#)


窗体最大化的时候,如何指定窗体的位置、大小(C#)


(1)使用窗体默认的最大化方法
this.WindowState = FormWindowState.Maximized 发现窗体的位置x和y都是小于零的。设置了 this.Location = new Point(1, 1);后仍然无效。。。



(2)设置了窗体的最大值后,测试发现有时候(窗体宽度接近屏幕的宽度的时候),窗体的高度的设置是无效的。
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);

郁闷啊。后来在MSND上找到了
http://support.microsoft.com/?scid=kb%3Ben-us%3B327824&x=10&y=14  这才算解决了。




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    
public partial class FormRegion : Form
    
{
        
private const long WM_GETMINMAXINFO = 0x24;

        
public struct POINTAPI
        
{
            
public int x;
            
public int y;
        }


        
public struct MINMAXINFO
        
{
            
public POINTAPI ptReserved;
            
public POINTAPI ptMaxSize;
            
public POINTAPI ptMaxPosition;
            
public POINTAPI ptMinTrackSize;
            
public POINTAPI ptMaxTrackSize;
        }

        
public FormRegion()
        
{
            InitializeComponent();
            
this.MaximumSize = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
        }



        
protected override void WndProc(ref System.Windows.Forms.Message m)
        
{
            
base.WndProc(ref m);
            
if (m.Msg == 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 
= 1;
                mmi.ptMaxPosition.y 
= 1;

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

        }

    }

}


原文地址:https://www.cnblogs.com/adandelion/p/1136198.html