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

  1. {***********************************************************************
  2.  *  Copyright (c) 1991 - Borland International.
  3.  *
  4.  *  File: TDODEMOB.PAS
  5.  *
  6.  *  Buggy version of the TDODEMO.PAS that shows how to use TDW to
  7.  *  debug an Object Windows application.
  8.  *
  9.  *  The Color Scribble program lets the user draw on the screen in
  10.  *  any of four colors: red, green, blue, and black. This version
  11.  *  contains several bugs that you can use TDW to find and Turbo Pascal
  12.  *  for Windows to correct. For more information, see the Turbo Debugger
  13.  *  User's Guide and read the section on debugging an Object Windows
  14.  *  application.
  15.  ***********************************************************************}
  16.  
  17. program CScribble;
  18.  
  19. {$R TDODEMO.RES} { Include resource file having menu definition. }
  20.  
  21. uses  WinTypes, WinProcs, WObjects;
  22.  
  23. const
  24.   PenWidth   = 1;          { Width of Scribble line.      }
  25.   MenuID     = 100;        { ID of menu in resource file. }
  26.   IconID     = 100;        { ID of Icon in resource file. }
  27.   RedMenu    = 101;        { Value of Pen|Red menu.       }
  28.   GreenMenu  = 102;        { Value of Pen|Green menu.     }
  29.   BlueMenu   = 103;        { Value of Pen|Blue menu.      }
  30.   BlackMenu  = 104;        { Value of Pen|Black menu.     }
  31.  
  32. type
  33. { --------------------------------------------------------
  34.   CScribbleApplication type.
  35.   -------------------------------------------------------- }
  36.  CScribbleApplication = object(TApplication)
  37.    procedure InitMainWindow; virtual; { Creates main window }
  38.  end;
  39.  
  40.  
  41. type
  42. { --------------------------------------------------------
  43.   ScribbleWindow type.
  44.   -------------------------------------------------------- }
  45.  
  46.   PScribbleWindow = ^ScribbleWindow;
  47.   ScribbleWindow = object(TWindow)
  48.     HandleDC: HDC;        { Display context for drawing.          }
  49.               { Preserves value while dragging mouse. }
  50.     ButtonDown: Boolean;  { left-button-down flag }
  51.  
  52.     constructor Init(aParent: PWindowsObject; aTitle: PChar);
  53.  
  54.       { Virtual method that gets called when the left mouse      }
  55.       {    button is clicked in the window.  This method sets up    }
  56.       {    the window for scribbling by creating a display context. }
  57.     procedure WMLButtonDown(var Msg: TMessage); virtual WM_LBUTTONDOWN;
  58.  
  59.       { Virtual method that gets called when the left mouse     }
  60.       {    button is released in the window.  This method releases }
  61.       {    the display context that is used for drawing.           }
  62.     procedure WMLButtonUp(var Msg: TMessage); virtual WM_LBUTTONUP;
  63.  
  64.       { Virtual method that gets called when the mouse is   }
  65.       { moved anywhere in the window.  If the left mouse    }
  66.       { button is pressed, the window will be scribbled in. }
  67.     procedure WMMouseMove(var Msg: TMessage); virtual WM_MOUSEMOVE;
  68.  
  69.       { Virtual method that gets called when the right mouse button }
  70.       {    is clicked in the window.  It clears the window by invali-  }
  71.       {    dating the window, causing a WM_PAINT message to be sent.   }
  72.     procedure WMRButtonDown(var Msg: TMessage); virtual WM_RBUTTONDOWN;
  73.       
  74.   end;
  75.  
  76.  
  77. { --------------------------------------------------------
  78.   CScribbleWindow type.
  79.   -------------------------------------------------------- }
  80.   PCScribbleWindow = ^CScribbleWindow;
  81.   CScribbleWindow = object(ScribbleWindow)
  82.     thePen: HPen;  { Pen that is used for drawing in color }
  83.  
  84.       { Adds a menu to the window and }
  85.       { initializes the pen to black. }
  86.     constructor Init(aParent: PWindowsObject; ATitle: PChar);
  87.  
  88.     destructor Done; virtual;   { Disposes of the pen. }
  89.  
  90.       { Virtual method that gets called when user      }
  91.       { selects Pen.Red from the menu bar. Disposes   }
  92.       { of the current pen and creates a red pen.      }
  93.     procedure SelectRedPen(var Msg: TMessage);
  94.       virtual cm_First + RedMenu;
  95.  
  96.       { Virtual method that gets called when user      }
  97.       { selects Pen.Green from the menu bar. Disposes }
  98.       { of the current pen and creates a green pen.    }
  99.     procedure SelectGreenPen(var Msg: TMessage);
  100.       virtual cm_First + GreenMenu;
  101.  
  102.       { Virtual method that gets called when user      }
  103.       { selects Pen.Blue from the menu bar. Disposes  }
  104.       { of the current pen and creates a blue pen.     }
  105.     procedure SelectBluePen(var Msg: TMessage);
  106.       virtual cm_First + BlueMenu;
  107.  
  108.       { Virtual method that gets called when user       }
  109.       { selects Pen.Black from the menu bar. Disposes  }
  110.       { of the current pen and creates a black pen.     }
  111.     procedure SelectBlackPen(var Msg: TMessage);
  112.       virtual cm_First + BlackMenu;
  113.  
  114.       { Method that gets called when the user presses the         }
  115.       { left mouse button.  Selects pen into the display context. }
  116.     procedure WMLButtonDown(var Msg: TMessage);
  117.       virtual WM_LBUTTONDOWN;
  118.  
  119.       { Method to change the window class of the Scribble Window. }
  120.       {    Allows program to have an Icon associated with the        }
  121.       {    main window                                               }
  122.     procedure GetWindowClass(var AWndClass: TWndClass);virtual;
  123.  
  124.       { Returns a unique name for this class of window.  Needed }
  125.       {    because this class has a unique icon associated through }
  126.       {    GetWindowClass method                                   }
  127.     function GetClassName: PChar;virtual;
  128.  
  129.   end;
  130.  
  131. {*****************************************************************
  132.  * ScribbleWindow constructor.
  133.  *****************************************************************}
  134. constructor ScribbleWindow.Init(aParent: PWindowsObject; aTitle: PChar);
  135. begin
  136.   TWindow.Init(aParent, aTitle);
  137.   ButtonDown := False;
  138. end;
  139.  
  140. {*****************************************************************
  141.  * procedure ScribbleWindow.WMLButtonDown
  142.  *
  143.  * Process WM_LBUTTONDOWN messages by creating a display context and
  144.  * marking mouse as being pressed.  Also tell Windows to send
  145.  * all mouse messages to window.
  146.  *****************************************************************}
  147. procedure ScribbleWindow.WMLButtonDown(var Msg: TMessage);
  148. begin
  149.   if not ButtonDown then
  150.   begin
  151.     ButtonDown := True;  { Mark mouse button as being     }
  152.              { pressed so when mouse movement }
  153.              { occurs, a line will be drawn.  }
  154.  
  155.     MoveTo(HandleDC, Msg.LParamLo, { Move drawing point to location }
  156.        Msg.LParamHi);          { where mouse was pressed.       }
  157.  
  158.   end;
  159. end;
  160.  
  161. {*****************************************************************
  162.  * procedure ScribbleWindow.WM_Mousemove
  163.  *
  164.  * Process WM_MOUSEMOVE messages by drawing a line if the
  165.  * mouse button is marked as being pressed.
  166.  *****************************************************************}
  167. procedure ScribbleWindow.WMMouseMove(var Msg: TMessage);
  168. begin
  169.   if ButtonDown then  { If the mouse button is currently down        }
  170.     LineTo(HandleDC, Msg.LParamLo, Msg.LParamHi);
  171.               { Draw a line to where the mouse is presently. }
  172. end;
  173.  
  174. {*****************************************************************
  175.  * procedure ScribbleWindow.WM_LBUTTONUP
  176.  *
  177.  * Process WM_LBUTTONUP messages by allowing other applications
  178.  * to receive mouse messages, releasing the display context, and
  179.  * marking the mouse button as not being pressed.
  180.  *****************************************************************}
  181. procedure ScribbleWindow.WMLButtonUp(var Msg: TMessage);
  182. begin
  183.   if ButtonDown then
  184.   begin
  185.     ReleaseDC(hWindow, handleDC); { Release display context created   }
  186.                           { by WMLButtonDown method.          }
  187.     ButtonDown := False;          { Mark mouse button as not pressed. }
  188.   end;
  189. end;
  190.  
  191. {*****************************************************************
  192.  * procedure ScribbleWindow.WMRButtonDown
  193.  *
  194.  * Process WM_RBUTTONDOWN messages by erasing the window.
  195.  ***************************************************************** }
  196. procedure ScribbleWindow.WMRButtonDown(var Msg: TMessage);
  197. begin
  198.   UpdateWindow(HWindow);  { Causes WM_PAINT message }
  199.                           { to be sent to window.   }
  200. end;
  201.  
  202. {*****************************************************************
  203.  * CScribbleWindow constructor.
  204.  *****************************************************************}
  205. constructor CScribbleWindow.Init(aParent: PWindowsObject; ATitle:PChar);
  206. begin
  207.  ScribbleWindow.Init(aParent,ATitle);      { Call parent constructor.  }
  208.  Attr.Menu := LoadMenu(HInstance,          { Attach menu from resource }
  209.          MAKEINTRESOURCE(MenuID)); { file to window.           }
  210.  
  211.  thePen := CreatePen(PS_SOLID,PenWidth,    {Initialize pen to black.   }
  212.              RGb(0, 0, 0));
  213. end;
  214.  
  215. {*****************************************************************
  216.  * CScribbleWindow destructor.
  217.  *****************************************************************}
  218. destructor CScribbleWindow.Done;
  219. begin
  220.   TWindow.Done;         { Call standard OWL destructor for a window. }
  221.   DeleteObject(thePen); { Dispose of pen that was created. }
  222. end;
  223.  
  224. {*****************************************************************
  225.  * procedure CScribbleWindow.SelectRedPen
  226.  *
  227.  * Create a red pen in response to a "Red" selection from
  228.  * Pen menu.
  229.  *****************************************************************}
  230. procedure CScribbleWindow.SelectRedPen(var Msg: TMessage);
  231. begin
  232.   DeleteObject(thePen);                { Dispose of the current pen }
  233.   thePen := CreatePen(PS_SOLID, PenWidth, RGB(255, 0, 0));
  234. end;
  235.  
  236. {*****************************************************************
  237.  * procedure CScribbleWindow.SelectGreenPen
  238.  *
  239.  * Create a green pen in response to a "Green" selection from
  240.  * Pen menu.
  241.  *****************************************************************}
  242. procedure CScribbleWindow.SelectGreenPen(var Msg: TMessage);
  243. begin
  244.   DeleteObject(thePen);                 { Dispose of the current pen }
  245.   thePen := CreatePen(PS_SOLID, PenWidth, RGB(0, 255, 0));
  246. end;
  247.  
  248. {*****************************************************************
  249.  * procedure CScribbleWindow.SelectBluePen
  250.  *
  251.  * Create a blue pen in response to a "Blue" selection from
  252.  * Pen menu.
  253.  *****************************************************************}
  254. procedure CScribbleWindow.SelectBluePen(var Msg: TMessage);
  255. begin
  256.   DeleteObject(thePen);                 { Dispose of the current pen }
  257.   thePen := CreatePen(PS_SOLID, PenWidth, RGB(0, 0, 255));
  258. end;
  259.  
  260. {*****************************************************************
  261.  * procedure CScribbleWindow.SelectBlackPen
  262.  *
  263.  * Create a black pen in response to a "Black" selection from
  264.  * Pen menu.
  265.  *****************************************************************}
  266. procedure CScribbleWindow.SelectBlackPen(var Msg: TMessage);
  267. begin
  268.   DeleteObject(thePen); { Dispose of the current pen }
  269.   thePen := CreatePen(PS_SOLID, PenWidth, RGB(0, 0, 0));
  270. end;
  271.  
  272. {*****************************************************************
  273.  * procedure CScribbleWindow.WM_LButtonDown
  274.  *
  275.  * Select a colored pen into the display context.
  276.  *****************************************************************}
  277. procedure CScribbleWindow.WMLButtonDown(var Msg: TMessage);
  278. begin
  279.   ScribbleWindow.WMLButtonDown(Msg); { Call ScribbleWindow   }
  280.                                      { WMLButtonDown method. }
  281.   SelectObject(handleDC, thePen);    { Select pen into display context. }
  282. end;
  283.  
  284. {*****************************************************************
  285.  * procedure CScribbleWindow.GetWindowClass
  286.  *
  287.  * Changes the window icon to a custom icon
  288.  *****************************************************************}
  289. procedure CScribbleWindow.GetWindowClass(var AWndClass: TWndClass);
  290. begin
  291.   ScribbleWindow.GetWindowClass(AWndClass); { Get the ScribbleWindow }
  292.                         { class                  }
  293.   AWndClass.hIcon := LoadIcon(HInstance,MakeIntResource(IconID));
  294.                         { Attach a resource to }
  295.                         {  the window          }
  296. end;
  297.  
  298. {*****************************************************************
  299.  * function CScribbleWindow.GetClassName: PChar;
  300.  *
  301.  * Returns a unique class name for the Color Scribble window class.
  302.  *****************************************************************}
  303. function CScribbleWindow.GetClassName: PChar;
  304. begin
  305.   GetClassName := 'ColorScribble';
  306. end;
  307.  
  308. {*****************************************************************
  309.  * procedure CScribbleApplication.InitMainWindow
  310.  *
  311.  * Initialize a Color Scribble window for the main window.
  312.  *****************************************************************}
  313. procedure CScribbleApplication.InitMainWindow;
  314. begin
  315.   MainWindow := New(PCScribbleWindow, Init(nil, 'Scribble With Color!'));
  316. end;
  317.  
  318.  
  319. {*** Program begins here ***}
  320.  
  321. var
  322.   CSApp: CScribbleApplication;
  323.  
  324. begin
  325.   CSApp.Init('CScribble');
  326.   CSApp.Run;
  327.   CSApp.Done;
  328. end.
  329.