VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 拖动没有标题栏的窗体
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:8699, 日期:2001-11-26
拖动没有标题栏的窗体

程序说明:

  这种方法是一种高手方法,同样是讲传递给Windows一个鼠标的当前位置是在标题区内,但和(一).(二)不同的地方是前两个方法是通过SendMessage函数来传消息给Windows,而这个方法是在Windows的到消息前,通过对WM_NCHITTSET消息的栏截.来判断当前的鼠标位置,然后作出相应的处理

程序代码:

Module1
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
End Function


  程序首先调用CallWindowProc,这样就可以从还回值的知当前的鼠标位置位于那一个区域,接着判断返回值是否等于HTCLENT(工作区)内,如果是,则将返回值设置成TICAPTION(标题区),这样就可以瞒过Windows,让Windows以为鼠标位于标题区内

Form1

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