2021-05-21 仓库温控系统(Winform) 19 窗体拖动功能实现

Posted 微软MVP Eleven

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-05-21 仓库温控系统(Winform) 19 窗体拖动功能实现相关的知识,希望对你有一定的参考价值。

Point point = new Point();
bool isMove = false;
private void panelTop_MouseDown(object sender, MouseEventArgs e)
{
    point = e.Location;//按住的点
    isMove = true;

}

private void panelTop_MouseMove(object sender, MouseEventArgs e)
{
     if(e.Button==MouseButtons.Left&&isMove)
    {
        Point pointNew = e.Location;//按住的点拖动到的位置
        Point fPointNew = new Point(pointNew.X - point.X, pointNew.Y - point.Y);//相对于原来起点的距离点的描述
        this.Location += new Size(fPointNew);
    }
}

const int WM_NCHITTEST = 0x0084;// 移动鼠标
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 = 0x10;
const int HTBOTTOMRIGHT = 17;

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);
    switch (m.Msg)
    {
        case WM_NCHITTEST:
                Point vPoint = new Point((int)m.LParam & 0xFFFF,
               (int)m.LParam >> 16 & 0xFFFF);
                vPoint = PointToClient(vPoint);
                if (vPoint.X <= 5)
                    if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOPLEFT;
                    else if (vPoint.Y >= ClientSize.Height - 5)
                        m.Result = (IntPtr)HTBOTTOMLEFT;
                    else m.Result = (IntPtr)HTLEFT;
                else if (vPoint.X >= ClientSize.Width - 5)
                    if (vPoint.Y <= 5)
                        m.Result = (IntPtr)HTTOPRIGHT;
                    else if (vPoint.Y >= ClientSize.Height - 5)
                        m.Result = (IntPtr)HTBOTTOMRIGHT;
                    else m.Result = (IntPtr)HTRIGHT;
                else if (vPoint.Y <= 5)
                    m.Result = (IntPtr)HTTOP;
                else if (vPoint.Y >= ClientSize.Height - 5)
                    m.Result = (IntPtr)HTBOTTOM;
          
                break;
    }
}

/// <summary>
/// 面板中的页面尺寸与面板的尺寸同步---面板中Form页自适应处理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void panelPage_SizeChanged(object sender, EventArgs e)
{
     foreach(Control c in panelPage.Controls)
    {
        Form f = c as Form;
        f.WindowState = FormWindowState.Normal;
        f.SuspendLayout();
        f.Size = panelPage.Size;
        //DataGridView 闪烁--- 先挂起  ,,,再恢复
        f.ResumeLayout();
        f.WindowState = FormWindowState.Maximized;
    }
}

以上是关于2021-05-21 仓库温控系统(Winform) 19 窗体拖动功能实现的主要内容,如果未能解决你的问题,请参考以下文章

2021-05-21 仓库温控系统(Winform) 19 窗体拖动功能实现

2021-05-21 仓库温控系统(Winform) 17 定时器的使用

2021-05-21 仓库温控系统(Winform) 05 获取类属性静态方法PropertyHelper

2021-05-21 仓库温控系统(Winform) 18 封装Panel中显示Form页方法

2021-05-22 仓库温控系统(Winform) 20 PanelPage面板自适应

吉特仓库管理系统(开源)-如何在网页端启动WinForm 程序