在窗体代码中加上以上这段,ok搞定!
Option ExplicitPrivate Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const WS_THICKFRAME As Long = &H40000 ’(恢复大小)
Private Const WS_MINIMIZEBOX As Long = &H20000 ’(最小化)
Private Const WS_MAXIMIZEBOX As Long = &H10000 ’(最大化)
Private Sub UserForm_Initialize()
Dim hWndForm As Long
Dim IStyle As Long
hWndForm = FindWindow("ThunderDFrame", Me.Caption)
IStyle = GetWindowLong(hWndForm, GWL_STYLE)
IStyle = IStyle Or WS_THICKFRAME ’还原
IStyle = IStyle Or WS_MINIMIZEBOX ’最小化
IStyle = IStyle Or WS_MAXIMIZEBOX ’最大化
SetWindowLong hWndForm, GWL_STYLE, IStyle
End Sub
这个问题难点在于窗体状态的判断上,至于DoCmd.Minimize只能作用于当前窗体其实好办,将需要最小化的窗体设置为当前窗体后就可以使用了。由于ACCESS VBA没有WindowState属性,因此要判断窗体是否处于最小化状态我们需要借助WINDOWS API函数。
下面是具体解决方案:
1)新建一个模块,取名“模块1“
将下列代码复制黏贴到该模块之下
Option Compare Database
'声明API函数
Declare Function apiIsIconic Lib "user32" Alias "IsIconic" (ByVal hwnd As Long) As Long
Declare Function apiIsZoomed Lib "user32" Alias "IsZoomed" (ByVal hwnd As Long) As Long
'编写下列自定义函数
Private Function IsAccMaxFrom(frm As Form) As Boolean
'此函数为中间过渡函数,判断ACCESS窗体是否处于最大化状态,
'供下面自定义函数GetWindowState 调用
If apiIsZoomed(frm.hwnd) = 0 Then
IsAccMaxFrom = False
Else
IsAccMaxFrom = True
End If
End Function
Private Function IsAccMinForm(frm As Form) As Boolean
'此函数中间过渡函数,判断ACCESS窗体是否处于最小化状态,
'供下面自定义函数GetWindowState 调用
If apiIsIconic(frm.hwnd) = 0 Then
IsAccMinForm = False
Else
IsAccMinForm = True
End If
End Function
Public Function GetWindowState(frm As Form) As Integer
'此函数判断ACCESS窗体处于何种状态,供需要时调用
'函数返回0,表示窗体处于正常状态
'函数返回1,表示窗体处于最小化状态
'函数返回2,表示窗体处于最大化状态
If IsAccMinForm(frm) = True Then
GetWindowState = 1
Exit Function
ElseIf IsAccMaxFrom(frm) = True Then
GetWindowState = 2
Exit Function
Else
GetWindowState = 0
End If
End Function
2)假设A、B两个窗体名分别为“窗体1” 和“窗体2” 。为“窗体1”的命令按钮“COMMAND8”编写下列单击事件过程
Private Sub Command8_Click()
If CurrentProject.AllForms("窗体2").IsLoaded = True Then
If GetWindowState(Forms!窗体2) = 1 Then
'窗体处于最小化时,则复原
DoCmd.SelectObject acForm, "窗体2"
DoCmd.Restore
Else
'窗体不处于最小化状态时,则最小化
DoCmd.SelectObject acForm, "窗体2"
DoCmd.Minimize
End If
Else
MsgBox "窗体尚未打开" ''如果窗体尚未打开则予以提示
End If
End Sub
'如果要求在“窗体2”还没又打开时,强制打开窗体并最小化,则代码可以改写如下
Private Sub Command9_Click()
If CurrentProject.AllForms("窗体2").IsLoaded = True Then
If GetWindowState(Forms!窗体2) = 1 Then
DoCmd.SelectObject acForm, "窗体2" '将窗体2设置为当前窗体
DoCmd.Restore
Else
DoCmd.SelectObject acForm, "窗体2"
DoCmd.Minimize
End If
Exit Sub
End If
'窗体2尚未打开时强制打开窗体并最小化
DoCmd.OpenForm "窗体2"
DoCmd.Minimize
End Sub
好了,到此大功告成。如有疑问,可以私信给我。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)