t e m p o r a l 
 d o o r w a y 

Adding to the System Menu For Your Window

 

Introduction

Sometimes it's nice to be able to add your own capabilities to the system menu on a window. In this example, the menu entry restores the original size of the window.

The .h File Additions

    private:   // User declarations

      void __fastcall WMSysCommand(Messages::TWMSysCommand &Msg); // special handling for the system menu entries

   public:

      void __fastcall Dispatch(void *Message)// Provide special handling for the system menu entries

      {
         switch (((PMessage)Message)->Msg)
         {
            case WM_SYSCOMMAND:
               WMSysCommand(*((Messages::TWMSysCommand *)Message));
               break;

            default:
                TForm::Dispatch(Message);
                break;
         };
      };

Additions To The Form Constructor

const String MenuString = "Original Size";

MENUITEMINFO info =
 {
  sizeof(MENUITEMINFO),
  MIIM_TYPE | MIIM_STATE | MIIM_ID ,
  MFT_STRING,
  MFS_ENABLED,
  IDS_CUSTOMID,
  NULL,
  NULL,
  NULL,
  0,
  MenuString.c_str(),
  0

 }; // last item not used

 // obtain the menu handle for the system menu
 HMENU SysMenu = GetSystemMenu(Handle, FALSE);

 // Insert the new menu item just after the close menu item. If the 3rd arg
 // is TRUE, the menu item is inserted after the menu item listed in the
 // second arg. FALSE puts the new item before the specified menu id.

 InsertMenuItem (SysMenu, SC_CLOSE, TRUE, &info);

The Handler For System Menu Items In The Form .cpp

//---------------------------------------------------------------------------
void __fastcall Form1::WMSysCommand(TWMSysCommand &Msg)
{
   if (Msg.CmdType == IDS_CUSTOMID)
   {
      Width = OriginalWidth; // Collected in Loaded()
      Height = OriginalHeight;
      Msg.Result = true;
   }
   else
   {
      TForm::Dispatch(&Msg);
   };
}

Conclusion

Yeah, it's not too hard, is it? Though there aren't many situations where you need it, this technique can come in handy.

Copyright © 2004 by Mark Cashman (unless otherwise indicated), All Rights Reserved