201206141723UserForm的最大化、最小化、正常化與全螢幕

版本更新: (2012.06.14更新) 

更新內容:新增全螢幕,按ESC鍵則unload userform。

下載範例檔案


Excel 的UserForm所提供的sizing非常死板,不方便動態改變其大小。因此需要自行程式控制。
以下的代碼就讓你的UserForm大小的變化十分具有彈性:

Private Declare Function SetActiveWindow 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 SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong 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 ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const GWL_STYLE = (-16)
Private Const SW_SHOWMAXIMIZED = 3
Private Const SW_SHOWNORMAL = 1
Private Const SW_SHOWMINIMIZED = 2
Private Const WS_THICKFRAME = &H40000
 
Dim hWndForm As Long
Dim IStyle As Long

 
Private Sub UserForm_Initialize()
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

 
Public Sub myShowMin()
SetActiveWindow hWndForm
ShowWindow hWndForm, SW_SHOWMINIMIZED
End Sub

 
Public Sub myShowNor()
SetActiveWindow hWndForm
ShowWindow hWndForm, SW_SHOWNORMAL
End Sub

 
Public Sub myShowMax()
SetActiveWindow hWndForm
ShowWindow hWndForm, SW_SHOWMAXIMIZED
End Sub
回應
關鍵字
    沒有新回應!
累積 | 今日
loading......