Preventing the user from closing a document using the close button on the window title bar

By Philippe Leefsma

Q:

How to prevent user from closing a document using the close button on the window title bar of the document?

A:

You can disable that close button by P/Invoking couple of Win32 API methods. Here is the VB.Net for disabling and enabling the close button:

    <DllImport("user32.dll")>

    Private Shared Function GetSystemMenu(

        ByVal hWnd As IntPtr,

        ByVal bRevert As Integer) As IntPtr

    End Function

 

    <DllImport("user32.dll")>

    Private Shared Function EnableMenuItem(

        ByVal hMenu As IntPtr,

        ByVal wIDEnableItem As UInteger,

        ByVal wEnable As UInteger) As IntPtr

    End Function

 

    Const MF_GRAYED = &H1&

    Const MF_BYCOMMAND = &H0&

    Const SC_CLOSE = &HF060&

 

    Sub DisableDocClose(ByVal app As Inventor.Application)

 

        Dim hwnd As IntPtr = GetSystemMenu(app.ActiveView.HWND, 0)

 

        Call EnableMenuItem(

            hwnd,

            SC_CLOSE,

            MF_BYCOMMAND Or MF_GRAYED)

 

    End Sub

 

    Sub EnableDocClose(ByVal app As Inventor.Application)

 

        Dim hwnd As IntPtr = GetSystemMenu(app.ActiveView.HWND, 0)

 

        Call EnableMenuItem(

            hwnd,

            SC_CLOSE,

            MF_BYCOMMAND And Not MF_GRAYED)

 

    End Sub

<

p style=”line-height: normal;margin: 0in 0in 0pt” class=”MsoNormal”> 


Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading