窗体: Option Explicit Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long Private Declare Function GetDesktopWindow Lib "user32" () As LongPrivate Sub Form_Load() Dim hTaskBar As Long hTaskBar = FindWindow("Shell_TrayWnd", vbNullString) Debug.Print hTaskBar Dim RC As RECT Dim i As Long i = GetWindowRect(hTaskBar, RC) Dim taskheight As Long taskheight = RC.Bottom - RC.Top '任务栏高度 i = GetWindowRect(GetDesktopWindow, RC) Dim maxwidth As Long Dim maxheight As Long maxwidth = RC.Right - RC.Left '获取屏幕宽度 maxheight = RC.Bottom - RC.Top - taskheight '屏幕高度-任务栏高度 LockWindow Me.hwnd, , , maxwidth, maxheight End Sub模块: Option ExplicitPublic Type POINTAPI x As Long y As Long End TypePublic Type MINMAXINFO ptReserved As POINTAPI ptMaxSize As POINTAPI ptMaxPosition As POINTAPI ptMinTrackSize As POINTAPI ptMaxTrackSize As POINTAPI End TypePublic Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Public 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 LongPublic Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long Public Const WM_GETMINMAXINFO = &H24 Public Const GWL_WNDPROC = -4Global lpPrevWndProc As Long Public procOld As Long Public udtMMI As MINMAXINFO
Public Function WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long Select Case iMsg Case WM_GETMINMAXINFO Dim udtMINMAXINFO As MINMAXINFO CopyMemory udtMINMAXINFO, ByVal lParam, 40& With udtMINMAXINFO .ptMaxSize.x = udtMMI.ptMaxSize.x .ptMaxSize.y = udtMMI.ptMaxSize.y .ptMaxPosition.x = 0 .ptMaxPosition.y = 0 .ptMaxTrackSize.x = .ptMaxSize.x .ptMaxTrackSize.y = .ptMaxSize.y .ptMinTrackSize.x = udtMMI.ptMinTrackSize.x .ptMinTrackSize.y = udtMMI.ptMinTrackSize.y Debug.Print .ptMaxSize.x & "," & .ptMaxSize.y End With CopyMemory ByVal lParam, udtMINMAXINFO, 40& WindowProc = False Exit Function End Select WindowProc = CallWindowProc(procOld, hwnd, iMsg, wParam, lParam) End FunctionPublic Function LockWindow(hwnd As Long, Optional MinWidth As Long, Optional MinHeight As Long, Optional maxwidth As Long, Optional maxheight As Long) As Boolean With udtMMI '指定窗体最小宽度 If MinWidth = 0 Then .ptMinTrackSize.x = 0 Else .ptMinTrackSize.x = MinWidth '指定窗体最小高度 If MinHeight = 0 Then .ptMinTrackSize.y = 0 Else .ptMinTrackSize.y = MinHeight '指定窗体最大宽度 If maxwidth = 0 Then .ptMaxSize.x = Screen.Width \ Screen.TwipsPerPixelX Else .ptMaxSize.x = maxwidth '指定窗体最大高度 If maxheight = 0 Then .ptMaxSize.y = Screen.Width \ Screen.TwipsPerPixelX Else .ptMaxSize.y = maxheight End With procOld = SetWindowLong(hwnd, -4, AddressOf WindowProc) End Function
或者用SetWindowPos设置窗口总在最前:Const HWND_TOPMOST = -1 Const HWND_NOTOPMOST = -2 Const SWP_NOSIZE = &H1 Const SWP_NOMOVE = &H2 Const SWP_NOACTIVATE = &H10 Const SWP_SHOWWINDOW = &H40 Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)Private Sub Form_Activate() 'Set the window position to topmost SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE End Sub
Option Explicit
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As LongPrivate Sub Form_Load()
Dim hTaskBar As Long
hTaskBar = FindWindow("Shell_TrayWnd", vbNullString)
Debug.Print hTaskBar
Dim RC As RECT
Dim i As Long
i = GetWindowRect(hTaskBar, RC)
Dim taskheight As Long
taskheight = RC.Bottom - RC.Top '任务栏高度
i = GetWindowRect(GetDesktopWindow, RC)
Dim maxwidth As Long
Dim maxheight As Long
maxwidth = RC.Right - RC.Left '获取屏幕宽度
maxheight = RC.Bottom - RC.Top - taskheight '屏幕高度-任务栏高度
LockWindow Me.hwnd, , , maxwidth, maxheight
End Sub模块:
Option ExplicitPublic Type POINTAPI
x As Long
y As Long
End TypePublic Type MINMAXINFO
ptReserved As POINTAPI
ptMaxSize As POINTAPI
ptMaxPosition As POINTAPI
ptMinTrackSize As POINTAPI
ptMaxTrackSize As POINTAPI
End TypePublic Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public 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 LongPublic Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const WM_GETMINMAXINFO = &H24
Public Const GWL_WNDPROC = -4Global lpPrevWndProc As Long
Public procOld As Long
Public udtMMI As MINMAXINFO
Public Function WindowProc(ByVal hwnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Select Case iMsg
Case WM_GETMINMAXINFO
Dim udtMINMAXINFO As MINMAXINFO
CopyMemory udtMINMAXINFO, ByVal lParam, 40&
With udtMINMAXINFO
.ptMaxSize.x = udtMMI.ptMaxSize.x
.ptMaxSize.y = udtMMI.ptMaxSize.y
.ptMaxPosition.x = 0
.ptMaxPosition.y = 0
.ptMaxTrackSize.x = .ptMaxSize.x
.ptMaxTrackSize.y = .ptMaxSize.y
.ptMinTrackSize.x = udtMMI.ptMinTrackSize.x
.ptMinTrackSize.y = udtMMI.ptMinTrackSize.y
Debug.Print .ptMaxSize.x & "," & .ptMaxSize.y
End With
CopyMemory ByVal lParam, udtMINMAXINFO, 40&
WindowProc = False
Exit Function
End Select
WindowProc = CallWindowProc(procOld, hwnd, iMsg, wParam, lParam)
End FunctionPublic Function LockWindow(hwnd As Long, Optional MinWidth As Long, Optional MinHeight As Long, Optional maxwidth As Long, Optional maxheight As Long) As Boolean
With udtMMI
'指定窗体最小宽度
If MinWidth = 0 Then .ptMinTrackSize.x = 0 Else .ptMinTrackSize.x = MinWidth
'指定窗体最小高度
If MinHeight = 0 Then .ptMinTrackSize.y = 0 Else .ptMinTrackSize.y = MinHeight
'指定窗体最大宽度
If maxwidth = 0 Then .ptMaxSize.x = Screen.Width \ Screen.TwipsPerPixelX Else .ptMaxSize.x = maxwidth
'指定窗体最大高度
If maxheight = 0 Then .ptMaxSize.y = Screen.Width \ Screen.TwipsPerPixelX Else .ptMaxSize.y = maxheight
End With
procOld = SetWindowLong(hwnd, -4, AddressOf WindowProc)
End Function
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long)Private Sub Form_Activate()
'Set the window position to topmost
SetWindowPos Me.hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
End Sub
Me.Left = 0
Me.Height = Screen.Height - 400(400为开始菜单的高度,在800*600,1024*768下基本都可以,不行自己改改数值)
Me.Width = Screen.Width
这样就解决问题了