返回列表 上一主題 發帖

[發問] Userform最大化(且內容比例自動放大?)

我也第一次用ZOOM
發現它有缺陷, 寬高只能同放大或縮小, 不能一邊放大另一邊縮小
只好遷就它

// ================

Dim Form_First_wh
Dim Form_Last_wh

   
    Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    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
    Const SM_CXFULLSCREEN = 16
    Const SM_CYFULLSCREEN = 17
    Const HWND_TOPMOST = -1
    Const SWP_SHOWWINDOW = &H40
    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
      
      Form_First_wh = Array(UserForm1.Width, UserForm1.Height)
      Form_Last_wh = Form_First_wh
    End Sub

    Public Sub myShowMax()
        SetActiveWindow hWndForm
        ShowWindow hWndForm, SW_SHOWMAXIMIZED
    End Sub

Private Sub UserForm_Resize()
If (Form_First_wh(1) - UserForm1.Height) * (Form_First_wh(0) - UserForm1.Width) < 0 Then
     MsgBox "UserForm1.Zoom 無法支援視窗一邊放大, 另一邊卻縮小"
     UserForm1.Move UserForm1.Left, UserForm1.Top, Form_Last_wh(0), Form_Last_wh(1)
     Exit Sub
End If

On Error Resume Next
If Abs(Form_First_wh(1) - UserForm1.Height) > Abs(Form_First_wh(0) - UserForm1.Width) Then
     UserForm1.Width = UserForm1.Height / Form_First_wh(1) * Form_First_wh(0)
Else
    UserForm1.Height = UserForm1.Width / Form_First_wh(0) * Form_First_wh(1)
End If
On Error GoTo 0

   Form_Last_wh = Array(UserForm1.Width, UserForm1.Height)
UserForm1.Zoom = UserForm1.Height / Form_First_wh(1) * 100
End Sub

TOP

本帖最後由 bobomi 於 2015-5-17 12:04 編輯

我拿到13吋筆電試
比例好像ok耶
但是筆電有開視覺特效
才發現我的寫法會因為視覺特效一直刷新螢幕
導致 UserForm1 出現顫抖 ( 桌機沒開視覺特效 , 不會有這問題 )

所以你改成這樣看看
不去修改UserForm1 寬高, 使其自由度最大
但缺點你拉拉看就知道了
這問題出在  ZOOM 寬高只能同放大或縮小, 不能一邊放大另一邊縮小
不然 code 只要2行就解決了

(1)
Private Sub UserForm_Resize()
On Error Resume Next
UserForm1.Zoom = UserForm1.Height / Form_First_wh(1) * 100
End Sub
(2)
Private Sub UserForm_Resize()
On Error Resume Next
UserForm1.Zoom = UserForm1.Width / Form_First_wh(0) * 100
End Sub
(1) (2) 分別測試一下, 看看哪個比較適合

TOP

如果還是不行
把 UserForm1 的程式碼刪光(只留控制項就可以了 )
然後匯出 UserForm1  -> 上傳 UserForm1.frm
我再拉拉看為何不行

TOP

本帖最後由 bobomi 於 2015-5-17 14:30 編輯

改成下面的 code  (有發現問題在哪了)
看看筆電的13"螢幕問題是否改善
========================
Dim Form_First_wh
Dim Form_Last_wh
Dim UserForm1_BarHeight

   
    Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
    Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
    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
    Const SM_CXFULLSCREEN = 16
    Const SM_CYFULLSCREEN = 17
    Const HWND_TOPMOST = -1
    Const SWP_SHOWWINDOW = &H40
    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
      
      
      a = UserForm1.Height
      UserForm1.Height = 0
      UserForm1_BarHeight = UserForm1.Height
      UserForm1.Height = a + 1.5
      
      Form_First_wh = Array(UserForm1.Width, UserForm1.Height)
      Form_Last_wh = Form_First_wh
    End Sub

    Public Sub myShowMax()
        SetActiveWindow hWndForm
        ShowWindow hWndForm, SW_SHOWMAXIMIZED
    End Sub

Private Sub UserForm_Resize()
If Not IsArray(Form_First_wh) Then Exit Sub
' If (Form_First_wh(1) - UserForm1.Height) * (Form_First_wh(0) - UserForm1.Width) < 0 Then
'     MsgBox "UserForm1.Zoom 無法支援視窗一邊放大, 另一邊卻縮小"
'     UserForm1.Move UserForm1.Left, UserForm1.Top, Form_Last_wh(0), Form_Last_wh(1)
'     Exit Sub
' End If

On Error Resume Next
If Abs(Form_First_wh(1) - UserForm1.Height) > Abs(Form_First_wh(0) - UserForm1.Width) Then
     UserForm1.Width = (UserForm1.Height - UserForm1_BarHeight) / (Form_First_wh(1) - UserForm1_BarHeight) * Form_First_wh(0)
Else
    UserForm1.Height = UserForm1.Width / Form_First_wh(0) * (Form_First_wh(1) - UserForm1_BarHeight) + UserForm1_BarHeight
   'UserForm1.Height = UserForm1.Width / Form_First_wh(0) * Form_First_wh(1)
End If
On Error GoTo 0

   Form_Last_wh = Array(UserForm1.Width, UserForm1.Height)
On Error Resume Next
UserForm1.Zoom = (UserForm1.Height - UserForm1_BarHeight) / (Form_First_wh(1) - UserForm1_BarHeight) * 100
End Sub

TOP

還有一個方法, 就是不要用 ZOOM , 改成自己縮放

TOP

Zoom  比較好的方案
22.zip (12.39 KB)

TOP

UserForm.Zoom3.zip (15.19 KB)

TOP

1.zip (2.71 KB)
最大化時, 控件不會過大

TOP

回復 23# PKKO

zoom 就只能這樣 (它的寬高都只能是相同比例)
要想 下邊的控件不會過頭, 那麼右邊就會有多餘的空間  
要想右邊沒有多餘的空間 , 那麼下邊的控件就會過頭

自己縮放沒有上面問題 ( 但是字型無法完美縮放  )
終極一點  自己縮放 + Zoom 混搭 -> 字型可以較完美縮放

TOP

1.zip (2.71 KB)
這樣還會蓋到嗎?

TOP

        靜思自在 : 君子如水,隨方就圓,無處不自在。
返回列表 上一主題