方法1:API(模拟鼠标操作)(噢,跟楼上的那位差不多)
我们提供的服务有:成都网站设计、做网站、微信公众号开发、网站优化、网站认证、金口河ssl等。为成百上千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的金口河网站制作公司
首先加入以下 API 声明以及相关常数(在标准模块中用 Public;在类模块或窗体中用 Private):
Public/Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public/Private Declare Function ReleaseCapture Lib "user32" () As Long
Public/Private Const WM_NCLBUTTONDOWN = HA1 '表示在非客户区(标题栏、边框)按下鼠标左键
Public/Private Const HTCAPTION = 2 '表示鼠标击中了窗口的标题栏
然后在某个控件的 MouseDown 事件的处理程序中加入:
ReleaseCapture
'在 VB 内部都会自动使用 SetCapture(API)来捕捉鼠标事件
'因此先使用 ReleaseCapture 使 SetCapture 失效
SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0
'然后向窗口发送鼠标在标题栏被按下的消息
例如:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage Me.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, ByVal 0
End Sub
方法2:VB
以通过窗口上的一个按钮(Command1)来拖动窗口为例,代码如下:
Dim LastX As Single, LastY As Single, IsMoving As Boolean
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbLeftButton Then
LastX = X
LastY = Y
IsMoving = True
End If
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If IsMoving Then
Me.Move Me.Left + (X - LastX), Me.Top + (Y - LastY)
End If
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
IsMoving = False
End Sub
方法1较为简便,但确实有效;
方法2的操控性较强,您可以通过程序来干预拖动过程(例如防止窗口被拖离屏幕)。
1、无边框窗体也就是无标题栏窗体,对于这样的窗体移动需要编程实现。
2、vb有两种办法实现,一直接编程实现,二调用windows API编程实现。
3、这里示例直接编程实现:
Option Explicit
Dim BolIsMove As Boolean, MousX As Long, MousY As Long
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then BolIsMove = True
MousX = X
MousY = Y
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Dim CurrX As Long, CurrY As Long
If BolIsMove Then
CurrX = Me.Left - MousX + X
CurrY = Me.Top - MousY + Y
Me.Move CurrX, CurrY
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
BolIsMove = False
End Sub
1.在mouse事件中实现
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);
}
}