怎样让一个Image控件或Picture控件跟随鼠标指针
这个和拖动无标题窗口差不多
通常我们是拖动标题栏来移动窗口,当没有标题栏时,该如何来拖动窗口呢?方法是 当我们在窗口区按下鼠标左键时,调用ReleaseCapture函数,释放鼠标的俘获,同时 发送移动标题栏的消息,就可以实现窗口的拖动。
下面的例子实现了该功能。
>>步骤1----建立新工程,在窗体上放置CommandButton按钮。
>>步骤2----改变窗体的外观,使BorderStyle = 0(None)
>>步骤3----编写如下代码:
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 Any) As Long
Private Const WM_SYSCOMMAND = &H112
Private Const SC_MOVE = &HF010&
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2
Private Sub Command1_Click()
End
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage Me.hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0
'SendMessage hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
'上述两种方法都能实现该功能。
End Sub
>>步骤4----编译运行,在窗体上按下鼠标左键,是不是可以拖动了?按下Command1结束。
我的代码如下
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 Any) As Long
Private Const WM_SYSCOMMAND = &H112
Private Const SC_MOVE = &HF010&
Private Const WM_NCLBUTTONDOWN = &HA1
Private Const HTCAPTION = 2
Private Sub Forn_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
ReleaseCapture
SendMessage Picture1.hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&
End Sub
这时,无论鼠标在窗体中怎么移动,Picture1就是不动,我想可能跟WM_NCLBUTTONDOWN有关吧,请问该如何更改,使Picture1跟随鼠标指针,或者用其他方法吧