明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3931|回复: 6

[推荐]为VBA窗体添加最大化,最小化,图标

[复制链接]
发表于 2008-11-3 20:36:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2008-11-3 20:40:07 编辑
  1. '/                                     /'
  2. '///////////////////////////////////////'
  3. '
  4. 'Written: Jan. 30, 2007
  5. 'Author: Leith Ross
  6. 'Returns an Icon from a File (.ico)
  7.  Private Declare Function LoadImage _
  8.   Lib "user32.dll" _
  9.    Alias "LoadImageA" _
  10.     (ByVal hInst As Long, _
  11.      ByVal lpsz As String, _
  12.      ByVal uType As Long, _
  13.      ByVal cxDesired As Long, _
  14.      ByVal cyDesired As Long, _
  15.      ByVal fuLoad As Long) As Long
  16. 'Direct System what to do with the Window
  17.  Private Declare Function SendMessage _
  18.   Lib "user32.dll" _
  19.    Alias "SendMessageA" _
  20.     (ByVal hWnd As Long, _
  21.      ByVal wMsg As Long, _
  22.      ByVal wParam As Long, _
  23.      lParam As Long) As Long
  24. 'Constants for SendMessage
  25.  Const WM_GETICON As Long = &H7F
  26.  Const WM_SETICON As Long = &H80
  27.  Const ICON_SMALL As Long = &H0
  28.  Const ICON_BIG As Long = &H1
  29. 'Constants for Load Image's fuLoad Parameter (Load Resource)
  30.  Const LR_DEFAULTCOLOR As Long = &H0
  31.  Const LR_MONOCHROME As Long = &H1
  32.  Const LR_COLOR As Long = &H2
  33.  Const LR_COPYRETURNORG As Long = &H4
  34.  Const LR_COPYDELETEORG As Long = &H8
  35.  Const LR_LOADFROMFILE As Long = &H10
  36.  Const LR_LOADTRANSPARENT As Long = &H20
  37.  Const LR_DEFAULTSIZE As Long = &H40
  38.  Const LR_VGACOLOR As Long = &H80
  39.  Const LR_LOADMAP3DCOLORS As Long = &H1000
  40.  Const LR_CREATEDIBSECTION As Long = &H2000
  41.  Const LR_COPYFROMRESOURCE As Long = &H4000
  42.  Const LR_SHARED As Long = &H8000
  43. 'Constants for Load Image's uType Parameter
  44.  Const IMAGE_BITMAP As Long = &H0
  45.  Const IMAGE_ICON As Long = &H1
  46.  Const IMAGE_CURSOR As Long = &H2
  47. 'Constants for ShowWindow (nCmdShow)
  48.  Const SW_HIDDEN As Long = 0
  49.  Const SW_NORMAL As Long = 1
  50.  Const SW_MINIMIZED As Long = 2
  51.  Const SW_MAXIMIZED As Long = 3
  52.  Const SW_NOTACTIVE As Long = 4
  53.  Const SW_UNHIDDEN As Long = 5
  54.  Const SW_MINWITHFOCUS As Long = 6
  55.  Const SW_MINNOTACTIVE As Long = 7
  56.  Const SW_RESTORE As Long = 9
  57. 'Constants for GetWindow
  58.  Const GW_HWNDFIRST As Long = &H0
  59.  Const GW_HWNDLAST As Long = &H1
  60.  Const GW_HWNDNEXT As Long = &H2
  61.  Const GW_HWNDPREV As Long = &H3
  62.  Const GW_OWNER As Long = &H4
  63.  Const GW_CHILD As Long = &H5
  64. 'Window Style constants
  65.  Const WS_DISABLE As Long = 0
  66.  Const WS_MAXIMIZEBOX As Long = &H10000
  67.  Const WS_MINIMIZEBOX As Long = &H20000
  68.  Const WS_THICKFRAME As Long = &H40000    'Style to add a sizable frame
  69.  Const WS_SYSMENU As Long = &H80000
  70.  Const WS_ENABLE As Long = &HFFFFFFFF
  71.  
  72. 'Get Window Long constants
  73.  Const GWL_HINSTANCE As Long = (-6)
  74.  Const GWL_HWNDPARENT As Long = (-8)
  75.  Const GWL_ID As Long = (-12)
  76.  Const GWL_STYLE As Long = (-16)
  77.  Const GWL_EXSTYLE As Long = (-20)
  78. Private Declare Function GetWindowLong _
  79.   Lib "user32.dll" _
  80.    Alias "GetWindowLongA" _
  81.     (ByVal hWnd As Long, ByVal nIndex As Long) As Long
  82.                
  83.  Private Declare Function SetWindowLong _
  84.   Lib "user32.dll" _
  85.    Alias "SetWindowLongA" _
  86.     (ByVal hWnd As Long, _
  87.      ByVal nIndex As Long, _
  88.      ByVal dwNewLong As Long) As Long
  89. 'Function to Change how Window is Displayed
  90.  Private Declare Function ShowWindow _
  91.   Lib "user32.dll" _
  92.    (ByVal hWnd As Long, _
  93.     ByVal nCmdShow As Long) As Long
  94. 'Returns the Window Handle of the Active Window
  95.  Private Declare Function GetActiveWindow _
  96.   Lib "user32.dll" () As Long
  97. 'Redraw the Icons on the Window's Title Bar
  98.  Private Declare Function DrawMenuBar _
  99.   Lib "user32.dll" _
  100.    (ByVal hWnd As Long) As Long
  101. Public Sub MinimizeWindow(Optional ByVal Window_Handle As Long, Optional ByVal With_Focus As Boolean)
  102.  
  103.  Dim RetVal
  104.  
  105.   If With_Focus = True Then
  106.     RetVal = ShowWindow(Window_Handle, SW_MINWITHFOCUS)
  107.   Else
  108.     RetVal = ShowWindow(Window_Handle, SW_MINNOTACTIVE)
  109.   End If
  110.  
  111. End Sub
  112. Public Sub RestoreWindow(Optional ByVal Window_Handle As Long)
  113.  
  114.  Dim RetVal
  115.  
  116.   RetVal = ShowWindow(Window_Handle, SW_NORMAL)
  117. End Sub
  118. Public Sub AddMinBox(Optional Window_Handle As Long)
  119.  Dim hWnd As Long
  120.  Dim BitMask As Long
  121.  Dim WindowStyle As Long
  122.    If Window_Handle = 0 Then
  123.       hWnd = GetActiveWindow()
  124.    Else
  125.       hWnd = Window_Handle
  126.    End If
  127.  
  128.    WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
  129.    BitMask = WindowStyle Or WS_MINIMIZEBOX
  130.  
  131.    Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
  132.    Call DrawMenuBar(hWnd)
  133. End Sub
  134. Public Sub AddMaxBox(Optional Window_Handle As Long)
  135.  Dim hWnd As Long
  136.  Dim BitMask As Long
  137.  Dim WindowStyle As Long
  138.    If Window_Handle = 0 Then
  139.       hWnd = GetActiveWindow()
  140.    Else
  141.       hWnd = Window_Handle
  142.    End If
  143.  
  144.    WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
  145.    BitMask = WindowStyle Or WS_MAXIMIZEBOX
  146.    Call SetWindowLong(hWnd, GWL_STYLE, BitMask)
  147.    Call DrawMenuBar(hWnd)
  148. End Sub
  149.      
  150. Public Function ChangeIcon(ByVal Icon_File_Path As String, Optional ByVal Window_Handle As Long)
  151.   Dim hWnd As Long
  152.   Dim hIcon As Long
  153.   Dim LoadMask As Long
  154.     If Window_Handle = 0 Then
  155.        hWnd = GetActiveWindow()
  156.     Else
  157.        hWnd = Window_Handle
  158.     End If
  159.     
  160.      LoadMask = LR_LOADFROMFILE Or LR_DEFAULTSIZE Or LR_SHARED
  161.      hIcon = LoadImage(0&, Icon_File_Path, IMAGE_ICON, 32, 32, LoadMask)
  162.      Call SendMessage(hWnd, WM_SETICON, ICON_BIG, ByVal hIcon)
  163.      Call DrawMenuBar(hWnd)
  164. End Function
  165. Private Sub UserForm_Activate()
  166.     AddMinBox
  167.     AddMaxBox
  168.     ChangeIcon "C:\ndpsetup.ico"    '图标路径
  169.     
  170. End Sub

评分

参与人数 1威望 +1 收起 理由
兰州人 + 1 【好评】 使用了API技术

查看全部评分

发表于 2008-11-4 19:23:00 | 显示全部楼层
没想到还比较麻烦.
发表于 2008-11-4 21:53:00 | 显示全部楼层
不错!!!!!!!!!!
发表于 2008-11-5 11:00:00 | 显示全部楼层
用了API技术
发表于 2008-11-7 08:29:00 | 显示全部楼层
很好,谢谢分享!
发表于 2008-11-8 16:04:00 | 显示全部楼层
程序很好,支持了!!!
发表于 2010-6-14 16:55:00 | 显示全部楼层
不能运行啊,运行后没反应
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 22:26 , Processed in 0.163370 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表