Hi, I would really appreciate some help with disabling the close button (and the system menu's close option) of a MDI child form in C++ .NET. I've scoured the web, and although I can find it in VB .NET, and bits in C#, I can't find anything to help with c++. A step by step guide would be really appreciated as I guess this will involve some API programming, which I have no experience in.
Thanks very much to anyone that can help.

After HOURS and HOURS of trying to fix this, I think I have finally come up with a solution for c++ .NET :D ! The issue you will notice with my code is that it has been placed in the VisibleChanged and sizechanged functions, instead of the Load function. this is because when visibility is changed on the form, for some reason the buttons were reset. I solved this obviously by activating the remove button function, whenever the form is becoming visible, or sized.

The code is as follows:

  1. At the beginning of the file, include this header file:

    #include "windows.h" 
    
  2. Add an event handler for when the visiblility and size is changed.

  3. Add a function with the following code:

    if(this->Visible)
    {
    HMENU test = GetSystemMenu((HWND)this->Handle.ToInt32(), false);
    EnableMenuItem(test, SC_CLOSE, MF_GRAYED | MF_BYCOMMAND);
    //the values you pass into RemoveMenu are from the file WinUser.h
    }
    
  4. Add to event handlers a call to the function above!

  5. Thats It!

If anyone can see any improvement to the code, then please go ahead and post it! I added the call to the function, so that the close button would remain disabled, even when the MDI child is maximized.

One easy way of disable the close button (X) in a form is to create a "base form" that your other form inherits from and in the base form add this code:

Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
'This Greys out the X and stops close from appearing in the
'Control box on the form
Dim cp As CreateParams = MyBase.CreateParams
Const CS_DBLCLKS As Int32 = &H8
Const CS_NOCLOSE As Int32 = &H200
cp.ClassStyle = CS_DBLCLKS Or CS_NOCLOSE
Return cp
End Get
End Property

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.