Home > Visual Basic > Menonaktifkan tombol Minimize, Maximize dan Close MDI Form

Menonaktifkan tombol Minimize, Maximize dan Close MDI Form

Pada postingan saya yang sebelumnya kita sudah mengetahui bagaimana cara menonaktifkan tombol Minimize, Maximize dan Close pada SDI Form.

Sesuai judul postingan kali ini yang akan kita nonaktifkan adalah tombol Minimize, Maximize dan Close pada MDI Form. Jika pada SDI Form untuk menonaktifkan tombol Minimize dan Maximize masih bisa dilakukan secara manual (via properties) untuk MDI Form tidak bisa, semuanya full coding.

Oke kita langsung saja tambahkan sebuah modul kemudian copas kode berikut :

Option Explicit

Private Const SC_CLOSE As Long = &HF060&
Private Const SC_MAXIMIZE As Long = &HF030&
Private Const SC_MINIMIZE As Long = &HF020&

Private Const xSC_CLOSE As Long = -10&
Private Const xSC_MAXIMIZE As Long = -11&
Private Const xSC_MINIMIZE As Long = -12&

Private Const GWL_STYLE = (-16)
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000

Private Const hWnd_NOTOPMOST = -2
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOMOVE = &H2
Private Const SWP_FRAMECHANGED = &H20

Private Const MIIM_STATE As Long = &H1&
Private Const MIIM_ID As Long = &H2&
Private Const MFS_GRAYED As Long = &H3&
Private Const WM_NCACTIVATE As Long = &H86

Private Type MENUITEMINFO
    cbSize As Long
    fMask As Long
    fType As Long
    fState As Long
    wID As Long
    hSubMenu As Long
    hbmpChecked As Long
    hbmpUnchecked As Long
    dwItemData As Long
    dwTypeData As String
    cch As Long
End Type

Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Private Declare Function GetMenuItemInfo Lib "user32" Alias "GetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal b As Boolean, lpMenuItemInfo As MENUITEMINFO) As Long
Private Declare Function SetMenuItemInfo Lib "user32" Alias "SetMenuItemInfoA" (ByVal hMenu As Long, ByVal un As Long, ByVal bool As Boolean, lpcMenuItemInfo As MENUITEMINFO) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function IsWindow 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 GetParent Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
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

Public Function EnableCloseButton(ByVal hWnd As Long, Enable As Boolean) As Integer
    EnableSystemMenuItem hWnd, SC_CLOSE, xSC_CLOSE, Enable, "EnableCloseButton"
End Function

'*******************************************************************************
' Enable / Disable Minimise Button
'-------------------------------------------------------------------------------

Public Sub EnableMinButton(ByVal hWnd As Long, Enable As Boolean)
    Dim lngFormStyle As Long

    ' Enable / Disable System Menu Item
    EnableSystemMenuItem hWnd, SC_MINIMIZE, xSC_MINIMIZE, Enable, "EnableMinButton"

    ' Enable / Disable TitleBar button

    lngFormStyle = GetWindowLong(hWnd, GWL_STYLE)
    If Enable Then
        lngFormStyle = lngFormStyle Or WS_MINIMIZEBOX
    Else
        lngFormStyle = lngFormStyle And Not WS_MINIMIZEBOX
    End If
    SetWindowLong hWnd, GWL_STYLE, lngFormStyle

    ' Dirty, slimy, devious hack to ensure that the changes to the
    ' window's style take immediate effect before the form is shown

    SetParent hWnd, GetParent(hWnd)
    SetWindowPos hWnd, hWnd_NOTOPMOST, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_FRAMECHANGED
End Sub

'*******************************************************************************
' Enable / Disable Maximise Button
'-------------------------------------------------------------------------------

Public Sub EnableMaxButton(ByVal hWnd As Long, Enable As Boolean)
    Dim lngFormStyle As Long

    ' Enable / Disable System Menu Item
    EnableSystemMenuItem hWnd, SC_MAXIMIZE, xSC_MAXIMIZE, Enable, "EnableMaxButton"

    ' Enable / Disable TitleBar button
    lngFormStyle = GetWindowLong(hWnd, GWL_STYLE)
    If Enable Then
        lngFormStyle = lngFormStyle Or WS_MAXIMIZEBOX
    Else
        lngFormStyle = lngFormStyle And Not WS_MAXIMIZEBOX
    End If
    SetWindowLong hWnd, GWL_STYLE, lngFormStyle

    ' Dirty, slimy, devious hack to ensure that the changes to the
    ' window's style take immediate effect before the form is shown

    SetParent hWnd, GetParent(hWnd)
    SetWindowPos hWnd, hWnd_NOTOPMOST, 0, 0, 0, 0, SWP_NOZORDER Or SWP_NOSIZE Or SWP_NOMOVE Or SWP_FRAMECHANGED
End Sub

Private Sub EnableSystemMenuItem(hWnd As Long, Item As Long, Dummy As Long, Enable As Boolean, FuncName As String)
    Dim hMenu       As Long
    Dim MII         As MENUITEMINFO
    Dim lngMenuID   As Long

    If IsWindow(hWnd) = 0 Then
        Err.Raise vbObjectError, "modCloseBtn::" & FuncName, _
            "modCloseBtn::" & FuncName & "() - Invalid Window Handle"
        Exit Sub
    End If

    ' Retrieve a handle to the window's system menu
    hMenu = GetSystemMenu(hWnd, 0)

    ' Retrieve the menu item information for the Max menu item/button
    MII.cbSize = Len(MII)
    MII.dwTypeData = String$(80, 0)
    MII.cch = Len(MII.dwTypeData)
    MII.fMask = MIIM_STATE

    If Enable Then
        MII.wID = Dummy
    Else
        MII.wID = Item
    End If

    If GetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then
        Err.Raise vbObjectError, "modCloseBtn::" & FuncName, _
            "modCloseBtn::" & FuncName & "() - Menu Item Not Found"
        Exit Sub
    End If

    ' Switch the ID of the menu item so that VB can not undo the action itself
    lngMenuID = MII.wID

    If Enable Then
        MII.wID = Item
    Else
        MII.wID = Dummy
    End If

    MII.fMask = MIIM_ID
    If SetMenuItemInfo(hMenu, lngMenuID, False, MII) = 0 Then
        Err.Raise vbObjectError, "modCloseBtn::" & FuncName, _
            "modCloseBtn::" & FuncName & "() - Error encountered " & _
            "changing ID"
        Exit Sub
    End If

    ' Set the enabled / disabled state of the menu item
    If Enable Then
        MII.fState = MII.fState And Not MFS_GRAYED
    Else
        MII.fState = MII.fState Or MFS_GRAYED
    End If

    MII.fMask = MIIM_STATE
    If SetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then
         Err.Raise vbObjectError, "modCloseBtn::" & FuncName, _
            "modCloseBtn::" & FuncName & "() - Error encountered " & _
            "changing state"
        Exit Sub
    End If

    ' Activate the non-client area of the window to update the titlebar, and
    ' draw the Max button in its new state.
    SendMessage hWnd, WM_NCACTIVATE, True, 0
End Sub

Contoh pemanggilan di MDI Form  :

1. Menonaktifkan tombol Minimize

Private Sub cmdDisableMinButton_Click()
    EnableMinButton Me.hWnd, False
End Sub

2. Menonaktifkan tombol Maximize

Private Sub cmdDisableMinButton_Click()
    EnableMinButton Me.hWnd, False
End Sub

3. Menonaktifkan tombol Close

Private Sub cmdDisableMinButton_Click()
    EnableMinButton Me.hWnd, False
End Sub


Yaph mungkin itu saja

Selamat mencoba :)

About these ads
  1. iqbal
    February 17, 2010 at 5:04 am | #1

    bantuin aku dong,,
    gmana ne listing koneksi hp dengan menggunakan USB ke visua basic,,,
    tolong ya,,,,,

  2. February 17, 2010 at 6:40 am | #2

    iqbal :

    bantuin aku dong,,
    gmana ne listing koneksi hp dengan menggunakan USB ke visua basic,,,
    tolong ya,,,,,

    Masnya bisa coba gammu atau ActiveXperts SMS and MMS Toolkit, kalo kesulitan baru dishare disini

  3. Roni
    June 28, 2010 at 1:40 pm | #3

    thanks ya. usahakan semakin berkembang lagi ilmunya….., tp kok hasilnya bodoh amat sih?????

  4. October 24, 2010 at 2:17 pm | #5

    kesuwun ang…, kalo ingin buat efek suara sih gmn ? mksdny, klo kita klik command so keluar suara “kesuwun ang..,”

    • October 25, 2010 at 1:49 am | #6

      Gampang rekam dulu aja suaranya trus disimpan dengan format wav, setelah itu pelajari artikel yang disini.

  5. gigien
    May 31, 2011 at 8:48 pm | #7

    double klik di atas kok masih bisa kang jadi ngikut ukuran aselinya
    di disable sekalian kang :D

    • June 6, 2011 at 8:36 am | #8

      Langsung dihandle aja om di event MDIForm_Resize nya

  6. ahmad7802
    April 11, 2012 at 11:35 am | #9

    Om tanya dong , aku pernah buat form di komputer dengan desktop resolusi nya 1440 x 900 , waktu aku install di kompi lain dengan desktop lebih kecil dan resolusi nya di bawah 1440 x 900 kok form nya cuma kebuka setengah ga full , mungkin ada solusinya … thanks atas bantuan nya

    • April 11, 2012 at 12:28 pm | #10

      Ya udah resolusinya aja dinaikin :D

      Klo sy biasanya desain standar formnya pake resolusi 1024 x 768, kan sekarang udah jarang resolusi dibawah 1024 x 768, klo lebihkan enggak masalah :D

      Untuk solusi sementara mungkin bisa coba komponen ActiveResize ada yg versi freenya kok

  7. ahmad7802
    April 14, 2012 at 1:52 am | #11

    Maaf Om cara mengatur resolusi form nya di mana ?…

    • April 14, 2012 at 8:51 am | #12

      Tinggal tempelen om komponen ActiveResize di tiap form2, atw coba dulu lah di beberapa form.

  8. RIZAL001
    May 18, 2012 at 8:04 pm | #13

    maaf om.. apa kalau form di klik dua kali masih bisa minimize saya pernah coba pakai kode semacam ini tapi ketika bagian atas form saya double klik masih bisa minimize..

    • May 19, 2012 at 9:25 am | #14

      Kan baru code “semacam diatas” om, makanya dicoba lagi :D

  9. AngeL
    March 10, 2013 at 4:47 pm | #15

    Halo om…

    Om, kalo mo ngganti caption form MDI gmn y? Hee…

  10. AngeL
    March 10, 2013 at 5:01 pm | #16

    AngeL :
    Halo om…
    Om, kalo mo ngganti caption form MDI gmn y? Hee…

    Mksdny properti font di MDI om..

    • March 16, 2013 at 1:05 pm | #17

      Kayaxnya enggak tersedia deh property fontnya

  11. Alfa
    August 6, 2013 at 11:32 am | #18

    om, kalo cara membuat tombol yang menujukan ke shorcut key gimana ya om ?

    contoh :
    misalnya kita klik “CommandButton1″ nah otomatis kita kehubung ke shorcut key ( misal : CTRL+ Z, dll )

    • Alfa
      August 6, 2013 at 11:33 am | #19

      *shorcut = shortcut

    • August 7, 2013 at 7:49 am | #20

      Coba ini om :

      Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
          If KeyCode = vbKeyZ And (Shift And vbCtrlMask) Then
              MsgBox "Anda menekan tombol CTRL + Z"
          End If
      End Sub
      
      Private Sub Command1_Click()
          Form_KeyDown vbKeyZ, vbCtrlMask
      End Sub
      
      • bobzwkwkAlfa
        August 7, 2013 at 9:52 pm | #21

        ok, thanks

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: