VB程序运行时,把窗口最大化后,窗口是不能被调整大小的(最小化除外),在程序中也不行。
设计时,可以正常窗口呈现,并屏蔽窗体的最大化按钮,自己做个替代的“最大化”按钮,实现窗体放大。当点击“最大化”时,触发Form的ReSize事件,在Form的ReSize事件中,写入:
Private Sub Form_Resize()
Form1.Left = 0
Form1.Top = 0
Form1.Width = Screen.Width
Form1.Height = Screen.Height - GetTaskbarHeight
End Sub
(当程序启动时,也会同时触发Form_ReSize的)
其中,GetTaskbarHeight的获取要写进模块文件中:
Public Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Public Const SPI_GETWORKAREA = 48
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Function GetTaskbarHeight() As Integer
Dim lRes As Long
Dim rectVal As RECT
lRes = SystemParametersInfo(SPI_GETWORKAREA, 0, rectVal, 0)
GetTaskbarHeight = ((Screen.Height / Screen.TwipsPerPixelX) -rectVal.Bottom) * Screen.TwipsPerPixelX
End Function
但是,这样做还要考虑到有些人把任务栏移到其他地方的,比如左侧,右侧,甚至隐藏,所以,也是很麻烦的哦
用API获取当前窗口的分辨率,自动调整窗体。
有三种方法可行:
1、将任务栏设置为自动隐藏;
2、将窗体大小调整为适应大小,当然要考虑任务栏的大小;
3、将屏幕分辨率调大,这样你的窗体最大化不会被遮住。
获取屏幕宽度和高度:
Form1.Width = Screen.Width
Form1.Height = Screen.Height
'如下代码,不论任务栏在何处,也不管高度和宽度,只要单击窗体,都能达到你想要的效果。
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Const SPI_GetWorkArea = 48
Private Type Rect
Left As Long
Top As Long
Width As Long 'Right
Height As Long 'Bottom
End Type
Private Sub Form_Click()
Dim nRect As Rect
Call ScreenWorkArea(nRect)
Me.WindowState = 0
Me.Move nRect.Left, nRect.Top, nRect.Width, nRect.Height
End Sub
Private Sub ScreenWorkArea(nRect As Rect)
Dim dl As Long
dl = SystemParametersInfo(SPI_GetWorkArea, 0, nRect, 0)
'工作区:除开任务栏外的区域
nRect.Width = (nRect.Width - nRect.Left) * Screen.TwipsPerPixelX '工作区宽度
nRect.Height = (nRect.Height - nRect.Top) * Screen.TwipsPerPixelY '工作区高度
nRect.Left = nRect.Left * Screen.TwipsPerPixelX
nRect.Top = nRect.Top * Screen.TwipsPerPixelY
End Sub