home *** CD-ROM | disk | FTP | other *** search
/ Delphi 4 Bible / Delphi_4_Bible_Tom_Swan_IDG_Books_1998.iso / source / Status / Main.pas < prev    next >
Pascal/Delphi Source File  |  1998-03-04  |  4KB  |  128 lines

  1. unit Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   ComCtrls, StdCtrls, Buttons, ExtCtrls, ToolWin;
  8.  
  9. type
  10.   TMainForm = class(TForm)
  11.     Label1: TLabel;
  12.     StatusBar1: TStatusBar;
  13.     Timer1: TTimer;
  14.     CoolBar1: TCoolBar;
  15.     ToolBar1: TToolBar;
  16.     ToolButton1: TToolButton;
  17.     ToolButton2: TToolButton;
  18.     ToolButton3: TToolButton;
  19.     ToolButton4: TToolButton;
  20.     Label2: TLabel;
  21.     procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  22.       Y: Integer);
  23.     procedure FormResize(Sender: TObject);
  24.     procedure Timer1Timer(Sender: TObject);
  25.     procedure ToolButton1Click(Sender: TObject);
  26.     procedure ToolButton2Click(Sender: TObject);
  27.     procedure ToolButton4Click(Sender: TObject);
  28.     procedure Label1MouseMove(Sender: TObject; Shift: TShiftState; X,
  29.       Y: Integer);
  30.   private
  31.     { Private declarations }
  32.   public
  33.     { Public declarations }
  34.   end;
  35.  
  36. var
  37.   MainForm: TMainForm;
  38.  
  39. implementation
  40.  
  41. {$R *.DFM}
  42.  
  43. { Rather than use literal index values, this section defines
  44.   descriptive contants for the statusbar's four panels }
  45. const
  46.   XPanelIndex       = 0;
  47.   YPanelIndex       = 1;
  48.   MessagePanelIndex = 2;
  49.   TimePanelIndex    = 3;
  50.  
  51. { Update X and Y coordinate values in the statusbar }
  52. procedure TMainForm.FormMouseMove(Sender: TObject;
  53.   Shift: TShiftState; X, Y: Integer);
  54. begin
  55.   StatusBar1.Panels[XPanelIndex].Text := 'X = ' + IntToStr(X);
  56.   StatusBar1.Panels[YPanelIndex].Text := 'Y = ' + IntToStr(Y);
  57. end;
  58.  
  59. { Calculate width of middle panel so the others stay the
  60.   same size when the window resizes. }
  61. procedure TMainForm.FormResize(Sender: TObject);
  62. const
  63.   Fudge = 25;  // Allow extra space for width calculation
  64. var
  65.   W: Integer;  // Width of fixed-size panels
  66. begin
  67.   with StatusBar1 do
  68.   begin
  69.     W := Panels[XPanelIndex].Width +
  70.       Panels[YPanelIndex].Width + Panels[TimePanelIndex].Width;
  71.     Panels[MessagePanelIndex].Width := Width - (W + Fudge);
  72.   end;
  73. end;
  74.  
  75. { Display the time in the rightmost statusbar panel }
  76. procedure TMainForm.Timer1Timer(Sender: TObject);
  77. begin
  78.   StatusBar1.Panels[TimePanelIndex].Text := TimeToStr(Time);
  79. end;
  80.  
  81. { Force an exception to occur and display its message
  82.   in a statusbar panel. The call to ShowMessage is never
  83.   made, but is included to prevent the compiler from
  84.   optimizing out the integer division. }
  85. procedure TMainForm.ToolButton1Click(Sender: TObject);
  86. var
  87.   K, J: Integer;
  88. begin
  89.   K := 100; J := 0;
  90.   try
  91.     K := K div J;  // Force divide by zero exception
  92.     ShowMessage(IntToStr(K) + ':' + IntToStr(J));
  93.   except on E: Exception do
  94.     begin
  95.       MessageBeep(0);
  96.       StatusBar1.Panels[MessagePanelIndex].Text :=
  97.        'Error: ' + E.Message;
  98.     end;
  99.   end;
  100. end;
  101.  
  102. { Clear the text from middle statusbar panel }
  103. procedure TMainForm.ToolButton2Click(Sender: TObject);
  104. begin
  105.   StatusBar1.Panels[MessagePanelIndex].Text := '';
  106. end;
  107.  
  108. { End the program }
  109. procedure TMainForm.ToolButton4Click(Sender: TObject);
  110. begin
  111.   Close;
  112. end;
  113.  
  114. { Translate mouse coordinates when the cursor moves over
  115.   one of the two large labels, and pass the results on
  116.   to the MainForm's OnMouseMove event handler. }
  117. procedure TMainForm.Label1MouseMove(Sender: TObject;
  118.   Shift: TShiftState; X, Y: Integer);
  119. var
  120.   T: TLabel;  // Refers to Sender as a TLabel object
  121. begin
  122.   T := Sender as TLabel;  // Initialize T
  123.   // Pass coordinates to MainForm's event handler
  124.   MainForm.FormMouseMove(Sender, Shift, T.Left + X, T.Top + Y);
  125. end;
  126.  
  127. end.
  128.