minimize icon to the vba form and remove close icon.

Posted on 2005-04-18
Last Modified: 2012-08-14
Is there a way to add a minimize icon on the vba form in excel and remove the close icon (the while x in red background)

Thank you.
Question by:Aiysha
    LVL 32

    Expert Comment

    To disable both the close button and close menu option from the system menu on a VB form, use this

    visual basic code:Option Explicit

    Private Const xSC_CLOSE As Long = -10
    Private Const SC_CLOSE As Long = &HF060&
    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 Function EnableCloseButton(ByVal plnghWnd As Long, pblnEnable As Boolean) As Integer
             Dim lngMenuID As Long
        EnableCloseButton = -1
        If IsWindow(plnghWnd) = 0 Then Exit Function
        Dim hMenu As Long
        hMenu = GetSystemMenu(plnghWnd, 0)
        MII.cbSize = Len(MII)
        MII.dwTypeData = String(80, 0)
        MII.cch = Len(MII.dwTypeData)
        MII.fMask = MIIM_STATE
        If pblnEnable Then
            MII.wID = xSC_CLOSE
            MII.wID = SC_CLOSE
        End If
            EnableCloseButton = -0
        If GetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function
            lngMenuID = MII.wID
        If pblnEnable Then
            MII.wID = SC_CLOSE
            MII.wID = xSC_CLOSE
        End If
            MII.fMask = MIIM_ID
        EnableCloseButton = -2
        If SetMenuItemInfo(hMenu, lngMenuID, False, MII) = 0 Then Exit Function
            If pblnEnable Then
            MII.fState = (MII.fState Or MFS_GRAYED)
            MII.fState = MII.fState - MFS_GRAYED
            MII.fState = (MII.fState Or MFS_GRAYED)
        End If
            MII.fMask = MIIM_STATE
        EnableCloseButton = -3
        If SetMenuItemInfo(hMenu, MII.wID, False, MII) = 0 Then Exit Function
        SendMessage plnghWnd, WM_NCACTIVATE, True, 0
        EnableCloseButton = 0
    End Function

    Private Sub Form_Load()
    EnableCloseButton Me.hWnd, False
    End Sub
    LVL 32

    Accepted Solution


    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Article by: Martin
    Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now