C#使用_鼠标拖动无标题栏窗口

来源:互联网 发布:西安财经行知学院好吗 编辑:程序博客网 时间:2024/05/16 07:10

 今天晚上在论坛看到牛人回复了,受教不少

 

用鼠标控制无标题栏Form的移动,以前只知道通过注册MouseDown,MouseMove和MouseUp事件来实现,而使用下面的方法更加简单

来源(http://topic.csdn.net/u/20081208/19/f5d92437-4602-4798-b8d1-0d6730dec98f.html?seed=1470908657

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
  9. namespace WindowsApplication1
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         [DllImport("user32.dll", EntryPoint = "SendMessage")]
  18.         public static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
  19.         [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
  20.         public static extern int ReleaseCapture();
  21.         public const int WM_SysCommand = 0x0112;
  22.         public const int SC_MOVE = 0xF012;
  23.         
  24.         private void Form1_MouseDown(object sender, MouseEventArgs e)
  25.         {
  26.             ReleaseCapture();
  27.             SendMessage(this.Handle.ToInt32(), WM_SysCommand, SC_MOVE, 0);
  28.         }
  29.         private void Form1_Load(object sender, EventArgs e)
  30.         {
  31.         }
  32.     }
  33. }
原创粉丝点击