Oğuzhan Türk
Oğuzhan Türk

Reputation: 380

How to re-enable close button in windows forms?

I disabled a form's close button using following code:

virtual property System::Windows::Forms::CreateParams^ CreateParams
{
    System::Windows::Forms::CreateParams^ get() override
    {
        System::Windows::Forms::CreateParams^ cp = Form::CreateParams;
        cp->ClassStyle |= 0x200; //CP_NOCLOSE_BUTTON
        return cp;
    }
}

However, I want to re-enable this close button in a for example foo() function. How can I do?

Upvotes: 2

Views: 1585

Answers (1)

Evgeny Timoshenko
Evgeny Timoshenko

Reputation: 3257

You need to change window's class style using SetClassLong

There is example in , but idea is still the same:

public partial class Form1 : Form
{
    protected override CreateParams CreateParams
    {
        get
        {
            var cp = base.CreateParams;
            //cp.ClassStyle |= 0x200; // note this is off
            return cp;
        }
    }

    public Form1()
    {
        InitializeComponent();
    }

    // here button is being disabled
    private void Form1_Load(object sender, EventArgs e)
    {
        HandleRef handle = new HandleRef(null, this.Handle);
        var cp = CreateParams;
        cp.ClassStyle = cp.ClassStyle | (0x200);

        IntPtr style = new IntPtr(cp.ClassStyle);
        var classLong = Form1.SetClassLong(handle, (int)ClassLongFlags.GCL_STYLE, style);
    }

    // here is being enabled
    private void Form1_DoubleClick(object sender, EventArgs e)
    {
        HandleRef handle = new HandleRef(null, this.Handle);
        var cp = CreateParams;
        cp.ClassStyle = cp.ClassStyle & (~0x200);

        IntPtr style = new IntPtr(cp.ClassStyle);
        var classLong = Form1.SetClassLong(handle, (int)ClassLongFlags.GCL_STYLE, style);
    }
}

SetClassLong, ClassLongFlags can be found here http://www.pinvoke.net/

Here is version, without pinvoke.

#include <windows.h>

#define GCL_STYLE -26
using namespace System::Windows::Forms;
using namespace System::Runtime::InteropServices;
using namespace System;

public ref class Form1 : public Form
{
public:
    Form1()
    {
        InitializeComponent();

        this->Load += gcnew EventHandler(
            this, &Form1::Form1_Load);
        this->DoubleClick += gcnew EventHandler(
            this, &Form1::Form1_DoubleClick);
    }
protected:
    virtual property System::Windows::Forms::CreateParams^ CreateParams
    {
        System::Windows::Forms::CreateParams^ get() override
        {
            System::Windows::Forms::CreateParams^ cp = Form::CreateParams;
            //cp->ClassStyle |= 0x200; //CP_NOCLOSE_BUTTON
            return cp;
        }
    }
private:
    void Form1_Load(Object^ sender, EventArgs^ e)
    {
        HandleRef^ handle = gcnew HandleRef(nullptr, this->Handle);
        System::Windows::Forms::CreateParams^ cp = Form::CreateParams;
        cp->ClassStyle = cp->ClassStyle | (0x200);

        IntPtr^ style = gcnew IntPtr(cp->ClassStyle);
        ::SetClassLong(
            (HWND)this->Handle.ToPointer(), 
        (int)GCL_STYLE, 
        (LONG)style->ToInt32());
    }

    // here is being enabled
    // possibly, it is gonna be your `foo`
    void Form1_DoubleClick(Object^ sender, EventArgs^ e)
    {
        HandleRef^ handle = gcnew HandleRef(nullptr, this->Handle);
        System::Windows::Forms::CreateParams^ cp = Form::CreateParams;
        cp->ClassStyle = cp->ClassStyle & (~0x200);

        IntPtr^ style = gcnew IntPtr(cp->ClassStyle);

        ::SetClassLong(
            (HWND)this->Handle.ToPointer(), 
        (int)GCL_STYLE, 
        (LONG)style->ToInt32());
    }
};

Upvotes: 5

Related Questions