VB窗口自动停靠桌面边界

'Module模块代码:
Option Explicit
Public Declare Function SystemParametersInfo Lib “user32.dll” Alias “SystemParametersInfoA” ( _
ByVal uAction As Long, _
ByVal uParam As Long, _
lpvParam As Any, _
ByVal fuWinIni As Long) As Long '去掉lpvParam的Byval修饰符才可以正常工作

Public Declare Function SetWindowLong Lib “user32.dll” Alias “SetWindowLongA” ( _
ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
Public Declare Function GetWindowLong Lib “user32.dll” Alias “GetWindowLongA” ( _
ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Public Declare Function CallWindowProc Lib “user32.dll” Alias “CallWindowProcA” ( _
ByVal lpPrevWndFunc As Long, _
ByVal hwnd As Long, _
ByVal msg As Long, _
ByVal wParam As Long, _
ByVal lParam As Long) As Long
Public Declare Sub CopyMemory Lib “kernel32.dll” Alias “RtlMoveMemory” ( _
Destination As Any, _
Source As Any, _
ByVal Length As Long)

Public Type WINDOWPOS
hwnd As Long
hWndInsertAfter As Long
x As Long
y As Long
cx As Long
cy As Long
flags As Long
End Type

Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Public Const SPI_GETWORKAREA As Long = 48
Public Const GWL_WNDPROC As Long = -4
Public Const WM_WINDOWPOSCHANGING As Long = &H46

Global lpPrevWndProc As Long
Global gHW As Long
Private Const Margin As Long = 20

Public Sub Hook()
lpPrevWndProc = SetWindowLong(gHW, GWL_WNDPROC, AddressOf WindowProc)
End Sub

Public Sub Unhook()
Dim temp As Long
temp = SetWindowLong(gHW, GWL_WNDPROC, lpPrevWndProc)
End Sub

Function WindowProc(ByVal hw As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim lpwndpos As WINDOWPOS
Dim WorkArea As RECT

If uMsg = WM_WINDOWPOSCHANGING Then
  SystemParametersInfo SPI_GETWORKAREA, 0, WorkArea, 0
  CopyMemory lpwndpos, ByVal lParam, Len(lpwndpos)
 
  If lpwndpos.x - WorkArea.Left < Margin And WorkArea.Left - lpwndpos.x < Margin Then lpwndpos.x = 0
  If lpwndpos.y - WorkArea.Top < Margin And WorkArea.Top - lpwndpos.y < Margin Then lpwndpos.y = 0
  If WorkArea.Right - lpwndpos.x - lpwndpos.cx < Margin And lpwndpos.x + lpwndpos.cx - WorkArea.Right < Margin Then lpwndpos.x = WorkArea.Right - lpwndpos.cx
  If WorkArea.Bottom - lpwndpos.y - lpwndpos.cy < Margin And lpwndpos.y + lpwndpos.cy - WorkArea.Bottom < Margin Then lpwndpos.y = WorkArea.Bottom - lpwndpos.cy
 
  CopyMemory ByVal lParam, lpwndpos, Len(lpwndpos)
End If

WindowProc = CallWindowProc(lpPrevWndProc, hw, uMsg, wParam, lParam)

End Function

‘Form代码:
Option Explicit

Private Sub Command1_Click()
Call SaveImage(App.path & “\2.jpg”)
End Sub

Private Sub Form_Load()
Me.Caption = loadpicture(App.path & “\001.jpg”)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Call GdiRev
End Sub

'声明:修改模块常数Private Const Margin As Long = 20可调整距离

你可能感兴趣的:(VisualBasic编程源码,VB6,窗口)