VB中窗體操作.

  1. '
  2. '窗口操作(SmWinCtrl)
  3. '
  4. Option Explicit
  5. Private Const HWND_TOP = 0
  6. Private Const HWND_BOTTOM = 1
  7. Private Const HWND_TOPMOST = -1
  8. Private Const HWND_NOTOPMOST = -2
  9. Private Const SWP_NOSIZE = 
  10. Private Const SWP_NOMOVE = 
  11. Private Const SWP_NOZORDER = 
  12. Private Const SWP_NOREDRAW = 
  13. '/*以下常數在 ShowWindow中應用*
  14. Private Const SW_SHOWMAXIMIZED = 3
  15. Private Const SW_HIDE = 0
  16. Private Const SW_MINIMIZE = 6
  17. Private Const SW_RESTORE = 9
  18. Private Const SW_SHOW = 5
  19. Private Const SW_SHOWMINIMIZED = 2
  20. Private Const SW_SHOWMINNOACTIVE = 7
  21. Private Const SW_SHOWNA = 8
  22. Private Const SW_SHOWNOACTIVATE = 4
  23. Private Const SW_SHOWNORMAL = 1
  24. '/====================================
  25. '/隱藏窗口,活動狀態給令一個窗口
  26. '/  SW_HIDE
  27. '/  最小化窗口,活動狀態給令一個窗口
  28. '/  SW_MINIMIZE
  29. '/ 用原來的大小和位置顯示一個窗口,同時令其進入活動狀態
  30. '/  SW_RESTORE
  31. '/用當前的大小和位置顯示一個窗口,同時令其進入活動狀態
  32. '/  SW_SHOW
  33. '/  最大化窗口,並將其激活
  34. '/ SW_SHOWMAXIMIZED
  35. '/  最小化窗口,並將其激活
  36. '/  SW_SHOWMINIMIZED
  37. '/  最小化一個窗口,同時不改變活動窗口
  38. '/  SW_SHOWMINNOACTIVE
  39. '/  用當前的大小和位置顯示一個窗口,不改變活動窗口
  40. '/ SW_SHOWNA
  41. '/ 用最近的大小和位置顯示一個窗口,同時不改變活動窗口
  42. '/ SW_SHOWNOACTIVATE
  43. '/  與SW_RESTORE相同
  44. '/SW_SHOWNORMAL
  45. Private Const xMenuID = 10
  46. Private Const WM_NCACTIVATE = 
  47. '/********************************************
  48. '/MENU ID
  49. Private Const SC_RESTORE = &HF120&  '還原
  50. Private Const SC_MOVE = &HF010& '移動
  51. Private Const SC_SIZE = &HF000& '大小
  52. Private Const SC_MINIMIZE = &HF020& '縮到最小
  53. Private Const SC_MAXIMIZE = &HF030& '放到最大
  54. Private Const SC_CLOSE = &HF060& '關閉
  55. '/********************************************
  56. Private Const MIIM_STATE = &H1
  57. Private Const MIIM_ID = &H2
  58. Private Const MFS_GRAYED = &H3
  59. Private Const MFS_CHECKED = &H8
  60. Private Type MENUITEMINFO
  61.     cbSize As Long
  62.     fMask As Long
  63.     fType As Long
  64.     fState As Long
  65.     Wid As Long
  66.     hSubMenu As Long
  67.     hbmpChecked As Long
  68.     hbmpUnchecked As Long
  69.     dwItemData As Long
  70.     dwTypeData As String
  71.     cch As Long
  72. End Type
  73. Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As LongByVal bRevert As LongAs Long
  74. Private Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu As LongByVal un As LongByVal B As Boolean, lpMenuItemInfo As MENUITEMINFO) As Long
  75. Private Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu As LongByVal un As LongByVal bool As Boolean, lpcMenuItemInfo As MENUITEMINFO) As Long
  76.     
  77. '/--------------------------------------------------
  78. '/爲窗口指定一個新位置和狀態
  79. Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, _
  80.                                          ByVal hWndInsertAfter As Long, _
  81.                                          ByVal X As LongByVal Y As Long, _
  82.                                          ByVal cx As LongByVal cy As Long, _
  83.                                          ByVal wFlags As LongAs Long
  84. '/控制窗口的可見性
  85. Private Declare Function ShowWindow Lib "user32" _
  86.                               (ByVal hWnd As Long, _
  87.                               ByVal nCmdShow As LongAs Long
  88. '/恢復一個最小化的程序,並將其激活
  89. Private Declare Function OpenIcon Lib "user32" (ByVal hWnd As LongAs Long
  90. '/將輸入焦點移到指定的窗體(必須與調用者同一線程)
  91. Private Declare Function SetFocus Lib "user32" (ByVal hWnd As LongAs Long
  92. '/激活指定窗口(必須與調用者同一線程)
  93. Private Declare Function SetActiveWindow Lib "user32" (ByVal hWnd As LongAs Long
  94. '/獲取前臺窗口或者聚焦窗口的句柄
  95. Private Declare Function GetForegroundWindow Lib "user32" () As Long
  96. '/===============================================================================
  97. '/取窗口標題
  98. Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
  99.                                                      (ByVal hWnd As Long, _
  100.                                                      ByVal lpString As String, _
  101.                                                      ByVal cch As LongAs Long
  102. '/設置窗口標題
  103. Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" _
  104.                                                       (ByVal hWnd As Long, _
  105.                                                        ByVal lpString As StringAs Long
  106. '/將窗口放在前臺
  107. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As LongAs Long
  108. '/向窗口發送消息
  109. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
  110.                             (ByVal hWnd As LongByVal wMsg As Long, _
  111.                             ByVal wParam As Long, lParam As Any) As Long
  112. '/----------------------------------------------------------------------
  113. '/關閉一個窗口
  114. '/HWND 要關閉的窗口的句柄
  115. '/返回值  如果非零,則成功,否則失敗
  116. Private Declare Function DestroyWindow Lib "user32" (ByVal hWnd As LongAs Long
  117. '/爲窗體指定一個新父
  118. '/自身的HWND,新父的HWND.
  119. Private Declare Function SetParent Lib "user32" (ByVal HwndChild As LongByVal HWndNewParent As LongAs Long
  120. '
  121. '設置窗口爲最頂層
  122. '函數:SetTopWindow
  123. '參數:Winwnd 要設置爲最頂層窗口的HWND
  124. '返回值:
  125. '例子:
  126. Public Function SetTopWindow(WinWnd As Long)
  127.        SetWindowPos WinWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
  128. End Function
  129. Public Function MoveWindow(WinHwnd As Long, X As Long, Y As Long)
  130.        SetWindowPos WinHwnd, HWND_NOTOPMOST, X, Y, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
  131. End Function
  132. '
  133. '將窗口位於Z軸方向的最前.
  134. '函數:SetTopWindowZ
  135. '參數:MWinwnd 要設置爲Z軸最前窗口的HWND
  136. '返回值:
  137. '例子:
  138. Public Function SetTopWindowZ(MWinWnd As Long)
  139.   Dim WinHwnd As Long
  140.   WinHwnd = GetForegroundWindow()
  141.   If WinHwnd <> MWinWnd And WinHwnd <> 0 Then
  142.      SetWindowPos WinHwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
  143.   End If
  144. End Function
  145. '
  146. '將最頂層窗口恢復爲正常
  147. '函數:NoTopWindow
  148. '參數:Winwnd 要恢復窗口的HWND
  149. '返回值:
  150. '例子:
  151. Public Function NoTopWindow(WinWnd As Long)
  152.        SetWindowPos WinWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE Or SWP_NOMOVE
  153. End Function
  154. '
  155. '將焦點移到指定的窗體上
  156. '函數:SetWinFocus
  157. '參數:hWnd 操作窗口HWND
  158. '返回值:
  159. '例子:
  160. Public Sub SetWinFocus(hWnd As Long)
  161.        Call SetFocus(hWnd)
  162. End Sub
  163. '
  164. '無關閉按鈕.
  165. '函數:NoClose
  166. '參數:FHwnd 操作窗口HWND
  167. '返回值:
  168. '例子:
  169. Public Function NoClose(FHwnd As Long)
  170.     Dim hMenu As Long, MII As MENUITEMINFO
  171.     
  172.     hMenu = GetSystemMenu(FHwnd, 0)
  173.     MII.cbSize = Len(MII)
  174.     MII.dwTypeData = String(80, 0)
  175.     MII.cch = Len(MII.dwTypeData)
  176.     MII.fMask = MIIM_STATE
  177.     MII.Wid = SC_CLOSE
  178.     GetMenuItemInfo hMenu, SC_CLOSE, False, MII
  179.     MII.Wid = xMenuID
  180.     MII.fMask = MIIM_ID
  181.     SetMenuItemInfo hMenu, SC_CLOSE, False, MII
  182.     MII.fState = MII.fState Or MFS_GRAYED
  183.     MII.fMask = MIIM_STATE
  184.     SetMenuItemInfo hMenu, MII.Wid, False, MII
  185.     SendMessage FHwnd, WM_NCACTIVATE, TrueByVal 0
  186. End Function
  187. '
  188. '無最小化按鈕
  189. '函數:NoMin
  190. '參數:FHwnd 操作窗口HWND
  191. '返回值:
  192. '例子:
  193. Public Function NoMin(FHwnd As Long)
  194.     Dim hMenu As Long, MII As MENUITEMINFO
  195.     hMenu = GetSystemMenu(FHwnd, 0)
  196.     MII.cbSize = Len(MII)
  197.     MII.dwTypeData = String(80, 0)
  198.     MII.cch = Len(MII.dwTypeData)
  199.     MII.fMask = MIIM_STATE
  200.     MII.Wid = SC_MINIMIZE
  201.     GetMenuItemInfo hMenu, SC_MINIMIZE, False, MII
  202.     MII.Wid = xMenuID
  203.     MII.fMask = MIIM_ID
  204.     SetMenuItemInfo hMenu, SC_MINIMIZE, False, MII
  205.     MII.fState = MII.fState Or MFS_GRAYED
  206.     MII.fMask = MIIM_STATE
  207.     SetMenuItemInfo hMenu, MII.Wid, False, MII
  208.     SendMessage FHwnd, WM_NCACTIVATE, TrueByVal 0
  209. End Function
  210. '
  211. '無最大化按鈕.
  212. '函數:NoMax
  213. '參數:FHwnd 操作窗口HWND
  214. '返回值:
  215. '例子:
  216. Public Function NoMax(FHwnd As Long)
  217.     Dim hMenu As Long, MII As MENUITEMINFO
  218.     hMenu = GetSystemMenu(FHwnd, 0)
  219.     MII.cbSize = Len(MII)
  220.     MII.dwTypeData = String(80, 0)
  221.     MII.cch = Len(MII.dwTypeData)
  222.     MII.fMask = MIIM_STATE
  223.     MII.Wid = SC_RESTORE
  224.     GetMenuItemInfo hMenu, SC_RESTORE, False, MII
  225.     MII.Wid = xMenuID
  226.     MII.fMask = MIIM_ID
  227.     SetMenuItemInfo hMenu, SC_RESTORE, False, MII
  228.     MII.fState = MII.fState Or MFS_GRAYED
  229.     MII.fMask = MIIM_STATE
  230.     SetMenuItemInfo hMenu, MII.Wid, False, MII
  231.     SendMessage FHwnd, WM_NCACTIVATE, TrueByVal 0
  232. End Function
  233. '
  234. '爲窗口指定一個新父窗口
  235. '函數:SetNewParent
  236. '參數:HwndChild 操作窗口的句柄,HWndNewParent 新父窗口的句柄.
  237. '返回值:
  238. '例子:
  239. Public Function SetNewParent(HwndChild As Long, HWndNewParent As Long)
  240.        SetParent HwndChild, HWndNewParent
  241. End Function
  242. '
  243. '取焦點窗口句柄
  244. '函數:GetTopWinHwnd
  245. '參數:
  246. '返回值:窗口句柄.
  247. '例子:
  248. Public Function GetTopWinHwnd() As Long
  249.        GetTopWinHwnd = GetForegroundWindow()
  250. End Function
  251. '
  252. '激活指定窗口
  253. '函數:GetTopWinHwnd
  254. '參數:WinHwnd 操作窗口句柄.
  255. '返回值:
  256. '例子:
  257. Public Function SetActWin(WinHwnd As Long)
  258.        SetFocus WinHwnd
  259. End Function
  260. '
  261. '返回窗口標題
  262. '函數:GetWinLab
  263. '參數:WinHwnd 操作窗口句柄.
  264. '返回值:String,窗口標題.
  265. '例子:
  266. Public Function GetWinLab(WinHwnd As LongAs String
  267.     Dim MyStr As String
  268.     MyStr = String(255, Chr$(0))
  269.     GetWindowText WinHwnd, MyStr, 255
  270.     MyStr = Left$(MyStr, InStr(MyStr, Chr$(0)) - 1)
  271.     GetWinLab = MyStr
  272. End Function
  273. '
  274. '關閉一個窗口
  275. '函數:CloseWin
  276. '參數:WinHwnd 操作窗口句柄.
  277. '返回值:
  278. '例子:
  279. Public Function CloseWin(WinHwnd As Long)
  280.        DestroyWindow WinHwnd
  281. End Function
  282. Private Sub Class_Initialize()
  283.     Dim T As New ClsRev
  284.     Call T.GetIniVal
  285.     Set T = Nothing
  286. End Sub
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章