这个问题也是来自论坛提问,同样是.Net WinForm新手的问题,这样的问题如果在Deplphi这样的板块立刻就被秒杀了,可是.Net版知道的人好像不太多。
介绍两个方法,一个是发送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 ;
- }
- }
- }
- }
- }