使用面板

时间:2017-01-26 08:55:02

标签: c# .net winforms

我有3个面板,它们位于顶部,左侧和中间,就像容器一样。

因为面板已满了,所以没有空间可以点击主表单。

我想移动我的表单,点击顶部停靠的面板。

下面的代码工作正常,不使用面板(单击面板下的主窗体)

 protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case 0x84:
                base.WndProc(ref m);
                if ((int)m.Result == 0x1)
                    m.Result = (IntPtr)0x2;
                return;
        }

        base.WndProc(ref m);
    }

我尝试使用panel1_MouseDown制作但没有运气。

有没有办法实现它?

2 个答案:

答案 0 :(得分:1)

这似乎对我有用:

bool moving;
Point offset;
Point original;

void panel1_MouseDown(object sender, MouseEventArgs e)
{
    moving = true;
    panel1.Capture = true;
    offset = MousePosition;
    original = this.Location;
}

void panel1_MouseMove(object sender, MouseEventArgs e)
{
    if (!moving)
        return;

    int x = original.X + MousePosition.X - offset.X;
    int y = original.Y + MousePosition.Y - offset.Y;

    this.Location = new Point(x, y);
}

void panel1_MouseUp(object sender, MouseEventArgs e)
{
    moving = false;
    panel1.Capture = false;
}

答案 1 :(得分:0)

尝试一下,它对我有用。在主面板上使用此方法,是指您要移动表格或去观看我发现的视频的面板https://www.youtube.com/watch?v=CN1Y8diE3Zg

int mouseX, mouseY;
        bool mouseM;

        private void panel7_MouseUp(object sender, MouseEventArgs e)
        {
            mouseM = false;
        }

        private void panel7_MouseDown(object sender, MouseEventArgs e)
        {
            mouseX = e.X;
            mouseY = e.Y;
            mouseM = true;
        }

        private void panel7_MouseMove(object sender, MouseEventArgs e)
        {
            if (mouseM)
            {
                SetDesktopLocation(MousePosition.X - mouseX, MousePosition.Y - mouseY);
            }
        }