home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 7.ddi / TDDEMOS.ZIP / TDODEMOB.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  12.7 KB  |  331 lines

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