1.在mouse事件中实现
创新互联公司专业IDC数据服务器托管提供商,专业提供成都服务器托管,服务器租用,服务器托管,服务器托管,成都多线服务器托管等服务器托管服务。
2.调用windows API
实现方式为:
1.在mouse事件中实现
[csharp] view plain copy
Point mouseOff;//鼠标移动位置变量
bool leftFlag;//标签是否为左键
private void groupControl1_MouseUp(object sender, MouseEventArgs e)
{
if (leftFlag)
{
leftFlag = false;//释放鼠标后标注为false;
}
}
private void groupControl1_MouseMove(object sender, MouseEventArgs e)
{
if (leftFlag)
{
Point mouseSet = Control.MousePosition;
mouseSet.Offset(mouseOff.X, mouseOff.Y); //设置移动后的位置
Location = mouseSet;
}
}
private void groupControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseOff = new Point(-e.X, -e.Y); //得到变量的值
leftFlag = true; //点击左键按下时标注为true;
}
}
private void groupControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseOff = new Point(-e.X, -e.Y); //得到变量的值
leftFlag = true; //点击左键按下时标注为true;
}
}
2.调用windows API
调用前需要添加using System.Runtime.InteropServices;
[csharp] view plain copy
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
private void groupControl1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture(); //释放鼠标捕捉
//发送左键点击的消息至该窗体(标题栏)
SendMessage(Handle, 0xA1, 0x02, 0);
}
}
SetCursorPos(old_x,old_y);
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
SetCursorPos(pt_x,pt_y);
长按你可以用计时器来实现啊,写个例子:
Private blCtrl As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.LControlKey Then blCtrl = True
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If e.KeyCode = Keys.LControlKey Then blCtrl = False
End Sub
然后你可以在计时器里检测blCtrl的状态来确定,不知道你的具体情况是什么样的
总之是用一个布尔变量来记录按键状态,配合计时器来达到你想要的功能
很简单,加入全局变量中有一个变量,mousedown事件中设为1,在mouseup事件中置为0。这样就能实现你说的功能。
以下参考
Private Sub 天平左移按钮_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles 天平左移按钮.PreviewMouseDown
a=true
End Sub
Private Sub 天平左移按钮_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)Handles 天平左移按钮.PreviewMouseUp
a=false
End Sub