home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D12 / WINDEMOS.ZIP / WMENU.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  4.6 KB  |  178 lines

  1. {************************************************}
  2. {                                                }
  3. {   Demo Program                                 }
  4. {   Copyright (c) 1991 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. program WMenu;
  9.  
  10. {$R WMenu}
  11.  
  12. uses WinProcs, WinTypes;
  13.  
  14. type
  15.   TList = array[1..99] of String[17];
  16.  
  17. const
  18.   ListCount:Word = 0;
  19.  
  20. var
  21.   UserMenu: hMenu;
  22.   List: TList;
  23.   ListCounter: Word;
  24.   MenuName:String[17];
  25.   Window: hWnd;
  26.  
  27. function AboutProc(Dlg: hWnd; iMessage, wParam: Word; lParam: LongInt): Bool; Export;
  28.   begin
  29.     AboutProc:=false;
  30.     case iMessage of
  31.       WM_Create: AboutProc:=true;
  32.       WM_Command: if (wParam = IDOK) or (wParam = IDCancel) then
  33.             begin
  34.               AboutProc:=true;
  35.               EndDialog(Dlg, 0);
  36.             end;
  37.     end;
  38.   end;
  39.  
  40. function CreateProc(Dlg: hWnd; iMessage, wParam:Word; lParam: LongInt): WordBool; export;
  41.   var
  42.     Style: Word;
  43.     Name: String[17];
  44.     Found: Boolean;
  45.     I: Integer;
  46.     DC: hDC;
  47.     R: TRect;
  48.   begin
  49.       CreateProc:=False;
  50.       case iMessage of
  51.     WM_InitDialog: CreateProc:=true;
  52.     WM_Command: case wParam of
  53.               IDCancel: begin
  54.                   EndDialog(Dlg, 0);
  55.                   CreateProc:=true;
  56.                 end;
  57.               104: begin
  58.                  Name[0]:=Char(GetWindowText(GetDlgItem(Dlg, 103), @Name[1], 16));
  59.                  Found:=false;
  60.                  for I:=1 to ListCount do
  61.                    if Name=List[I] then
  62.                  begin
  63.                    Found:=true;
  64.                    DeleteMenu(UserMenu, I+300, MF_ByCommand);
  65.                  end;
  66.                   if MenuName=Name then
  67.                 begin
  68.                   MenuName:='';
  69.                   GetClientRect(Window, R);
  70.                   InvalidateRect(Window, @R, true);
  71.                 end;
  72.                   if Not found then
  73.                 MessageBox(Dlg, 'Item Not Found', 'Error', MB_OK)
  74.                   else
  75.                 EndDialog(Dlg, 0);
  76.                end;
  77.               IDOK: begin
  78.                   if SendMessage(GetDlgItem(Dlg, 101), BM_GetCheck, 0,0)=0 then
  79.                 Style:=MF_UnChecked
  80.                   else
  81.                 Style:=MF_Checked;
  82.                   if SendMessage(GetDlgItem(Dlg, 102), BM_GetCheck, 0,0)=0 then
  83.                 Style:=Style or MF_Enabled
  84.                   else
  85.                 Style:=Style or MF_Grayed;
  86.                   Name[0]:=Char(GetWindowText(GetDlgItem(Dlg, 103), @Name[1], 16));
  87.                   Inc(ListCount);
  88.                   if ListCount>99 then
  89.                 begin
  90.                   MessageBox(Dlg, 'Too many menus', 'Error', MB_OK);
  91.                   Exit;
  92.                 end;
  93.                   List[ListCount]:=Name;
  94.                   AppendMenu(UserMenu, Style or MF_String, ListCount+300, @Name[1]);
  95.                   EndDialog(Dlg, 0);
  96.                   CreateProc:=true;
  97.                 end;
  98.              end;
  99.       end;
  100.  
  101.   end;
  102.  
  103. function WindowProc(Wnd: hWnd; iMessage, wParam:Word; lParam: LongInt): LongInt; export;
  104.   var
  105.     ProcInst: Pointer;
  106.     DC: hDC;
  107.     PaintStruct: TPaintStruct;
  108.     R: TRect;
  109.   begin
  110.     case iMessage of
  111.       WM_Command: case WParam of
  112.             100: begin
  113.                ProcInst:=MakeProcInstance(@CreateProc, hInstance);
  114.                DialogBox(hInstance, 'CreateDlg', Wnd, ProcInst);
  115.                FreeProcInstance(ProcInst);
  116.              end;
  117.             106: begin
  118.                ProcInst:=MakeProcInstance(@AboutProc, hInstance);
  119.                DialogBox(hInstance, 'About', Wnd, ProcInst);
  120.                FreeProcInstance(ProcInst);
  121.              end;
  122.             301..399: begin
  123.                 MenuName:=List[wParam-300];
  124.                 GetClientRect(Window, R);
  125.                 InvalidateRect(Window, @R, true);
  126.                   end;
  127.  
  128.           else
  129.             WindowProc:=DefWindowProc(Wnd, iMessage, wParam, lParam);
  130.           end;
  131.       WM_Paint: begin
  132.           DC:=BeginPaint(Wnd, PaintStruct);
  133.           TextOut(DC, 0, 0, @MenuName[1], Length(MenuName));
  134.           EndPaint(Wnd, PaintStruct);
  135.         end;
  136.       WM_Destroy: PostQuitMessage(0);
  137.     else
  138.       WindowProc:=DefWindowProc(Wnd, iMessage, wParam, lParam);
  139.     end;
  140.   end;
  141.  
  142. procedure WinMain;
  143.   var
  144.     WndClas: TWndClass;
  145.     Msg: TMsg;
  146.   begin
  147.     If hPrevInst = 0 then
  148.       begin
  149.     WndClas.Style := 0;
  150.     WndClas.lpfnWndProc:= @WindowProc;
  151.     WndClas.cbClsExtra := 0;
  152.     WndClas.cbWndExtra := 0;
  153.     WndClas.hInstance := HInstance;
  154.     WndClas.hIcon := 0;
  155.     WndClas.hCursor := LoadCursor(0, IDC_Arrow);
  156.     WndClas.hbrBackground := GetStockObject(White_Brush);
  157.     WndClas.lpszMenuName := 'Menu';
  158.     WndClas.lpszClassName := 'GenWindow';
  159.     if not RegisterClass(WndClas) then
  160.       Halt;
  161.       end;
  162.     Window := CreateWindow('GenWindow', 'Menu Example', WS_OverLappedWindow,
  163.              CW_UseDefault, 0, CW_UseDefault, 0, 0, 0, hInstance, nil);
  164.     UpDateWindow(Window);
  165.     UserMenu:=CreatePopUpMenu;
  166.     InsertMenu(GetMenu(Window), 106, MF_ByCommand or MF_PopUp, UserMenu, 'User Define Menu');
  167.     ShowWindow(Window,Sw_ShowNormal);
  168.     while GetMessage(Msg, 0, 0, 0) do
  169.       begin
  170.     TranslateMessage(msg);
  171.     DispatchMessage(msg);
  172.       end;
  173.   end;
  174.  
  175. begin
  176.   WinMain;
  177. end.
  178.