乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 115|回复: 6

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

[复制链接]

124

主题

837

帖子

9

银币

顶梁支柱

Rank: 50Rank: 50

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

使用道具 举报

18

主题

150

帖子

11

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
220
发表于 2008-11-4 19:23:00 | 显示全部楼层
没想到还比较麻烦.
回复

使用道具 举报

1

主题

5

帖子

1

银币

初来乍到

Rank: 1

铜币
9
发表于 2008-11-4 21:53:00 | 显示全部楼层
不错!!!!!!!!!!
回复

使用道具 举报

120

主题

326

帖子

7

银币

中流砥柱

Rank: 25

铜币
806
发表于 2008-11-5 11:00:00 | 显示全部楼层
用了API技术
回复

使用道具 举报

15

主题

70

帖子

2

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
130
发表于 2008-11-7 08:29:00 | 显示全部楼层
很好,谢谢分享!
回复

使用道具 举报

10

主题

134

帖子

8

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
174
发表于 2008-11-8 16:04:00 | 显示全部楼层
程序很好,支持了!!!
回复

使用道具 举报

0

主题

9

帖子

5

银币

初来乍到

Rank: 1

铜币
9
发表于 2010-6-14 16:55:00 | 显示全部楼层
不能运行啊,运行后没反应
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-7-1 13:04 , Processed in 0.586064 second(s), 67 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表