111,093
社区成员




用mouse_event试试
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);
[DllImport("user32.dll")]
static extern int SetCursorPos(int x, int y);
[DllImport("user32.dll")]
static extern int mouse_event(UInt32 dwFlags, Int32 dx, Int32 dy, Int32 cButtons, Int32 dwExtraInfo);
private readonly UInt32 WM_SYSCOMMAND = 0x112;
private readonly UInt32 SC_MOVE = 0xF010;
private readonly UInt32 HTCAPTION = 2;
/// <summary>
/// 鼠标控制参数
/// </summary>
private readonly UInt32 MOUSEEVENTF_LEFTDOWN = 0x2;
private readonly UInt32 MOUSEEVENTF_LEFTUP = 0x4;
private void Form1_Load(object sender, EventArgs e)
{
button1.MouseDown += MyMouseMove;
}
private void MyMouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.button1.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
}
private void button2_Click(object sender, EventArgs e)
{
System.Drawing.Point p = PointToScreen(button1.Location);
SetCursorPos(p.X + button1.Width / 2, p.Y + button1.Height / 2);
mouse_event(MOUSEEVENTF_LEFTDOWN, p.X + button1.Width / 2, p.Y + button1.Height / 2, 0, 0);
SetCursorPos(p.X, p.Y);
mouse_event(MOUSEEVENTF_LEFTUP, p.X, p.Y, 0, 0);
}
}
}