C# Can';t由于停靠面板,无法从顶部调整无边界winform的大小

C# Can';t由于停靠面板,无法从顶部调整无边界winform的大小,c#,winforms,C#,Winforms,我制作了一个无边框的窗口窗体,添加了阴影,可以拖动,可以从任何角度调整大小,下面是我所做的,有点混乱: using System.Runtime.InteropServices; public class MAIN { [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")] private static extern IntPtr CreateRoundRectRgn ( int nLeft

我制作了一个无边框的窗口窗体,添加了阴影,可以拖动,可以从任何角度调整大小,下面是我所做的,有点混乱:

using System.Runtime.InteropServices;

public class MAIN
{
    [DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
    private static extern IntPtr CreateRoundRectRgn
    (
        int nLeftRect, // x-coordinate of upper-left corner
        int nTopRect, // y-coordinate of upper-left corner
        int nRightRect, // x-coordinate of lower-right corner
        int nBottomRect, // y-coordinate of lower-right corner
        int nWidthEllipse, // height of ellipse
        int nHeightEllipse // width of ellipse
    );
    [DllImport("dwmapi.dll")]
    public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset);
    [DllImport("dwmapi.dll")]
    public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
    [DllImport("dwmapi.dll")]
    public static extern int DwmIsCompositionEnabled(ref int pfEnabled);
    private bool m_aeroEnabled;                     // variables for box shadow
    private const int CS_DROPSHADOW = 0x00020000;
    private const int WM_NCPAINT = 0x0085;
    private const int WM_ACTIVATEAPP = 0x001C;
    public struct MARGINS                           // struct for box shadow
    {
        public int leftWidth;
        public int rightWidth;
        public int topHeight;
        public int bottomHeight;
    }
    private const int WM_NCHITTEST = 0x84;          // variables for dragging the form
    private const int HTCLIENT = 0x1;
    private const int HTCAPTION = 0x2;
    protected override CreateParams CreateParams
    {
        get
        {
            m_aeroEnabled = CheckAeroEnabled();

            CreateParams cp = base.CreateParams;
            if (!m_aeroEnabled)
                cp.ClassStyle |= CS_DROPSHADOW;

            return cp;
        }
    }
    private bool CheckAeroEnabled()
    {
        if (Environment.OSVersion.Version.Major >= 6)
        {
            int enabled = 0;
            DwmIsCompositionEnabled(ref enabled);
            return (enabled == 1) ? true : false;
        }
        return false;
    }
    protected override void WndProc(ref Message m)
    {
        const int wmNcHitTest = 0x84;
        const int htLeft = 10;
        const int htRight = 11;
        const int htTop = 12;
        const int htTopLeft = 13;
        const int htTopRight = 14;
        const int htBottom = 15;
        const int htBottomLeft = 16;
        const int htBottomRight = 17;
        bool handled = false;
        switch (m.Msg)
        {
            case WM_NCPAINT:                        // box shadow
                if (m_aeroEnabled)
                {
                    var v = 2;
                    DwmSetWindowAttribute(this.Handle, 2, ref v, 4);
                    MARGINS margins = new MARGINS()
                    {
                        bottomHeight = 1,
                        leftWidth = 1,
                        rightWidth = 1,
                        topHeight = 1
                    };
                    DwmExtendFrameIntoClientArea(this.Handle, ref margins);

                }
                break;
            default:
                break;
        }
        if (m.Msg == wmNcHitTest)
        {
            int x = (int)(m.LParam.ToInt64() & 0xFFFF);
            int y = (int)((m.LParam.ToInt64() & 0xFFFF0000) >> 16);
            Point pt = PointToClient(new Point(x, y));
            Size clientSize = ClientSize;
            ///allow resize on the lower right corner
            if (pt.X >= clientSize.Width - 16 && pt.Y >= clientSize.Height - 16 && clientSize.Height >= 16)
            {
                m.Result = (IntPtr)(IsMirrored ? htBottomLeft : htBottomRight);
                return;
            }
            ///allow resize on the lower left corner
            if (pt.X <= 16 && pt.Y >= clientSize.Height - 16 && clientSize.Height >= 16)
            {
                m.Result = (IntPtr)(IsMirrored ? htBottomRight : htBottomLeft);
                return;
            }
            ///allow resize on the upper right corner
            if (pt.X <= 16 && pt.Y <= 16 && clientSize.Height >= 16)
            {
                m.Result = (IntPtr)(IsMirrored ? htTopRight : htTopLeft);
                return;
            }
            ///allow resize on the upper left corner
            if (pt.X >= clientSize.Width - 16 && pt.Y <= 16 && clientSize.Height >= 16)
            {
                m.Result = (IntPtr)(IsMirrored ? htTopLeft : htTopRight);
                return;
            }
            ///allow resize on the top border
            if (pt.Y <= 16 && clientSize.Height >= 16)
            {
                m.Result = (IntPtr)(htTop);
                return;
            }
            ///allow resize on the bottom border
            if (pt.Y >= clientSize.Height - 16 && clientSize.Height >= 16)
            {
                m.Result = (IntPtr)(htBottom);
                return;
            }
            ///allow resize on the left border
            if (pt.X <= 16 && clientSize.Height >= 16)
            {
                m.Result = (IntPtr)(htLeft);
                return;
            }
            ///allow resize on the right border
            if (pt.X >= clientSize.Width - 16 && clientSize.Height >= 16)
            {
                m.Result = (IntPtr)(htRight);
                return;
            }
        }
        if (!handled)
            base.WndProc(ref m);
    }
    public MAIN()
    {
        InitializeComponent();
        m_aeroEnabled = false;
    }
    private Point mMousePos;
    private Point mFormPos;
    private bool mDragging;
    private void Header_MouseDown(object sender, MouseEventArgs e)
    {
        mDragging = true;
        mMousePos = Header.PointToScreen(e.Location);
        mFormPos = Location; 
    } 
    private void Header_MouseUp(object sender, MouseEventArgs e)
    {
        mDragging = false;     
    }
    private void Header_MouseMove(object sender, MouseEventArgs e)
    {
        if (!mDragging) return;
        Point pos = Header.PointToScreen(e.Location);
        Left = mFormPos.X + pos.X - mMousePos.X;
        Top = mFormPos.Y + pos.Y - mMousePos.Y;
    }
}
使用System.Runtime.InteropServices;
公共班机
{
[DllImport(“Gdi32.dll”,EntryPoint=“CreateRoundRectRgn”)]
私有静态外部IntPtr CreateRoundRectRgn
(
int nLeftRect,//左上角的x坐标
int nTopRect,//左上角的y坐标
int nRightRect,//右下角的x坐标
int nBottomRect,//右下角的y坐标
int nWidthEllipse,//椭圆的高度
int nheight椭圆//椭圆的宽度
);
[DllImport(“dwmapi.dll”)]
公共静态外部int dwmextendframeintoclienterea(IntPtr hWnd,ref MARGINS pMarInset);
[DllImport(“dwmapi.dll”)]
公共静态外部int DwmSetWindowAttribute(IntPtr hwnd、int attr、ref int attrValue、int attrSize);
[DllImport(“dwmapi.dll”)]
公共静态外部int DwmIsCompositionEnabled(ref int pfEnabled);
private bool m_aeroEnabled;//框阴影的变量
私有常量int CS_DROPSHADOW=0x00020000;
私有常量int WM_NCPAINT=0x0085;
私有常量int WM_ACTIVATEAPP=0x001C;
公共结构页边距//框阴影结构
{
公共宽度;
公共宽度;
公共室内高度;
公众高度;
}
private const int WM_NCHITTEST=0x84;//用于拖动表单的变量
private const int HTCLIENT=0x1;
私有常量int HTCAPTION=0x2;
受保护的重写CreateParams CreateParams
{
得到
{
m_aeroEnabled=检查aeroEnabled();
CreateParams cp=base.CreateParams;
如果(!m_已启用)
cp.ClassStyle |=CS|u DROPSHADOW;
返回cp;
}
}
私有布尔检查启用()
{
如果(Environment.OSVersion.Version.Major>=6)
{
int enabled=0;
DWMICompositionEnabled(已启用参考);
返回(启用==1)?真:假;
}
返回false;
}
受保护的覆盖无效WndProc(参考消息m)
{
常量int wmNcHitTest=0x84;
常量int htLeft=10;
常数int htRight=11;
常数int htTop=12;
常数int htTopLeft=13;
常数int htTopRight=14;
常数int htBottom=15;
常量int htBottomLeft=16;
const int htBottomRight=17;
bool=false;
开关(m.Msg)
{
案例WM\NCPAINT://框阴影
如果(已启用m_)
{
var v=2;
DwmSetWindowAttribute(this.Handle,2,ref v,4);
页边距=新页边距()
{
底部高度=1,
leftWidth=1,
rightWidth=1,
顶高=1
};
DwmExtendFrameIntoClientArea(this.Handle,ref-margins);
}
打破
违约:
打破
}
如果(m.Msg==wmNcHitTest)
{
intx=(int)(m.LParam.ToInt64()&0xFFFF);
int y=(int)((m.LParam.ToInt64()&0xFFFF0000)>>16);
点pt=点到客户端(新点(x,y));
Size clientSize=clientSize;
///允许在右下角调整大小
如果(pt.X>=clientSize.Width-16&&pt.Y>=clientSize.Height-16&&clientSize.Height>=16)
{
m、 结果=(IntPtr)(IsMirrored?htBottomLeft:htBottomRight);
返回;
}
///允许在左下角调整大小
if(pt.X=clientSize.Height-16&&clientSize.Height>=16)
{
m、 结果=(IntPtr)(IsMirrored?htBottomRight:htBottomLeft);
返回;
}
///允许在右上角调整大小
if(pt.X=clientSize.Width-16&&pt.Y=16)
{
m、 结果=(IntPtr)(IsMirrored?htTopLeft:htTopRight);
返回;
}
///允许在顶部边框上调整大小
如果(第Y部分=16)
{
m、 结果=(IntPtr)(htTop);
返回;
}
///允许在底部边框上调整大小
if(pt.Y>=clientSize.Height-16&&clientSize.Height>=16)
{
m、 结果=(IntPtr)(htBottom);
返回;
}
///允许在左边框上调整大小
如果(pt.X=16)
{
m、 结果=(IntPtr)(htLeft);
返回;
}
///允许在右边框上调整大小
如果(pt.X>=clientSize.Width-16&&clientSize.Height>=16)
{
m、 结果=(IntPtr)(htRight);
返回;
}
}
如果(!已处理)
基准WndProc(参考m);
}
公用干管()
{
初始化组件();
m_=错误;
}
私人点mMousePos;
私人点mFormPos;
私人厕所;
私有空标头\u MouseDown(对象发送方,MouseEventArgs e)
{
mDragging=true;
mMousePos=标题。指向屏幕(如位置);
mFormPos=位置;
} 
私有无效标头\u MouseUp(对象发送方,MouseEventArgs e)
{
mDragging=false;
}
私有无效标头\u MouseMove(对象发送方,MouseEventArgs e)
{
如果(!mDragging)返回;
点位置=标题点至屏幕(如位置);
左=mFormPos.X+位置X-mMousePos.X;
顶部=M或MPOS.Y+位置