home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_03_01 / 3n01044a < prev    next >
Text File  |  1991-10-17  |  6KB  |  186 lines

  1. { LISTING 1 }
  2. {$M 2048,1024,R-,S-,D-,L-,N-,A-}
  3. Program Bye;
  4.  
  5.    (* BYE.PAS - Quick Exit For Windows in Generic Windows API
  6.     * Written by Richard R. Sands in Turbo Pascal for Windows
  7.     *)
  8.  
  9.    {$R BYE1}
  10.  
  11.    USES
  12.      WinTypes, WinProcs;
  13.  
  14.    CONST
  15.      AppName  = 'Bye';
  16.      Confirm  : Boolean = FALSE;  { Confirm Windows Shutdown? }
  17.  
  18.      idm_Quit    = 100;  { Sys Message Command: Quit Windows }
  19.      idm_Confirm = 101;  { Sys Message Command: Confirm Exit }
  20.      idm_OS      = 102;  { Sys Message Command: Execute DOS Shell }
  21.      idm_About   = 103;  { Sys Message Command: Show About Dialog }
  22.  
  23. { -------------------------------------------------------------------------- }
  24. function About(Dialog: hWnd; Message, wParam: Word; lParam: Longint): Bool; EXPORT;
  25.   begin
  26.     About := True;
  27.     case Message of
  28.       wm_InitDialog: EXIT;
  29.       wm_Command   : if (wParam = id_Ok) or (wParam = id_Cancel) then
  30.                      begin
  31.                         EndDialog(Dialog, 1);
  32.                         EXIT
  33.                      end;
  34.     end;
  35.     About := False
  36.   end;
  37.  
  38. { -------------------------------------------------------------------------- }
  39. procedure DoAbout(Window:hWnd);
  40.   var AboutProc: tFarProc;
  41.   begin
  42.      AboutProc := MakeProcInstance(@About, hInstance);
  43.      DialogBox(hInstance, 'AboutBox', Window, AboutProc);
  44.      FreeProcInstance(AboutProc)
  45.   end;
  46.  
  47. { -------------------------------------------------------------------------- }
  48. Procedure SetConfirmState;
  49.   { Sets the [BYE]
  50.              Confirm=Y/N
  51.      WIN.INI setting }
  52.   var Buffer: Array[0..1] of char;
  53.   begin
  54.      if Confirm then Buffer[0] := 'Y'
  55.      else
  56.         Buffer[0] := 'N';
  57.      Buffer[1] := #0;  { Null terminate the String }
  58.      WriteProfileString(AppName, 'Confirm', Buffer);
  59.   end;
  60.  
  61. { -------------------------------------------------------------------------- }
  62. procedure Quit(Window: hWnd);
  63.   begin
  64.      if Confirm then
  65.      begin
  66.         MessageBeep(0);
  67.         if MessageBox(Window,'Are you sure you want to exit Windows?', 'Exit Windows?',
  68.                       mb_IconQuestion + mb_OkCancel) = id_Cancel then
  69.            EXIT
  70.      end;
  71.      SetConfirmState;   { Write our Confirm=Y/N entry in WIN.INI }
  72.      ExitWindows(0, 0)  { See 'ya }
  73.   end;
  74.  
  75. { -------------------------------------------------------------------------- }
  76. Procedure ToggleConfirm(Window : hWnd);
  77.   var SysMenu  : hMenu;
  78.   begin
  79.       SysMenu := GetSystemMenu(Window, FALSE);
  80.       Confirm := NOT Confirm;
  81.       if Confirm then
  82.          CheckMenuItem(SysMenu, idm_Confirm, mf_byCommand + mf_Checked)
  83.       else
  84.          CheckMenuItem(SysMenu, idm_Confirm, mf_byCommand + mf_UnChecked)
  85.   end;
  86.  
  87. { -------------------------------------------------------------------------- }
  88. function WindowProc(Window: hWnd; Message, WParam: Word; LParam: Longint): Longint; EXPORT;
  89.  
  90.   { This is the main message handling routine.  If there is a System Menu
  91.     command, then wm_SysCommand is sent with the menu option.  Otherwise,
  92.     I check for a double click on the icon, which is sent as a
  93.     wm_QueryOpen message, as a signal to close Windows }
  94.  
  95.   begin
  96.     WindowProc := 0;
  97.     case Message of
  98.       wm_SysCommand:
  99.           case wParam of
  100.             idm_About  : DoAbout(Window);
  101.             idm_Confirm: ToggleConfirm(Window);
  102.             idm_OS     : WinExec('Command.Com', sw_Normal);
  103.             idm_Quit   : Quit(Window);
  104.           end;
  105.       wm_QueryOpen:          { Double click on Icon }
  106.           begin
  107.               Quit(Window);
  108.               { If returns 0 then we cannot open - this suppresses the
  109.                 restore window function }
  110.               EXIT
  111.           end;
  112.       wm_Destroy:
  113.           begin
  114.               SetConfirmState;  { Update WIN.INI with Confirm Setting }
  115.               PostQuitMessage(0);
  116.               EXIT
  117.           end;
  118.     end;
  119.     { For all messages we don't care about, they are handled by the
  120.       "default window process" }
  121.     WindowProc := DefWindowProc(Window, Message, wParam, lParam)
  122.   end;
  123.  
  124. { -------------------------------------------------------------------------- }
  125. procedure WinMain;
  126.   var Window  : hWnd;
  127.       Message : tMsg;
  128.       SysMenu : hMenu;
  129.       Buffer  : Array[0..1] of char;
  130.   const
  131.     WindowClass: tWndClass = (
  132.       style      : 0;
  133.       lpfnWndProc: @WindowProc;
  134.       cbClsExtra : 0;
  135.       cbWndExtra : 0;
  136.       hInstance  : 0;
  137.       hIcon      : 0;
  138.       hCursor    : 0;
  139.       hbrBackground: 0;
  140.       lpszMenuName : AppName;
  141.       lpszClassName: AppName);
  142.   begin
  143.     if hPrevInst = 0 then
  144.     begin
  145.       WindowClass.hInstance := hInstance;
  146.       WindowClass.hIcon := LoadIcon(hInstance, 'ICON');
  147.       WindowClass.hCursor := LoadCursor(0, idc_Arrow);
  148.       WindowClass.hbrBackground := GetStockObject(white_Brush);
  149.       if not RegisterClass(WindowClass) then Halt(255);
  150.     end
  151.     else
  152.        Halt(0);  { Only One Instance Allowed }
  153.     Window := CreateWindow(AppName, AppName,
  154.                   ws_OverlappedWindow,
  155.                   cw_UseDefault, cw_UseDefault, cw_UseDefault, cw_UseDefault,
  156.                   0, 0, hInstance, nil);
  157.  
  158.     SysMenu := GetSystemMenu(Window, FALSE);
  159.  
  160.     { Add Items to the System Menu }
  161.     AppendMenu(SysMenu, mf_SysMenu + mf_Separator, 0, NIL);
  162.     AppendMenu(SysMenu, mf_SysMenu, idm_Quit,    '&Exit Windows');
  163.     AppendMenu(SysMenu, mf_SysMenu, idm_Confirm, 'Confirm &Shutdown');
  164.     AppendMenu(SysMenu, mf_SysMenu, idm_OS,      '&Operating System');
  165.     AppendMenu(SysMenu, mf_SysMenu, idm_About,   '&About Bye...');
  166.  
  167.     GetProfileString(AppName, 'Confirm', 'N', Buffer, sizeof(Buffer));
  168.     if Buffer[0] = 'Y' then
  169.        ToggleConfirm(Window);
  170.  
  171.     ShowWindow(Window, sw_Minimize);  { This minimizes the window }
  172.  
  173.     { The application's message loop }
  174.     while GetMessage(Message, 0, 0, 0) do
  175.     begin
  176.        TranslateMessage(Message);
  177.        DispatchMessage(Message)
  178.     end;
  179.     Halt(Message.wParam)
  180.   end;
  181.  
  182. { -------------------------------------------------------------------------- }
  183. begin
  184.    WinMain
  185. end.
  186.