Hưỡng dẫn cách để thu nhỏ lại Form xuống thanh công cụ ở dưới màn hình.

Liên hệ QC

Thanhlam2425

Thành viên hoạt động
Tham gia
23/11/18
Bài viết
113
Được thích
12
Anh chị cho em hỏi là mình đang mở ở trong form,Mà bây giờ muốn thu lại xuống thanh công cụ ở dưới màn hình thì làm cách nào ạ.Em đã ẩn Excel rồi.Nếu ẩn luôn Form nữa thì không mở lên được.
 
Bạn thêm cái này vào Module
PHP:
Option Explicit
 #If VBA7 Then
    Public Declare PtrSafe Function FindWindowA& Lib "user32" (ByVal lpClassName$, ByVal lpWindowName$)
    Public Declare PtrSafe Function GetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&)
    Public Declare PtrSafe Function SetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
 #Else
    Public Declare Function FindWindowA& Lib "user32" (ByVal lpClassName$, ByVal lpWindowName$)
    Public Declare Function GetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&)
    Public Declare Function SetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
 #End If
' Déclaration des constantes
Public Const GWL_STYLE As Long = -16
Public Const WS_MINIMIZEBOX = &H20000
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_FULLSIZING = &H70000

Public Sub InitMaxMin(mCaption As String, Optional Max As Boolean = True, Optional Min As Boolean = True _
        , Optional Sizing As Boolean = True)
'Attention, envoyer après changement du caption de l'UF
Dim hwnd As Long
    hwnd = FindWindowA(vbNullString, mCaption)
    If Min Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_MINIMIZEBOX
    If Max Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_MAXIMIZEBOX
    If Sizing Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_FULLSIZING
End Sub
Và thêm bên trong UserForm
PHP:
Private Sub UserForm_Initialize()
    InitMaxMin Me.Caption
End Sub
Private Sub CommandButton1_Click()
    UserForm1.Show vbModeless
End Sub
Userform sẽ bổ sung 2 chức năng này cho bạn. Nó kg xuống được taskbar nhưng nó sẽ thu nhỏ về góc cho mình.
1660544265355.png
*** Nguồn : https://stackoverflow.com/questions/50853247/excel-vba-userform-minimise-button
 
Upvote 0
Bạn thêm cái này vào Module
PHP:
Option Explicit
 #If VBA7 Then
    Public Declare PtrSafe Function FindWindowA& Lib "user32" (ByVal lpClassName$, ByVal lpWindowName$)
    Public Declare PtrSafe Function GetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&)
    Public Declare PtrSafe Function SetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
 #Else
    Public Declare Function FindWindowA& Lib "user32" (ByVal lpClassName$, ByVal lpWindowName$)
    Public Declare Function GetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&)
    Public Declare Function SetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
 #End If
' Déclaration des constantes
Public Const GWL_STYLE As Long = -16
Public Const WS_MINIMIZEBOX = &H20000
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_FULLSIZING = &H70000

Public Sub InitMaxMin(mCaption As String, Optional Max As Boolean = True, Optional Min As Boolean = True _
        , Optional Sizing As Boolean = True)
'Attention, envoyer après changement du caption de l'UF
Dim hwnd As Long
    hwnd = FindWindowA(vbNullString, mCaption)
    If Min Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_MINIMIZEBOX
    If Max Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_MAXIMIZEBOX
    If Sizing Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_FULLSIZING
End Sub
Và thêm bên trong UserForm
PHP:
Private Sub UserForm_Initialize()
    InitMaxMin Me.Caption
End Sub
Private Sub CommandButton1_Click()
    UserForm1.Show vbModeless
End Sub
Userform sẽ bổ sung 2 chức năng này cho bạn. Nó kg xuống được taskbar nhưng nó sẽ thu nhỏ về góc cho mình.
View attachment 279942
*** Nguồn : https://stackoverflow.com/questions/50853247/excel-vba-userform-minimise-button
Em cảm ơn ạ.Cho em hỏi thêm là bây giờ em không muốn gán nó là dấu trừ nữa mà có thể là nút bấm.Thì em sửa code thế nào ạ.
 
Upvote 0
:( Code này chỉ biết copy paste xài theo.
Thử add vào button nhưng không được. Chắc phải nhờ các anh vào trợ giúp.
 
Upvote 0
Em cảm ơn ạ.Cho em hỏi thêm là bây giờ em không muốn gán nó là dấu trừ nữa mà có thể là nút bấm.Thì em sửa code thế nào ạ.
Tức bạn có CommandButton1 và khi cần thu nhỏ thì bạn muốn nhấn CommandButton1 thay vì nhấn nút minimize trên thanh tiêu đề cửa sổ? Nếu thế thì đọc tiếp, ngược lại thì dừng tại đây.

Code làm việc nằm ở Module1. Code tối thiểu trong UserForm1 phải như trong tập tin đính kèm. Có thể nhấn CommandButton1 hoặc nhấn nút minimize trên thanh tiêu đề cửa sổ tùy theo hứng ở thời điểm cần thu nhỏ.
 

File đính kèm

  • Book2.xlsm
    23.4 KB · Đọc: 20
Lần chỉnh sửa cuối:
Upvote 0
Tức bạn có CommandButton1 và khi cần thu nhỏ thì bạn muốn nhấn CommandButton1 thay vì nhấn nút minimize trên thanh tiêu đề cửa sổ? Nếu thế thì đọc tiếp, ngược lại thì dừng tại đây.

Code làm việc nằm ở Module1. Code tối thiểu trong UserForm1 phải như trong tập tin đính kèm. Có thể nhấn CommandButton1 hoặc nhấn nút minimize trên thanh tiêu đề cửa sổ tùy theo hứng ở thời điểm cần thu nhỏ.
Em cảm ơn anh ạ.
 
Upvote 0
Bạn thêm cái này vào Module
PHP:
Option Explicit
 #If VBA7 Then
    Public Declare PtrSafe Function FindWindowA& Lib "user32" (ByVal lpClassName$, ByVal lpWindowName$)
    Public Declare PtrSafe Function GetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&)
    Public Declare PtrSafe Function SetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
 #Else
    Public Declare Function FindWindowA& Lib "user32" (ByVal lpClassName$, ByVal lpWindowName$)
    Public Declare Function GetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&)
    Public Declare Function SetWindowLongA& Lib "user32" (ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
 #End If
' Déclaration des constantes
Public Const GWL_STYLE As Long = -16
Public Const WS_MINIMIZEBOX = &H20000
Public Const WS_MAXIMIZEBOX = &H10000
Public Const WS_FULLSIZING = &H70000

Public Sub InitMaxMin(mCaption As String, Optional Max As Boolean = True, Optional Min As Boolean = True _
        , Optional Sizing As Boolean = True)
'Attention, envoyer après changement du caption de l'UF
Dim hwnd As Long
    hwnd = FindWindowA(vbNullString, mCaption)
    If Min Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_MINIMIZEBOX
    If Max Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_MAXIMIZEBOX
    If Sizing Then SetWindowLongA hwnd, GWL_STYLE, GetWindowLongA(hwnd, GWL_STYLE) Or WS_FULLSIZING
End Sub
Và thêm bên trong UserForm
PHP:
Private Sub UserForm_Initialize()
    InitMaxMin Me.Caption
End Sub
Private Sub CommandButton1_Click()
    UserForm1.Show vbModeless
End Sub
Userform sẽ bổ sung 2 chức năng này cho bạn. Nó kg xuống được taskbar nhưng nó sẽ thu nhỏ về góc cho mình.
View attachment 279942
*** Nguồn : https://stackoverflow.com/questions/50853247/excel-vba-userform-minimise-button
Cho em hỏi! vị trí thu nhỏ muốn ở tại vị trí form đang đứng thì làm như nào ạ, em cảm ơn
 
Upvote 0
Web KT
Back
Top Bottom