# How do I disable the application Close button

**URL:** <https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284>\
**Category:** questions\
**Tags:** c11\
**Created:** [July 17, 2022, 10:03am UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284 "2022-07-17T10:03:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![DonnEdwards](https://clarionhub.com/user_avatar/clarionhub.com/donnedwards/32/1047_2.png) [@DonnEdwards](https://clarionhub.com/u/DonnEdwards)\
**Post date:** [July 17, 2022, 10:03am UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/1 "2022-07-17T10:03:17Z")

</div>

I am writing an email monitoring program that needs to run all the time. If the user goes to File → Exit then it should close. But I don’t want the user to click on the right-hand “x” button to close it. It’s too easy to close it by accident that way.

 ![Capture](https://clarionhub-uploads.s3.eu-north-1.amazonaws.com/original/2X/b/b1a07e7ef98993d191c90c791385a4daa783c6e8.jpeg)

How do I disable the close button, or reprogram it to minimise the app instead? I am using the CapeSoft WinEvent5 library if that will help.

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 17, 2022, 3:55pm UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/2 "2022-07-17T15:55:09Z")

</div>

AFAIK you cannot disable it.

Everything posts an Event:CloseWindow. In that embed if you CYCLE the close does not happen.

So create a local variable CloseAllowed. In Event:CloseWindow `IF ~CloseAllowed THEN CYCLE.`

In Accepted of Exit ITEM set `CloseAllowed=true`

That would be for a Window, for an APPLICATION Frame it’s EVENT:CloseDown.

You will also want to ALRT(EscKey) so they cannot press, I think ESC closes a frame.

Edit: Donn figured out that ?Exit ITEM must have STD(STD:Close) removed for this to work.

---

<div class="post-metadata">

**Author:** ![Mike\_Duglas](https://clarionhub.com/user_avatar/clarionhub.com/mike_duglas/32/1061_2.png) [@Mike\_Duglas](https://clarionhub.com/u/Mike_Duglas)\
**Post date:** [July 17, 2022, 4:20pm UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/3 "2022-07-17T16:20:21Z")

</div>

[How do I enable and disable the minimize, maximize, and close buttons in my caption bar? - The Old New Thing (microsoft.com)](https://devblogs.microsoft.com/oldnewthing/20100604-00/?p=13803#:~:text=If%20you%20never%20want%20the,item%20in%20your%20system%20menu.)

> To disable the Close button in the caption dynamically, you enable and disable the SC\_CLOSE menu item in your system menu.

```
void DisableCloseButton(HWND hwnd)
    {
     EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE,
                    MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
    }
    void EnableCloseButton(HWND hwnd)
    {
     EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE,
                    MF_BYCOMMAND | MF_ENABLED);
    }

```

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 17, 2022, 4:35pm UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/4 "2022-07-17T16:35:16Z")

</div>

> [@DonnEdwards](#):
>
> But I don’t want the user to click on the right-hand “x” button to close it. It’s too easy to close it by accident that way.

As Ray Chen says of you hide Close you may frustrate users since it works in every other program. _It makes them feel trapped and uncomfortable_

In my first post having Close do nothing will also frustrate or confuse. Instead give them a Message when Close button is pressed:

```
IF ~CloseAllowed THEN
     IF Message('if you close this you''re emails will not be blah blah blah', |
        'Close?',,'Monitor Email|Stop Email Monitoring' .... ) = 1 THEN CYCLE.

```

I prefer to not disable controls and leave a puzzle what is needed to enable. I would rather let them click on them then display a message ('You need to enter a yadda yadda…

---

<div class="post-metadata">

**Author:** ![DonnEdwards](https://clarionhub.com/user_avatar/clarionhub.com/donnedwards/32/1047_2.png) [@DonnEdwards](https://clarionhub.com/u/DonnEdwards)\
**Post date:** [July 17, 2022, 4:43pm UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/5 "2022-07-17T16:43:03Z")

</div>

This worked, until I tried File → Exit. I had to remove the STD property and then write

```
    CloseAllowed = TRUE    
    post(EVENT:CloseDown)

```

Without this small change the ?Exit code wasn’t executed.

I will put a warning message on the Close event when it is disabled.

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 17, 2022, 4:46pm UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/6 "2022-07-17T16:46:53Z")

</div>

Good catch … STD(STD:Close) has caused many WTF “why’s my embed code not running” delays

The Defaults.clw Windows used STD:Close on all the Close buttons. I asked to have it removed. Not sure if SV did it but I edited my Defaults.clw

---

<div class="post-metadata">

**Author:** ![Jane](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/ecb155/32.png) [@Jane](https://clarionhub.com/u/Jane)\
**Post date:** [July 17, 2022, 9:10pm UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/7 "2022-07-17T21:10:43Z")

</div>

I use the strategy of requiring a “canclose” variable.  
As in the attached demo app.

(Of course, the variable is normally set by a condition within the program, not by a checkbox as in the example.)

[CannotClose.zip](https://clarionhub.com/uploads/short-url/fgddMrXehPV3w6u6oRXfJZJNYBg.zip) (6.8 KB)

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 17, 2022, 11:45pm UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/8 "2022-07-17T23:45:23Z")

</div>

> [@Mike\_Duglas](#):
>
> [How do I enable and disable the minimize, maximize, and close buttons in my caption bar? - The Old New Thing](https://devblogs.microsoft.com/oldnewthing/20100604-00/?p=13803#:~:text=If%20you%20never%20want%20the,item%20in%20your%20system%20menu.) ([microsoft.com](http://microsoft.com))

Here’s the Clarion code:

```
 MAP 
    CloseButtonDisable(BOOL DisableClose=True) !Disables Close button (False Enable) on the Current Window

    MODULE('Win32')
      GetSystemMenu(LONG hWnd, BOOL bRevert=False),LONG,PASCAL,DLL(1) !Returns hMenu 
      EnableMenuItem(LONG hMenu, LONG uIDEnableItem, LONG uEnable ),LONG,PROC,PASCAL,DLL(1) !Returns Prior State
    END
 END

CloseButtonDisable PROCEDURE(BOOL DisableClose=True) !Disables Close button (False Enable) on the Current Window
hSysMenu LONG,AUTO
uEnable LONG,AUTO
cbSC_CLOSE EQUATE(0F060h)
!MF_BYCOMMAND EQUATE(0) Indicates uIDEnableItem gives the identifier of the menu item i.e. cbSC_CLOSE
!MF_ENABLED EQUATE(0)
!MF_DISABLED EQUATE(2) 2+1 to Disable and make visually Gray
!MF_GRAYED EQUATE(1)
  CODE
  hSysMenu=GetSystemMenu(0{PROP:Handle},False)
  IF hSysMenu THEN
     IF DisableClose THEN ! uEnable = 0 + CHOOSE(~DisableClose, 0, 2+1)
        uEnable = 0 + 2+1 ! Disable = 0 + 2 + 1 = MF_BYCOMMAND | MF_DISABLED | MF_GRAYED
     ELSE
        uEnable = 0 + 0 ! Enable = 0 + 0 = MF_BYCOMMAND | MF_ENABLED
     END
     EnableMenuItem(hSysMenu, cbSC_CLOSE, uEnable)
  END
  RETURN

```

* * *

> <https://gist.github.com/CarlTBarnes/86b64b19d5309a1ca00d1fce2bb225a3>

---

<div class="post-metadata">

**Author:** ![DonnEdwards](https://clarionhub.com/user_avatar/clarionhub.com/donnedwards/32/1047_2.png) [@DonnEdwards](https://clarionhub.com/u/DonnEdwards)\
**Post date:** [July 18, 2022, 11:57am UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/9 "2022-07-18T11:57:31Z")

</div>

Thanks everyone for the suggestions, code, etc.

 ![Capture](https://clarionhub-uploads.s3.eu-north-1.amazonaws.com/original/2X/6/60e1793cc23f0bb2f8f7ad4ed5e6ca22d30f057a.jpeg)

I put a warning message on the close window event, which shows up if you click on the close button in the top right corner. The File-\>Exit menu item allows the program to quit as expected. The blue message on the toolbar tells the rest of the story.

Thanks everyone for pitching in. I always appreciate it and I have learnt a lot more about events and menus too 🙂

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 18, 2022, 3:04pm UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/10 "2022-07-18T15:04:40Z")

</div>

> [@DonnEdwards](#):
>
> I put a warning message on the close window event,

You could add a Minimize button to the Message to save them the extra step of reading and following your instructions. If they click the Min button set `0{PROP:Iconize}=True`

Maybe button text of `'Minimize|Keep Open'`. I’d put Minimize first since that’s what you want them to do. I really go to this extra work to make it easy for them to do what I want them to do. And give them button names that are easy to understand, so they don’t have to read the message too well, versus Yes/No buttons require reading and understanding.

I would also add a caption to the `Message(, 'eMail Timer')` because its good to have a caption. Sometimes the Message can show on the taskbar so blank is bad, mainly shows when underlying window is hide.

---

<div class="post-metadata">

**Author:** ![MarkGoldberg](https://clarionhub.com/user_avatar/clarionhub.com/markgoldberg/32/1511_2.png) [@MarkGoldberg](https://clarionhub.com/u/MarkGoldberg)\
**Post date:** [July 19, 2022, 12:20pm UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/11 "2022-07-19T12:20:21Z")

</div>

All of what Carl said plus add system {prop:icon}

---

<div class="post-metadata">

**Author:** ![CarlBarnes](https://clarionhub.com/user_avatar/clarionhub.com/carlbarnes/32/1859_2.png) [@CarlBarnes](https://clarionhub.com/u/CarlBarnes)\
**Post date:** [July 19, 2022, 3:13pm UTC](https://clarionhub.com/t/how-do-i-disable-the-application-close-button/5284/12 "2022-07-19T15:13:31Z")

</div>

It took me a minute … you mean add the program icon to the Message? That would look nice and visually link it. Could also use the frame icon 0{PROP:Icon}.

```
IF Message('Please use File->Exit to quit |or minimize the window.', |
           'eMail Timer', 0{PROP:Icon} ,'Minimize |Keep Open')=1 THEN 0{PROP:Iconize}=1.

```
