,调用父类的方法可完成窗体的激活、定位等与Windows同步的消息处理。重载该方法可改变窗体如何相应Windows的消息。本文程序就是运用两者的配合实现了给系统菜单添加相应事件的。
附程序清单:
1 unit1.h
/- #ifndef Unit1H #define Unit1H // #include < Classes.hpp > #include < Controls.hpp > #include < StdCtrls.hpp > #include < Forms.hpp > #include < Db.hpp > #include < DBGrids.hpp > #include < DBTables.hpp > #include < Grids.hpp > //-- class TForm1 : public TForm { __published: // IDE-managed Components void __fastcall FormCreate(TObject *Sender); // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); void __fastcall MyWndProc(Messages::TMessage &Message); protected: }; //-- extern PACKAGE TForm1 *Form1; //--- #endif 2 unit1.cpp //--- #include < vcl.h > #pragma hdrstop #include "Unit1.h" //- #pragma package(smart_init) #pragma resource "*.dfm" #define IDM_HELP1 1 #define IDM_HELP2 2 #define IDM_REMOVE 3 #define IDM_SEPARATOR1 4 #define IDM_SEPARATOR2 5 TForm1 *Form1; //--- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { WindowProc=MyWndProc; } //-- void __fastcall TForm1::FormCreate(TObject *Sender) { HMENU hMenu; HBITMAP hBitmapHelp; hMenu = GetSystemMenu (this->Handle, FALSE) ; AppendMenu (hMenu, MF_SEPARATOR,IDM_SEPARATOR1, NULL) ; AppendMenu (hMenu, MF_STRING,IDM_HELP1,"帮助") ; hBitmapHelp =LoadBitmap ((void*)HInstance, "HELP"); AppendMenu (hMenu, MF_BITMAP,IDM_HELP2, (char*)hBitmapHelp); AppendMenu (hMenu, MF_SEPARATOR,IDM_SEPARATOR2, NULL) ; AppendMenu (hMenu, MF_STRING,IDM_REMOVE,"取消菜单") ; } //- void __fastcall TForm1::MyWndProc (Messages::TMessage &Message) { HMENU hMenu; hMenu = GetSystemMenu (this->Handle, FALSE); if (Message.Msg == WM_SYSCOMMAND) { switch(Message.WParamLo) { case IDM_HELP1: ShowMessage("这是一个测试!"); break; case IDM_HELP2: ShowMessage("这是一个测试!"); break; case IDM_REMOVE: { DeleteMenu(hMenu,IDM_HELP1,MF_BYCOMMAND); DeleteMenu(hMenu,IDM_HELP2,MF_BYCOMMAND); DeleteMenu(hMenu,IDM_REMOVE,MF_BYCOMMAND); DeleteMenu(hMenu,IDM_SEPARATOR1,MF_BYCOMMAND); DeleteMenu(hMenu,IDM_SEPARATOR2,MF_BYCOMMAND); } default: WndProc(Message); } } else WndProc(Message); }
|