没有标题栏的窗体一般是不能按住鼠标左键进行拖动的,但是通过API函数或是消息重定向就可以实现了。
介绍两个方法,一个是发送SC_Move消息,一个是改变鼠标区域消息
方法一:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace WindowsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- [DllImport("user32.dll", EntryPoint = "SendMessage")]
- public static extern int SendMessage(int hWnd, int wMsg, int wParam, int lParam);
- [DllImport("user32.dll", EntryPoint = "ReleaseCapture")]
- public static extern int ReleaseCapture();
- public const int WM_SysCommand = 0x0112;
- public const int SC_MOVE = 0xF012;
- private void Form1_MouseDown(object sender, MouseEventArgs e)
- {
- ReleaseCapture();
- SendMessage(this.Handle.ToInt32(), WM_SysCommand, SC_MOVE, 0);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- }
- }
方法二:
- using System;
- using System.Windows.Forms;
- namespace WindowsApplication1
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- protected override void WndProc(ref Message m)
- {
- base.WndProc(ref m); if (m.Msg == 0x84)
- {
- switch (m.Result.ToInt32())
- {
- case 1:
- m.Result = new IntPtr(2); break;
- }
- }
- }
- }
- }