home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpwinst / helpex.pak / HELPEX.PAS < prev    next >
Pascal/Delphi Source File  |  1991-05-21  |  9KB  |  347 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows                     }
  4. {   Demo program                                 }
  5. {   Copyright (c) 1991 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. program HelpEx;
  10.  
  11. {$X+}
  12.  
  13. {$R HELPEX.RES}
  14.  
  15. uses WinTypes, WinProcs, Strings;
  16.  
  17. const
  18.  
  19.   { File Menu Items }
  20.  
  21.   IdmNew    = 100;
  22.   IdmOpen   = 101;
  23.   IdmSave   = 102;
  24.   IdmSaveAs = 103;
  25.   IdmPrint  = 104;
  26.   IdmExit   = 105;
  27.  
  28.   { Edit Menu Items }
  29.  
  30.   IdmUndo  = 200;
  31.   IdmCut   = 201;
  32.   IdmCopy  = 202;
  33.   IdmPaste = 203;
  34.   IdmClear = 204;
  35.  
  36.   { Help Items }
  37.  
  38.   IdmAbout        = 300;
  39.   IdmHelpIndex    = 301;
  40.   IdmHelpKeyboard = 302;
  41.   IdmHelpHelp     = 303;
  42.  
  43.   ExeNameMaxSize = 128;
  44.  
  45.   HelpIdEditClear = 100;
  46.   HelpIdEditCopy  = 101;
  47.   HelpIdEditCut   = 102;
  48.   HelpIdEditPaste = 103;
  49.   HelpIdEditUndo  = 104;
  50.  
  51.   HelpIdFileExit   = 200;
  52.   HelpIdFileNew    = 201;
  53.   HelpIdFileOpen   = 202;
  54.   HelpIdFilePrint  = 203;
  55.   HelpIdFileSave   = 204;
  56.   HelpIdFileSaveAs = 205;
  57.  
  58.   HelpIdEditWindow   = 300;
  59.   HelpIdMaximizeIcon = 301;
  60.   HelpIdMinimizeIcon = 302;
  61.   HelpIdSystemMenu   = 303;
  62.   HelpIdTitleBar     = 306;
  63.   HelpIdSizingBorder = 307;
  64.  
  65. type
  66.   THelpName = array[0..ExeNameMaxSize+1] of Char;
  67.  
  68. var
  69.   Wnd: Hwnd;
  70.   Inst: tHandle;
  71.   Help: Boolean;
  72.   HelpCursor: HCursor;
  73.   HelpFileName: THelpName;
  74.   AccTable: THandle;
  75.  
  76. procedure MakeHelpPathName(var FileName: THelpName);
  77. var
  78.   FileNameLen: integer;
  79.   I: integer;
  80. begin
  81.   FileNameLen:= GetModuleFileName(Inst, FileName, ExeNameMaxSize);
  82.  
  83.   I := FileNameLen - 1;
  84.   while (I <> 0) and ((Filename[I] <> '\') and (Filename[I] <> ':')) do
  85.     Dec(I);
  86.   Inc(I);
  87.   if I + 13 <= ExeNameMaxSize then
  88.     StrCopy(@FileName[I], 'helpex.hlp')
  89.   else
  90.     StrCopy(@FileName[I], '?');
  91. end;
  92.  
  93. function About(Dlg: Hwnd; Message, WParam: Word; LParam: Longint): Boolean;
  94.   far;
  95. begin
  96.   About := False;
  97.   case Message of
  98.     WM_INITDIALOG:
  99.       About := True;
  100.     WM_COMMAND:
  101.       if WParam = idOk then
  102.       begin
  103.         EndDialog(Dlg, 1);
  104.         About := True;
  105.       end;
  106.   end;
  107. end;
  108.  
  109. function MainWndProc(Wnd: Hwnd; Message, WParam: Word;
  110.   LParam: LongInt): LongInt; export;
  111. var
  112.   ProcAbout: TFarProc;
  113.   HelpContextId: Longint;
  114.   Rect: TRect;
  115.   Pt: TPoint;
  116.   DoubleWord: LongInt;
  117.   WFormat: Word;
  118.   Arrow: HCursor;
  119. begin
  120.   MainWndProc := 0;
  121.   case message of
  122.  
  123.     WM_COMMAND:
  124.       { Was F1 just pressed in a menu, or are we in help mode
  125.         Shift+F1?  }
  126.       if Help then
  127.       begin
  128.         case WParam of
  129.           IdmNew: HelpContextId := HelpIdFileNew;
  130.           IdmOpen: HelpContextId := HelpIdFileOpen;
  131.           IdmSave: HelpContextId := HelpIdFileSave;
  132.           IdmSaveAs: HelpContextId := HelpIdFileSaveAs;
  133.           IdmPrint: HelpContextId := HelpIdFilePrint;
  134.           IdmExit: HelpContextId := HelpIdFileExit;
  135.           IdmUndo: HelpContextId := HelpIdEditUndo;
  136.           IdmCut: HelpContextId := HelpIdEditCut;
  137.           IdmClear: HelpContextId := HelpIdEditClear;
  138.           IdmCopy: HelpContextId := HelpIdEditCopy;
  139.           IdmPaste: HelpContextId := HelpIdEditPaste;
  140.         else
  141.           HelpContextId := 0;
  142.         end;
  143.  
  144.         if HelpContextId = 0 then
  145.         begin
  146.           MessageBox(Wnd, 'Help not available for Help Menu Item',
  147.             'Help Example', Mb_Ok);
  148.           MainWndProc := DefWindowProc(Wnd, Message, WParam, LParam);
  149.         end
  150.         else
  151.         begin
  152.           Help := False;
  153.           WinHelp(Wnd, HelpFileName, Help_Context, HelpContextId);
  154.         end
  155.       end
  156.       else
  157.         case WParam of
  158.           IdmNew,
  159.           IdmOpen,
  160.           IdmSave,
  161.           IdmSaveAs,
  162.           IdmPrint,
  163.           IdmUndo,
  164.           IdmCut,
  165.           IdmClear,
  166.           IdmCopy,
  167.           IdmPaste:
  168.             Messagebox(Wnd, 'Command not Implemented', 'Help Example', mb_Ok);
  169.           IdmExit:
  170.             DestroyWindow(Wnd);
  171.           IdmHelpIndex:
  172.             WinHelp(Wnd, HelpFileName, Help_Index, 0);
  173.           IdmHelpKeyBoard:
  174.             WinHelp(Wnd, HelpFileName, Help_Key, LongInt(PChar('keys')));
  175.           IdmHelpHelp:
  176.             WinHelp(Wnd, 'WINHELP.HLP', Help_Index, 0);
  177.           IdmAbout:
  178.             begin
  179.               ProcAbout:= MakeProcInstance(@About, Inst);
  180.               DialogBox(Inst, 'AboutBox', Wnd, ProcAbout);
  181.               FreeProcInstance(ProcAbout);
  182.             end;
  183.         else
  184.           MainWndProc := DefWindowProc(Wnd, Message, WParam, LParam);
  185.         end;
  186.  
  187.     WM_LBUTTONDOWN:
  188.       if Help then
  189.       begin
  190.         Help := False;
  191.         WinHelp(Wnd, HelpFileName, Help_Context, HelpIDEditWindow);
  192.       end
  193.       else
  194.         MainWndProc := DefWindowProc(Wnd, Message, WParam, LParam);
  195.  
  196.     WM_NCLBUTTONDOWN:
  197.  
  198.       { If we are in help mode (Shift+F1) then display
  199.         context sensitive help for non-client area.  }
  200.  
  201.       if Help then
  202.       begin
  203.         case WParam of
  204.           HtCaption: HelpContextId := HelpIdTitleBar;
  205.           HtReduce: HelpContextId := HelpIdMinimizeIcon;
  206.           HtZoom: HelpContextId := HelpIdMaximizeIcon;
  207.           HtSysMenu: HelpContextId := HelpIdSystemMenu;
  208.           HtBottom: HelpContextId := HelpIdSizingBorder;
  209.           HtBottomLeft: HelpContextId := HelpIdSizingBorder;
  210.           HtBottomRight: HelpContextId := HelpIdSizingBorder;
  211.           HtTop: HelpContextId := HelpIdSizingBorder;
  212.           HtLeft: HelpContextId := HelpIdSizingBorder;
  213.           HtRight: HelpContextId := HelpIdSizingBorder;
  214.           HtTopLeft: HelpContextId := HelpIdSizingBorder;
  215.           HtTopRight: HelpContextId := HelpIdSizingBorder;
  216.         else
  217.           HelpContextId := 0;
  218.         end;
  219.         if HelpContextId = 0 then
  220.           MainWndProc := DefWindowProc(Wnd, Message, WParam, LParam)
  221.         else
  222.         begin
  223.           Help := False;
  224.           WinHelp(Wnd, HelpFileName, Help_Context, HelpContextId);
  225.         end
  226.       end
  227.       else
  228.         MainWndProc := DefWindowProc(Wnd, Message, WParam, LParam);
  229.  
  230.     WM_KEYDOWN:
  231.       if WParam = vk_F1 then
  232.  
  233.         { If Shift-F1, turb help mode on and set help cursor }
  234.  
  235.         if GetKeyState(VK_Shift) < 0 then
  236.         begin
  237.           Help := True;
  238.           SetCursor(HelpCursor);
  239.           MainWndProc := DefWindowProc(Wnd, Message, WParam, LParam);
  240.         end
  241.  
  242.         { If F1 without shift, call up help main index topic }
  243.  
  244.         else
  245.           WinHelp(Wnd, HelpFileName, Help_Index, 0)
  246.  
  247.         { Escape during help mode: turn help mode off }
  248.  
  249.       else
  250.         if (WParam = vk_Escape) and Help then
  251.         begin
  252.           Help := False;
  253.           SetCursor(hCursor(GetClassWord(Wnd, GCW_HCursor)));
  254.         end;
  255.  
  256.     WM_SETCURSOR:
  257.  
  258.       { In help mode it is necessary to reset the cursor
  259.         in response to very WM_SETCURSOR message.  Otherwise,
  260.         by default, Windows will reset the cursor to that
  261.         of the window class.  }
  262.  
  263.       if Help then
  264.         SetCursor(HelpCursor)
  265.       else
  266.         MainWndProc := DefWindowProc(Wnd, Message, WParam, LParam);
  267.  
  268.     WM_INITMENU:
  269.       if Help then
  270.         SetCursor(HelpCursor)
  271.       else
  272.         MainWndProc := 1;
  273.  
  274.     WM_ENTERIDLE:
  275.       if ((WParam = msgf_Menu) and ((GetKeyState(VK_F1) and $8000) <> 0)) then
  276.       begin
  277.         Help := True;
  278.         PostMessage(Wnd, WM_KEYDOWN, VK_RETURN, 0);
  279.       end;
  280.  
  281.     WM_DESTROY:
  282.       begin
  283.         WinHelp(Wnd, HelpFileName, HELP_QUIT, 0);
  284.         PostQuitMessage(0);
  285.       end
  286.   else
  287.     MainWndProc := DefWindowProc(Wnd, Message, WParam, LParam);
  288.   end;
  289. end;
  290.  
  291. function InitInstance(Instance: THandle; CmdShow: Integer): Boolean;
  292. begin
  293.   Inst := Instance;
  294.   AccTable := LoadAccelerators(Inst,'HELPEXACC');
  295.   Wnd := CreateWindow('Helpex', 'Help Example', WS_OVERLAPPEDWINDOW,
  296.     CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, GetFocus, 0, Instance, nil);
  297.   if Wnd = 0 then
  298.   begin
  299.     InitInstance := False;
  300.     Exit;
  301.   end;
  302.   ShowWindow(Wnd, CmdShow);
  303.   UpdateWindow(Wnd);
  304.   EnableMenuItem(GetSubMenu(GetMenu(Wnd), 1), IdmClear, Mf_Enabled);
  305.  
  306.   MakeHelpPathName(HelpFileName);
  307.   HelpCursor := LoadCursor(Inst,'HELPCURSOR');
  308.   InitInstance := True;
  309. end;
  310.  
  311. function InitApplication(Instance: THandle): Boolean;
  312. var
  313.   WC: TWndClass;
  314. begin
  315.   with WC do
  316.   begin
  317.     style := CS_HRedraw or CS_VRedraw;
  318.     lpfnWndProc := @MainWndProc;
  319.     cbClsExtra := 0;
  320.     cbWndEXtra := 0;
  321.     hInstance := Instance;
  322.     hIcon := LoadIcon(0, IDI_Application);
  323.     hCursor := LoadCursor(0, IDC_Arrow);
  324.     hbrBackground := GetStockObject( White_Brush);
  325.     lpszMenuName := 'HELPEXMENU';
  326.     lpszClassName := 'Helpex';
  327.   end;
  328.   InitApplication := RegisterClass(WC);
  329. end;
  330.  
  331.  
  332. var
  333.   Message: TMsg;
  334.  
  335. begin { main }
  336.   if hPrevInst = 0 then
  337.     if not InitApplication(hInstance) then Halt;
  338.   if not InitInstance(hInstance, CmdShow) then Halt;
  339.   while GetMessage(Message, 0, 0, 0) do
  340.     if TranslateAccelerator(Wnd, AccTable, Message) = 0 then
  341.     begin
  342.       TranslateMessage(Message);
  343.       DispatchMessage(Message);
  344.     end;
  345. end.
  346.  
  347.