Skip to main content

Problem:

By default, the COBOL Text Window comes with the system menu that allows to minimize, restore down, and close the application window. Clicking on these buttons does not unfortunately trigger an event for the application to act upon, so it abruptly terminates the COBOL application without giving any chance to stop graciously. This often results to data file corruptions and other unexpected results.

The proposed solution in the past (see CloseButton.zip at the Examples and Utilities page) was to remove the system menu by using a Windows API functions GetWindowLong and SetWindowLong, and this does avoid the users to click on the Close button. What if you simply want to disable the Close button and keep the minimize and restore down buttons enabled?

Resolution:

There are actually other Windows API functions that can be used to disable the Close button from the system menu, and the following codes show how to use GetSystemMenu and EnableMenuItem:

           *>get handle of text window
           call "PC_WIN_HANDLE"
              using by reference ws-textwin-handle         

           *>get handle of window menu
           move isFalse to ws-BOOL
           call win32 "GetSystemMenu" using
                               by value ws-textwin-handle
                               by value ws-BOOL
               returning ws-HMENU

           if ws-HMENU = 0
               display "GetSystemMenu failed"
               stop run
           end-if           

           *>disable close button only
           move SC-CLOSE to uIDEnableItem
           add MF-BYCOMMAND to MF-GRAYED giving uEnable
           call win32 "EnableMenuItem" using
                               by value ws-HMENU
                               by value uIDEnableItem
                               by value uEnable
               returning retlong

           if retlong not = 0
               display "EnableMenuItem failed"
               stop run
           end-if

Here is a complete demo: CBLTxtWinNoClose.zip

You may find all the Windows API functions at the MSDN site, but GetSystemMenu, SetSystemMenu, and other related functions can be found specifically at the Menu Functions