VB 拖动控件移动窗体
我在FORM1里搞了一张PictureBox,并放进了一张图片,放在了没有标题栏的form1的最上边代替标题栏,但是鼠标无法拖动图片来移动窗体,在网上只找到了拖动窗体移动...
我在FORM1里搞了一张PictureBox,并放进了一张图片,放在了没有标题栏的form1的最上边代替标题栏,但是鼠标无法拖动图片来移动窗体,在网上只找到了拖动窗体移动的代码,没有我这个样子的,求高人写下代码,今天第7次发VB菜鸟题了,同样,答对了另外附加100分。
看看这个代码是否可以使用
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Const HTCAPTION = 2
Const WM_NCLBUTTONDOWN = &HA1
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = 1 Then
Dim ReturnVal As Long
x = ReleaseCapture()
ReturnVal = SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
End If
End Sub 展开
看看这个代码是否可以使用
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Const HTCAPTION = 2
Const WM_NCLBUTTONDOWN = &HA1
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = 1 Then
Dim ReturnVal As Long
x = ReleaseCapture()
ReturnVal = SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)
End If
End Sub 展开
1个回答
展开全部
Option Explicit
'变量声明
'MoveScreen,布尔型变量,标示窗体是否处于被移动状态
Dim MoveScreen As Boolean
'鼠标位置
Dim MousX As Integer
Dim MousY As Integer
'窗体位置
Dim CurrX As Integer
Dim CurrY As Integer
'“退出”按钮
Private Sub CmdExit_Click()
End
End Sub
'当鼠标在窗体上按下时
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'---------------------------------------------
'参数说明:
'---------------------------------------------
'button 返回一个整数,用来标识按下或释放的是哪一
'个按钮。button 参数的值为相应于左按钮(1)右按钮
'(2),以及中间按钮(4)。
'---------------------------------------------
'shift 返回一个整数,在鼠标按钮被按下或者被释放
'的同时,SHIFT,CTRL,和 ALT 键的状态,返回的shift
'参数值分别为1,2,和 4。指示这些键的状态。
'---------------------------------------------
'x, y 返回一个指定鼠标指针当前位置的数。
'---------------------------------------------
'如果是鼠标左键按下
If Button = 1 Then
'标示为移动状态
MoveScreen = True
'得到鼠标在窗体上的位置(相对与窗体内部坐标)
MousX = X
MousY = Y
End If
End Sub
'当鼠标在窗体上移过时
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'如果处于鼠标左键按下的状态,即MoveScreen = True时
If MoveScreen Then
'计算新的窗体坐标值
'仔细想一下,看看是不是这样
CurrX = Form1.Left - MousX + X
CurrY = Form1.Top - MousY + Y
'移动窗体到新的位置
Form1.Move CurrX, CurrY
End If
'把新的窗体坐标显示出来,是相对于屏幕的坐标
Label3.Caption = CurrX
Label4.Caption = CurrY
'把鼠标点击的位置显示出来,是相对与窗体的坐标
Label7.Caption = MousX
Label8.Caption = MousY
End Sub
'如果鼠标松开,则停止拖动
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MoveScreen = False
End Sub
这是移动窗体的,只要把form都改成picture,再加上
form1.move picture1.left,pictuer1.top
'变量声明
'MoveScreen,布尔型变量,标示窗体是否处于被移动状态
Dim MoveScreen As Boolean
'鼠标位置
Dim MousX As Integer
Dim MousY As Integer
'窗体位置
Dim CurrX As Integer
Dim CurrY As Integer
'“退出”按钮
Private Sub CmdExit_Click()
End
End Sub
'当鼠标在窗体上按下时
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'---------------------------------------------
'参数说明:
'---------------------------------------------
'button 返回一个整数,用来标识按下或释放的是哪一
'个按钮。button 参数的值为相应于左按钮(1)右按钮
'(2),以及中间按钮(4)。
'---------------------------------------------
'shift 返回一个整数,在鼠标按钮被按下或者被释放
'的同时,SHIFT,CTRL,和 ALT 键的状态,返回的shift
'参数值分别为1,2,和 4。指示这些键的状态。
'---------------------------------------------
'x, y 返回一个指定鼠标指针当前位置的数。
'---------------------------------------------
'如果是鼠标左键按下
If Button = 1 Then
'标示为移动状态
MoveScreen = True
'得到鼠标在窗体上的位置(相对与窗体内部坐标)
MousX = X
MousY = Y
End If
End Sub
'当鼠标在窗体上移过时
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
'如果处于鼠标左键按下的状态,即MoveScreen = True时
If MoveScreen Then
'计算新的窗体坐标值
'仔细想一下,看看是不是这样
CurrX = Form1.Left - MousX + X
CurrY = Form1.Top - MousY + Y
'移动窗体到新的位置
Form1.Move CurrX, CurrY
End If
'把新的窗体坐标显示出来,是相对于屏幕的坐标
Label3.Caption = CurrX
Label4.Caption = CurrY
'把鼠标点击的位置显示出来,是相对与窗体的坐标
Label7.Caption = MousX
Label8.Caption = MousY
End Sub
'如果鼠标松开,则停止拖动
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MoveScreen = False
End Sub
这是移动窗体的,只要把form都改成picture,再加上
form1.move picture1.left,pictuer1.top
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询