VB 程序设计

10个成员

VB中拖动没有标题栏的窗体

发表于 2016-12-22 3400 次查看
程序代码:

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

 

发表回复
你还没有登录,请先登录注册