如何在鼠标移动时移动表单. [英] How to Move a Form, on mouse Move.

查看:62
本文介绍了如何在鼠标移动时移动表单.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何拖动表格,就像在表格上按下鼠标并移动它一样.

How to Drag Form, like press Mouse on Form and Move it

推荐答案

这是怎么回事?无论如何,对于Google员工,我也为这个琐碎的问题做了一些复制粘贴.

What''s going on here? Anyway, for the googlers I did also some copy paste for this trivial problem.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace MovableForm
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.Run(new MovableForm());
        }

        public partial class MovableForm : Form
        {
            int m_iWindowPosX;
            int m_iWindowPosY;

            public MovableForm()
            {
                FormBorderStyle = FormBorderStyle.None;
            }

            protected override void OnMouseDown(MouseEventArgs e)
            {
                m_iWindowPosX = e.X;
                m_iWindowPosY = e.Y;

                base.OnMouseDown(e);
            }

            protected override void OnMouseMove(MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                    Location = new Point(Location.X - m_iWindowPosX + e.X, Location.Y - m_iWindowPosY + e.Y);

                base.OnMouseMove(e);
            }
        }
    }
}


尝试关注:
Try Follow:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        bool mousedown;
        int x, y;
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                x = MousePosition.X - DesktopLocation.X;
                y = MousePosition.Y - DesktopLocation.Y;
                mousedown = true;
            }
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if(mousedown)
            {
                SetBounds(MousePosition.X - y, MousePosition.Y - y, Width, Height);
            }
        }
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            mousedown = false;
        }
    }
}


这篇关于如何在鼠标移动时移动表单.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆