首页 > 其他分享 >Excel VBA 窗体UserForm制作菜单栏与添加窗体最大化最小化功能(转载)

Excel VBA 窗体UserForm制作菜单栏与添加窗体最大化最小化功能(转载)

时间:2023-08-01 13:47:15浏览次数:53  
标签:Function VBA UserForm ByVal Private MF 窗体 Long Declare

窗体

'--------------------------------------------------------
'->Forms
'  Module
'  ClassModules
'--------------------------------------------------------
Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetMenu Lib "user32" (ByVal hWnd As Long, ByVal hMenu As Long) As Long
Private Declare Function CreateMenu Lib "user32" () As Long
Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, ByVal lpNewItem As Any) As Long
Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
Private Declare Function CreatePopupMenu Lib "user32" () 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 Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long

Private Const GWL_WNDPROC = (-4)
Private Const MF_STRING = &H0&
Private Const MF_POPUP = &H10&
Private Const MF_SEPARATOR = &H800&


'Private Declare PtrSafe Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
'Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
'Private Declare PtrSafe Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong 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 MenuWnd As Long, Dump As Long, PopupMenuID As Long, PopupMenuWnd As Long, MenuID As Long


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
   
   '给窗体添加菜单
    If Val(Application.Version) < 9 Then
        hWnd = FindWindow("ThunderXFrame", Me.Caption)
    Else
        hWnd = FindWindow("ThunderDFrame", Me.Caption)
    End If
    MenuWnd = CreateMenu()
    
    PopupMenuID = CreatePopupMenu()
    Dump = AppendMenu(MenuWnd, MF_STRING + MF_POPUP, PopupMenuID, "Setting(&X)")
    Dump = AppendMenu(PopupMenuID, MF_STRING, 100, "save(&S)...")
    Dump = AppendMenu(PopupMenuID, MF_STRING, 101, "backup(&E)")
    Dump = AppendMenu(PopupMenuID, MF_STRING, 102, "Exit(&X)")
    
    PopupMenuID = CreatePopupMenu()
    Dump = AppendMenu(MenuWnd, MF_STRING + MF_POPUP, PopupMenuID, "Review(&P)")
    Dump = AppendMenu(PopupMenuID, MF_STRING, 110, "Record(&L)")
    Dump = AppendMenu(PopupMenuID, MF_STRING, 111, "Review(&C)")
    
    PopupMenuID = CreatePopupMenu()
    Dump = AppendMenu(MenuWnd, MF_STRING + MF_POPUP, PopupMenuID, "Tools(&Z)")
    Dump = AppendMenu(PopupMenuID, MF_STRING, 112, "Tuninghelper(&T)")
    Dump = AppendMenu(PopupMenuID, MF_STRING, 113, "Kgthelper(&J)")
    
    PopupMenuID = CreatePopupMenu()
    Dump = AppendMenu(MenuWnd, MF_STRING + MF_POPUP, PopupMenuID, "Help(&B)")
    Dump = AppendMenu(PopupMenuID, MF_STRING, 114, "help(&F)")
    Dump = AppendMenu(PopupMenuID, MF_STRING, 115, "about(&Y)")
    
    Dump = SetMenu(hWnd, MenuWnd)
    PreWinProc = GetWindowLong(hWnd, GWL_WNDPROC)
    SetWindowLong hWnd, GWL_WNDPROC, AddressOf MsgProcess
End Sub


Private Sub UserForm_Terminate()
    DestroyMenu MenuWnd
    DestroyMenu PopupMenuID
    DestroyMenu PopupMenuWnd
    SetWindowLong hWnd, GWL_WNDPROC, PreWinProc
End Sub




模块

'--------------------------------------------------------
'  Forms
'->Module
'  ClassModules
'--------------------------------------------------------
Public PreWinProc As Long, hWnd As Long

Public Declare Function CheckMenuRadioItem Lib "user32" (ByVal hMenu As Long, ByVal un1 As Long, ByVal un2 As Long, ByVal un3 As Long, ByVal un4 As Long) As Long
Public Declare Function CheckMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDCheckItem As Long, ByVal wCheck As Long) As Long
Public Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As Long, ByVal wIDEnableItem As Long, ByVal wEnable As Long) As Long
Public Const MF_UNCHECKED = &H0&
Public Const MF_CHECKED = &H8&
Public Const MF_DISABLED = &H2&
Public Const MF_GRAYED = &H1&
Public Const MF_ENABLED = &H0&

Private 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 Long
Private Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetSubMenu Lib "user32" (ByVal hMenu As Long, ByVal nPos As Long) As Long
Private Const MF_BYCOMMAND = &H0&



Public Function MsgProcess(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Dim SubMenu_hWnd As Long
    Select Case wParam
        Case 100
            MsgBox "YourChoose: Save Button"
        Case 101
            MsgBox "YourChoose: Backup Buttion"
        Case 102
            Unload UserForm1
        Case 110
            MsgBox "YourChoose: Record Button"
        Case 111
            MsgBox "YourChoose: Review Button"
        Case 112
            MsgBox "YourChoose: Tuninghelper Button"
        Case 113
            MsgBox "YourChoose: Kgthelper Button"
        Case 114
            MsgBox "YourChoose: help Button"
        Case 115
            MsgBox "YourChoose: about Button"
        Case Else
            MsgProcess = CallWindowProc(PreWinProc, hWnd, Msg, wParam, lParam)
    End Select
End Function

  

标签:Function,VBA,UserForm,ByVal,Private,MF,窗体,Long,Declare
From: https://www.cnblogs.com/luoye00/p/17596214.html

相关文章

  • access VBA 当变量会被当成参数进行传递时,最好不要在声明过程偷懒
    Dima,b,cAsString不用讲,大家都这么玩过.实际上,它只将c声明为String类型,而a和b实际上是未声明的Variant类型。然后,当你尝试将未声明类型的变量a作为参数传递给ByRef参数的函数时,会导致类型不匹配的错误。SubMySub(ByRefxAsString)’函数代码EndSubMy......
  • VBA利用transform函数和ADO实现交叉汇总
    VBA中transform函数基本语法:Createsacrosstabquery.SyntaxTRANSFORMaggfunctionselectstatementPIVOTpivotfield[IN(value1[,value2[,...]])]TheTRANSFORMstatementhastheseparts: 假设存在三列数据:受理人、受理日期、业务量,怎么变成表:受理人、(按照受......
  • 浅谈Excel开发:七 Excel 自定义任务窗体
    前面花了三篇文章讲解了Excel中的UDF函数,RTD函数和异步UDF函数,这些都是Excel开发中的重中之重。本文现在开始接着第二篇文章的菜单系统开始讲解Excel中可供开发的界面元素,本文要讲解的是Excel中的自定义任务面板(CustomeTaskPanel,CTP)。自定义任务面板在Office2003中就引入了......
  • 转载:PageOffice在线打开office文件通过js调用vba可实现的功能
    pageoffice封装的js接口有限,某些比较复杂的设置用到的客户不多,所以没有提供直接的js方法,但是pageoffice提供了Document属性和RunMacro方法,可以调vba或直接运行宏指令实现比较小众的一些需求   Word相关功能1、给word表格中的单元格赋值document.getElementById("Page......
  • python 根据句柄获取窗体截图
    Python根据句柄获取窗体截图概述在开发过程中,我们经常会遇到需要获取窗体截图的需求。本文将教你如何使用Python根据句柄获取窗体截图。步骤下面是整个流程的步骤:步骤描述步骤1导入相关模块步骤2获取窗体句柄步骤3获取窗体位置和大小步骤4根据窗体位置......
  • vba-常用代码记录
    一些代码优化:1.Range("B5:C6").CopyDestination:=Range("B8")2.使用mid$函数而不是mid3.把整个sheet保护,需要操作时,后台先关闭保护。4.Excel中自带的Sum函数是将整个区域转换为数组,在内存中进行求和,速度快多了。DimresultAsDoubleresult=Application.Worksh......
  • .Net 项目类型区别 Windows窗体应用程序
    .NET框架(特定于Windows),然后使用WindowsFormsApp(.NETFramework)。Windows窗体应用程序(.NETFramework) 解决方案默认名称:windowsFormsApp.NET核心/.NET(跨平台),然后使用WindowsFormsApp。Windows窗体应用程序  解决方案默认名称:winFormsApp......
  • 基于C#的无边框窗体动画效果的完美解决方案 - 开源研究系列文章
          最近在整理和编写基于C#的WinForm应用程序,然后碰到一个其他读者也可能碰到的问题,就是C#的Borderless无边框窗体的动画效果问题。      在VisualStudio2022里,C#的WinForm程序提供了Borderless无边框窗体的样式效果,但是它没提供在无边框窗体下,窗体的载入、最......
  • access 在数据表窗体下设置下拉菜单的宽度
     如上图所示,下拉框明显宽度不够,右边的数据都没展现出来.窗口模式下是不能设置宽度值的.解决的方案是在设计模式下,给"产品编码"这个字段设置一个合理的宽度值,便数据全部展现出来 然后回到数据视图下,发现数据已经能正常显示了.注意,此时,当字段的宽度小于下拉菜单的宽度......
  • c#、winfrom在一个窗体中鼠标双击datagridview1选中某行,将其选中的行的所有数据在data
    效果展示:代码逻辑:首先在datagridview1中按条件查询数据,然后在datagridview2在查询和datagridview1中一样的Select语句,只不过在datagridview2的查询语句中需添加where条件获取datagridview1在选中行的id,在datagridview2显示就好了代码:单据筛选按钮 privatevoidbutton1_Click......