请问如何移动不带标题栏的窗体?
同上 问题点数:20、回复次数:5Top
1 楼happy_sea(开心海(数据读取中,请稍候......))回复于 2004-12-02 19:46:55 得分 10
1、在MouseDown事件发生时判断按键状态并获取鼠标位置:
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'如果是鼠标左键按下
If Button = 1 Then
'标示为移动状态
MoveScreen = True
'得到鼠标在窗体上的位置(相对与窗体内部坐标)
MousX = X
MousY = Y
End If
End Sub
2、当鼠标MouseMove事件发生时,计算新的窗体坐标,并移动:
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
End Sub
Top
2 楼Zezese(蓝酷云)回复于 2004-12-02 20:19:44 得分 10
'模块
Option Explicit
Public Const GWL_WNDPROC = (-4)
Public Const WM_NCHITTEST = &H84
Public Const HTCLIENT = 1
Public Const HTCAPTION = 2
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public prevWndProc As Long
Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
WndProc = CallWindowProc(prevWndProc, hWnd, Msg, wParam, lParam)
If Msg = WM_NCHITTEST And WndProc = HTCLIENT Then
WndProc = HTCAPTION
End If
'窗体
Option Explicit
Private Sub Command1_Click()
Unload Me
End Sub
Private Sub Form_Load()
prevWndProc = GetWindowLong(Me.hWnd, GWL_WNDPROC)
SetWindowLong Me.hWnd, GWL_WNDPROC, AddressOf WndProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
SetWindowLong Me.hWnd, GWL_WNDPROC, prevWndProc
End Sub
Top
3 楼baoaya(点头)(大风起兮云飞扬)回复于 2004-12-02 20:51:10 得分 0
private sub from_mousedown(Button As Integer, Shift As Integer, X As Single, Y As Single)
if me.WindowState = 0 Then
const WM_NCLBUTTONDOWN=&HA1
call SendMessage (me.hWnd, WM_NCLBUTTONDOWN, 2, 0&) '发送按住标题栏的“假消息”呵呵
end if
end sub
这里是在from的mousedown事件里,你也可以 自己制作一个 标题栏Top
4 楼baoaya(点头)(大风起兮云飞扬)回复于 2004-12-02 20:56:10 得分 0
对不起 刚才手动输入 没测试过 这个是完整的代码,只要按住窗体的任何地方就可以移动他
Option Explicit
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 Declare Function ReleaseCapture Lib "user32" () As Long
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Me.WindowState = 0 Then
Const WM_NCLBUTTONDOWN = &HA1
ReleaseCapture
Call SendMessage(Me.hWnd, WM_NCLBUTTONDOWN, 2, 0&) '发送按住标题栏的“假消息”呵呵
End If
End Sub
Top
5 楼syre(神仙)回复于 2004-12-02 22:01:11 得分 0
private sub from_mousedown(Button As Integer, Shift As Integer, X As Single, Y As Single)
if me.WindowState = 0 Then
const WM_NCLBUTTONDOWN=&HA1
call SendMessage (me.hWnd, WM_NCLBUTTONDOWN, 2, 0&) '发送按住标题栏的“假消息”呵呵
end if
end sub
如此最简单Top




