作者:绿色风
分类:
时间:2022-08-17
浏览:132
楼主 kevinchengcw |
Q: 如何用vba代码判断窗体最小化事件并执行代码? A: 窗体中代码如下:- Option Explicit
- 'API声明部分*****************************
- Private Declare Function IsIconic Lib "user32 " (ByVal hwnd As Long) As Long
- Private 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 '(最大化)
- '****************************************
- Dim hWndForm As Long '将句柄设置成公共变量,方便其他程序调用
- Private Sub UserForm_Initialize() '初始化时为窗体添加最大化最小化按钮
- 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
- Private Sub UserForm_Resize() '在resize事件里通过api判断是否已经最小化
- If IsIconic(hWndForm) <> 0 Then MsgBox "窗体最小化了" '将此处替换成要执行的代码即可实现最小化时执行指定操作了
- End Sub
附示例文件。 判断窗体最小化并执行代码.rar |
2楼 xyf2210 |
API看来还是要学呀 |
免责声明
有感于原ExcelTip.Net留存知识的价值及部分知识具有的时间限定性因素,
经与ExcelTip.Net站长Apolloh商议并征得其同意,
现将原属ExcelTip.Net的知识帖采集资料于本站点进行展示,
供有需要的人士查询使用,也慰缅曾经的论坛时代。
所示各个帖子的原作者如对版权有异议,
可与本人沟通提出,或于本站点留言,我们会尽快处理。
在此,感谢ExcelTip.Net站长Apolloh的支持,感谢本站点所有人**绿色风(QQ:79664738)**的支持与奉献,特此鸣谢!
------本人网名**KevinChengCW(QQ:1210618015)**原ExcelTip.Net总版主之一