日期:2014-05-17  浏览次数:20783 次

C#鼠标拖动界面问题
我要把一个界面做成可以拖动的效果,但是,界面上有很多别的控件,PANEL,LABEL等等。。如果只给FORM绑定事件的话,鼠标放在别的控件上会没有效果,难道我要每一个控件都绑定事件?那不是太麻烦了吗?有什么解决的方法吗?

------解决方案--------------------
C# code
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("user32.dll")]
        static extern bool ReleaseCapture();
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, UInt32 wParam, UInt32 lParam);

        private readonly UInt32 WM_SYSCOMMAND = 0x112;
        private readonly UInt32 SC_MOVE = 0xF010;
        private readonly UInt32 HTCAPTION = 2;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MouseDown += MyMouseMove;
            foreach (Control c in this.Controls)
            {
                c.MouseDown += MyMouseMove;
            }
        }

        private void MyMouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
        }
    }
}

------解决方案--------------------
没必要所有控件都可以拖动窗体吧
------解决方案--------------------
探讨

额,这个方法只针对FORM上的控件是有用的,但是如果我PANEL上再放一个PANEL,PANEL上再加PANEL呢?不会用递归把所有的控件都绑定吧。。。