等比例改变窗口大小

    //宽高比
    private double height_width = Todouble(this.Height) / Todouble(this.Width);
    //转换成double类型
    public double Todouble(int value)
    {
        double wid = Convert.ToDouble(value);
        return wid;
    }
    const int WM_NCHITTEST = 0x0084;
    const int WM_SIZING = 0x0214;
    private double height_width = -1;
    private int mod = -1;
    private int SizeValue = 8;
    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case WM_NCHITTEST:
                base.WndProc(ref m);
                Point vPoint = new Point((int)m.LParam & 0xFFFF,
                    (int)m.LParam >> 16 & 0xFFFF);
                vPoint = PointToClient(vPoint);
                if (vPoint.X <= SizeValue)
                {
                    if (vPoint.Y <= SizeValue)
                    {
                        mod = SizeType.LeftTop;
                        m.Result = (IntPtr)mod;
                    }
                    else if (vPoint.Y >= ClientSize.Height - SizeValue)
                    {
                        mod = SizeType.LeftBottom;
                        m.Result = (IntPtr)mod;
                    }
                }
                else if (vPoint.X >= ClientSize.Width - SizeValue)
                {
                    if (vPoint.Y <= SizeValue)
                    {
                        mod = SizeType.RightTop;
                        m.Result = (IntPtr)mod;
                    }
                    else if (vPoint.Y >= ClientSize.Height - SizeValue)
                    {
                        mod = SizeType.RightBottom;
                        m.Result = (IntPtr)mod;
                    }
                }
                break;
            case WM_SIZING:
                FmRECT rect = (FmRECT)Marshal.PtrToStructure(m.LParam, typeof(FmRECT));
                int Width = rect.right - rect.left;
                if (Width < 50)
                {
                    Width = 50;
                }
                int height = Convert.ToInt32(Width * height_width);
                rect.left = this.Left;
                rect.right = this.Right;
                rect.top = this.Top;
                rect.bottom = this.Bottom;
                switch (mod)
                {
                    case SizeType.LeftTop:
                        rect.left = rect.right - Width;
                        rect.top = rect.bottom - height;
                        break;
                    case SizeType.LeftBottom:
                        rect.left = rect.right - Width;
                        rect.bottom = rect.top + height;
                        break;
                    case SizeType.RightTop:
                        rect.right = rect.left + Width;
                        rect.top = rect.bottom - height;
                        break;
                    case SizeType.RightBottom:
                        rect.right = rect.left + Width;
                        rect.bottom = rect.top + height;
                        break;
                    default:
                        break;
                }
                Marshal.StructureToPtr(rect, m.LParam, false);
                break;
            default:
                base.WndProc(ref m);
                break;
        }
    }
Last modification:July 10th, 2019 at 02:37 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment