home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpw / owldemos / menu.pas < prev    next >
Pascal/Delphi Source File  |  1991-05-20  |  5KB  |  182 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows                     }
  4. {   Demo program                                 }
  5. {   Copyright (c) 1991 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. program Menu;
  10.  
  11. {$R MENU}
  12.  
  13. uses WinProcs, WinTypes, WObjects, Strings;
  14.  
  15. const
  16.  
  17. { Command IDs }
  18.  
  19.   cm_Modify  = 100;
  20.   cm_About   = 101;
  21.   cm_Static  = 200;
  22.   cm_Dynamic = 300;
  23.  
  24. { Modify dialog item IDs }
  25.  
  26.   id_InputBox = 100;
  27.   id_Checked  = 101;
  28.   id_Grayed   = 102;
  29.   id_Add      = 103;
  30.   id_Delete   = 104;
  31.  
  32. type
  33.  
  34. { Modify dialog object }
  35.  
  36.   PModifyDialog = ^TModifyDialog;
  37.   TModifyDialog = object(TDialog)
  38.     procedure AddItem(var Msg: TMessage);
  39.       virtual id_First + id_Add;
  40.     procedure DeleteItem(var Msg: TMessage);
  41.       virtual id_First + id_Delete;
  42.   end;
  43.  
  44. { Menu name string }
  45.  
  46.   TMenuName = array[0..31] of Char;
  47.  
  48. { Main window object }
  49.  
  50.   PMenuWindow = ^TMenuWindow;
  51.   TMenuWindow = object(TWindow)
  52.     MenuCount: Word;
  53.     MenuName: TMenuName;
  54.     constructor Init;
  55.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  56.     procedure DefCommandProc(var Msg: TMessage); virtual;
  57.     procedure CMModify(var Msg: TMessage);
  58.       virtual cm_First + cm_Modify;
  59.     procedure CMAbout(var Msg: TMessage);
  60.       virtual cm_First + cm_About;
  61.   end;
  62.  
  63. { Application object }
  64.  
  65.   TMenuApp = object(TApplication)
  66.     procedure InitMainWindow; virtual;
  67.   end;
  68.  
  69. { Handle the Add button by appending a new item to the Dynamic menu. }
  70.  
  71. procedure TModifyDialog.AddItem(var Msg: TMessage);
  72. var
  73.   Style: Word;
  74.   Name: TMenuName;
  75. begin
  76.   GetWindowText(GetDlgItem(HWindow, id_InputBox), Name, SizeOf(Name));
  77.   if Name[0] <> #0 then
  78.   begin
  79.     if SendMessage(GetDlgItem(HWindow, id_Checked),
  80.       bm_GetCheck, 0, 0) = 0 then
  81.       Style := mf_Unchecked
  82.     else
  83.       Style := mf_Checked;
  84.     if SendMessage(GetDlgItem(HWindow, id_Grayed),
  85.       bm_GetCheck, 0, 0) = 0 then
  86.       Style := Style or mf_Enabled
  87.     else
  88.       Style := Style or mf_Grayed;
  89.     with PMenuWindow(Parent)^ do
  90.     begin
  91.       AppendMenu(GetSubMenu(GetMenu(HWindow), 2),
  92.         Style or mf_String, cm_Dynamic + MenuCount, @Name);
  93.       Inc(MenuCount);
  94.     end;
  95.   end;
  96.   EndDlg(id_Cancel);
  97. end;
  98.  
  99. { Handle the Delete button.  Loop through all menu items on the
  100.   Dynamic menu until a matching item is found, and then delete that
  101.   item.  If no match is found, bring up an error box. }
  102.  
  103. procedure TModifyDialog.DeleteItem(var Msg: TMessage);
  104. var
  105.   I: Integer;
  106.   DynamicMenu: HMenu;
  107.   Name, S: TMenuName;
  108. begin
  109.   GetWindowText(GetDlgItem(HWindow, id_InputBox), Name, SizeOf(Name));
  110.   DynamicMenu := GetSubMenu(GetMenu(Parent^.HWindow), 2);
  111.   for I := 0 to GetMenuItemCount(DynamicMenu) - 1 do
  112.   begin
  113.     GetMenuString(DynamicMenu, I, S, SizeOf(S), mf_ByPosition);
  114.     if StrIComp(Name, S) = 0 then
  115.     begin
  116.       DeleteMenu(DynamicMenu, I, mf_ByPosition);
  117.       EndDlg(id_Cancel);
  118.       Exit;
  119.     end;
  120.   end;
  121.   MessageBox(HWindow, 'Menu item not found', 'Error', mb_Ok);
  122. end;
  123.  
  124. { Constructor for main window object. }
  125.  
  126. constructor TMenuWindow.Init;
  127. begin
  128.   TWindow.Init(nil, 'Menu Demo');
  129.   Attr.Menu := LoadMenu(HInstance, 'Menu');
  130.   MenuCount := 1;
  131.   MenuName[0] := #0;
  132. end;
  133.  
  134. { Bring up the About box. }
  135.  
  136. procedure TMenuWindow.CMAbout(var Msg: TMessage);
  137. begin
  138.   Application^.ExecDialog(New(PDialog, Init(@Self, 'About')));
  139. end;
  140.  
  141. { Bring up the Modify dialog. }
  142.  
  143. procedure TMenuWindow.CMModify(var Msg: TMessage);
  144. begin
  145.   Application^.ExecDialog(New(PModifyDialog, Init(@Self, 'Modify')));
  146. end;
  147.  
  148. { Paint window by displaying the name of the last menu selection. }
  149.  
  150. procedure TMenuWindow.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  151. begin
  152.   TextOut(PaintDC, 10, 10, MenuName, StrLen(MenuName));
  153. end;
  154.  
  155. { All unrecognized commands are sent to DefCommandProc.  Get the name
  156.   of the menu selection that generated the command, and invalidate
  157.   the window's client area so the new menu name gets displayed. }
  158.  
  159. procedure TMenuWindow.DefCommandProc(var Msg: TMessage);
  160. begin
  161.   GetMenuString(GetMenu(HWindow), Msg.WParam, MenuName,
  162.     SizeOf(MenuName), mf_ByCommand);
  163.   InvalidateRect(HWindow, nil, True);
  164.   TWindow.DefCommandProc(Msg);
  165. end;
  166.  
  167. { Create the application's main window. }
  168.  
  169. procedure TMenuApp.InitMainWindow;
  170. begin
  171.   MainWindow := New(PMenuWindow, Init);
  172. end;
  173.  
  174. var
  175.   MenuApp: TMenuApp;
  176.  
  177. begin
  178.   MenuApp.Init('Menu');
  179.   MenuApp.Run;
  180.   MenuApp.Done;
  181. end.
  182.