WinForm无边框窗体依据任务栏位置最大化

任务栏位置可能在上下左右,而无边框窗体最大化的时候却默认坐标为0,0.下面是解决办法:

#region 最大化处理
/*任务栏位置*/
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SystemParametersInfo(int uAction, int uParam, ref RECT re, int fuWinTni);
[System.Runtime.InteropServices.DllImport("SHELL32", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
public static extern uint SHAppBarMessage(int dwMessage, ref APPBARDATA pData);
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct APPBARDATA
{
public int cbSize;
public IntPtr hWnd;
public int uCallbackMessage;
public int uEdge;//属性代表上、下、左、右
public RECT rc;
public IntPtr lParam;
}
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
public override string ToString()
{
return "{left=" + left.ToString() + ", " + "top=" + top.ToString() + ", " +
"right=" + right.ToString() + ", " + "bottom=" + bottom.ToString() + "}";
}
}
/*窗体坐标*/
private const long WM_GETMINMAXINFO = 0x24;
private struct POINTAPI
{
public int x;
public int y;
}
private struct MINMAXINFO
{
public POINTAPI ptReserved;
public POINTAPI ptMaxSize;
public POINTAPI ptMaxPosition;
public POINTAPI ptMinTrackSize;
public POINTAPI ptMaxTrackSize;
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_GETMINMAXINFO)
{
this.MaximumSize = SystemInformation.WorkingArea.Size;
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;
}
//-------------------------
int aaa = 0x00000005;
APPBARDATA pdat = new APPBARDATA();
SHAppBarMessage(aaa, ref pdat);

if (pdat.uEdge == 0) //左
{
mmi.ptMaxPosition.x = Screen.PrimaryScreen.Bounds.Width - SystemInformation.WorkingArea.Width;
mmi.ptMaxPosition.y = 0;
}
else if (pdat.uEdge == 1) //上
{
mmi.ptMaxPosition.x = 0;
mmi.ptMaxPosition.y = Screen.PrimaryScreen.Bounds.Height - SystemInformation.WorkingArea.Height;
}
else if (pdat.uEdge == 2) //右
{
mmi.ptMaxPosition.x = 0;
mmi.ptMaxPosition.y =0;
}
else if (pdat.uEdge == 3) //下
{
mmi.ptMaxPosition.x = 0;
mmi.ptMaxPosition.y = 0;
}

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

posted @ 2016-08-19 10:13  lihoo  阅读(532)  评论(0编辑  收藏  举报