Главная страница | назад

Article #17501: Trapping the Alt-F4 command.

 Question and Answer Database

FAQ2501C.txt   Trapping the Alt-F4 command.
Category   :VCL
Platform    :All
Product    :C++Builder  3.x

How do I prevent someone from useing  to shut down an
application that I have written?

The answer lies in overriding the WndProc function of your form 
calss thusly:

Add the following to the header file, right under the line that says 
        private: // User declarations
        virtual void __fastcall WndProc(Messages::TMessage & msg);

and then add the following function to the .cpp file

void __fastcall TForm1::WndProc(Messages::TMessage & msg)
  TWMSysCommand & syscmd = reinterpret_cast(msg);
  if (   syscmd.Msg == WM_SYSCOMMAND
      && syscmd.CmdType == SC_CLOSE
      && syscmd.YPos <= 0
    HMENU systemMenu = GetSystemMenu(Handle, FALSE);
    MENUITEMINFO menuItemInfo;
    ZeroMemory(&menuItemInfo, sizeof(menuItemInfo));
    menuItemInfo.cbSize = sizeof(menuItemInfo);
    menuItemInfo.fMask = MIIM_STATE;
    if (   GetMenuItemInfo(systemMenu, SC_CLOSE, FALSE, &menuItemInfo)
        && menuItemInfo.fState & (MFS_DISABLED | MFS_GRAYED)

7/2/98 10:32:32 AM

Last Modified: 01-SEP-99