WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Hiding-Disabling Apps to Prevent User Intervention

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

How to Disable an Application's
Close Menu Item

Keywords: disable close menu 

Question:

I've written a script to create a back ground intended to keep the user out of anything but the application I wrote.

I want to disable the Minimize, Zoom, etc icons on the title bar, and also the control icon in the top left corner.

Answer:

With *some* DllCall those can be grayed out.

It's time you bought the API book for VB or something so you can look up all these API calls...

  1. Convert these numbers to decimal for use in WinBatch DLL Calls.
    #define SC_SIZE 0xF000          ; 61440
    #define SC_MOVE 0xF010          ; 61456
    #define SC_MINIMIZE 0xF020      ; 61472
    #define SC_MAXIMIZE 0xF030      ; 61488
    #define SC_NEXTWINDOW 0xF040    ; 61504
    #define SC_PREVWINDOW 0xF050    ; 61520
    #define SC_CLOSE 0xF060         ; 61536
    #define SC_VSCROLL 0xF070       ; 61552
    #define SC_HSCROLL 0xF080       ; 61568
    #define SC_MOUSEMENU 0xF090     ; 61584
    #define SC_KEYMENU 0xF100       ; 61696
    #define SC_ARRANGE 0xF110       ; 61712
    #define SC_RESTORE 0xF120       ; 61728
    #define SC_TASKLIST 0xF130      ; 61744
    #define SC_SCREENSAVE 0xF140    ; 61760
    #define SC_HOTKEY 0xF150        ; 61776
    #define SC_DEFAULT 0xF160       ; 61792
    #define SC_MONITORPOWER 0xF170  ; 61808
    #define SC_CONTEXTHELP 0xF180   ; 61824
    #define SC_PROPERTIES 0xE005    ; 57349  ; for DOS windows
    #define SC_TOOLBAR    0xE00E    ; 57358  ; for DOS windows
    

  2. Here's how to do SC_CLOSE.

    This code written for 32 bit Windows 95 will remove the Close Menu item from the system menu, thus preventing the application from being closed. It also shows you how to re-enable the close menu item.

    When porting to Windows NT, you might have to poke around at the USER32.DLL to find the correct file. Maybe USER32.EXE or similiar file.

    As an example, this code affects a 'WinBatch Studio' window.

    hwnd=DllHwnd("WinBatch Studio") ;Window Title of desired window goes here
    sysmenu=DllCall(strcat(DirWindows(1),"USER32.DLL"),long:"GetSystemMenu",long:hwnd,long:0)
    DllCall(strcat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:61536,long:0)
    ;Sc_Close MF_ByCommand
    Message("Check Close line now","Should be gone")
    ;and to turn Close back on
    DllCall(strcat(DirWindows(1),"USER32.DLL"),long:"GetSystemMenu",long:hwnd,long:1)
    Message("Check Close line now","Should be back")
    
    The 61536 was the SC_CLOSE. Replace that number with the decimal number of the item you want. Hint: Windows Calculator in "Scientific" mode can do hex to decimal conversions.

    You ought to be able to get the example posted running. It removes the close button, then puts it back.

  3. All you need to do is duplicate the DllCall that does the close and change the numbers for the next to last parameter.

  4. But... Alt-Tab and Ctrl-Esc might still work.

  5. There is some DLL call that can turn Win95 into kiosk mode (search for it). Maybe that's what you want. Something about sending a message saying the current app is A Win95 screen saver (ummm it does not work under NT by the way).

Question:

In the routine to enable and disable the Close command in the system menu and the "X" Button, I tried to enable that command on a WinBatch window. The Close command appears in the menu and the X is not greyed, but nothing happens when I click on the X nor select Close in the System Menu.

Answer:

WinBatch does not respond to the close command.

SAMPLE CODE

Intcontrol(12,10,"",0,0)
Boxopen("Title","")

hwnd=DllHwnd("Title") ;Window Title of desired window goes here
sysmenu=DllCall(strcat(DirWindows(1),"USER32.DLL"),long:"GetSystemMenu",long:hwnd,long:0)
DllCall(strcat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:61536,long:0);close
DllCall(strcat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:61472,long:0);minimize
DllCall(strcat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:61728,long:0);restore
DllCall(strcat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:61488,long:0);max
;Sc_Close MF_ByCommand
Message("Check Close/Min/Max/Restore","Shouldn't work now")
timedelay(10)

;and to turn back on
;DllCall(strcat(DirWindows(1),"USER32.DLL"),long:"GetSystemMenu",long:hwnd,long:1)
exit

Remove Maximize Style

Question:

The commands (above) seem to work at removing the items from the menus themselves, but it doesn't disable the actual icons. Interestingly enough, if I remove the Close menu item, the × does get dimmed...

It seems that under Win XP, the menu items disappear, but the icons remain (and remain functional to boot). However if using the XP theme type windows the minimize and restore remove options are ignored...

Any further insight would be greatly appreciated.

BTW, I also tried EnableMenuItem(...) but that didn't work either... :(

Answer:

You can try to put this code in the case 0 of your dialog procedure:
;constants
user32=StrCat(DirWindows(1),"user32.dll")
WS_MAXIMIZEBOX=65536
GWL_STYLE=-16
;remove maximize style
oldstyle=DllCall(user32,long:"GetWindowLongA",long:handle,long:GWL_STYLE)
newstyle=oldstyle & ~WS_MAXIMIZEBOX
DllCall(User32,long:"SetWindowLongA",long:handle,long:GWL_STYLE,long:newstyle)
Assuming that handle is your dialog handle. I don't know if it's safe to use it anyway.

You can also add in minimize removal (WS_MINIMIZE = 131072), which effectively removes them from the dialog entirely.


Here is s UDF to Remove all but the terminate menu item:

#DefineFunction BoxRemoveSystemMenu(hwnd)
   sc_minimize= 61472
   sc_maximize= 61488
   sc_size =    61440
   sc_move = 61456
   sc_close = 61536
   sc_restore  = 61728

   sysmenu=DllCall(StrCat(DirWindows(1),"USER32.DLL"),long:"GetSystemMenu",long:hwnd,long:0)
   DllCall(StrCat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:sc_minimize,long:0)
   DllCall(StrCat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:sc_maximize,long:0)
   DllCall(StrCat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:sc_size,long:0)
   DllCall(StrCat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:sc_move,long:0)
   DllCall(StrCat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:sc_close,long:0)
   DllCall(StrCat(DirWindows(1),"USER32.DLL"),long:"RemoveMenu",long:sysmenu,long:sc_restore,long:0)
   Return 1
#EndFunction


hwnd=DllHwnd("") ; "" is shortcut for the WinBatch BoxOpen window
BoxOpen("BoxRemoveSystemMenu","See that system menu itmes have been removed")
BoxRemoveSystemMenu(hwnd)

TimeDelay(5)
BoxShut()

Article ID:   W13252
Filename:   Disable an Apps Close Menu Item.txt
File Created: 2012:04:04:07:56:58
Last Updated: 2012:04:04:07:56:58