home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 5.ddi / TDWEXMPL.ZIP / TDODEMOB.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  9.4 KB  |  286 lines

  1. /***********************************************************************
  2.  *  Copyright (c) 1992 - Borland International.
  3.  *
  4.  *  File: TDODEMO.CPP
  5.  *
  6.  *  Buggy version of TDODEMO.CPP 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 C++
  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. #define STRICT
  18.  
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <owl.h>
  22. #include "tdodemob.h"
  23.  
  24. // CScribbleApplication Declarations
  25.  
  26. class CScribbleApplication : public TApplication
  27. {
  28. public:
  29.   CScribbleApplication(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance,
  30.     LPSTR lpCmdLine, int nCmdShow)
  31.     : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {};
  32.   virtual void InitMainWindow();
  33. };
  34.  
  35. // ScribbleWindow Declarations
  36.  
  37. class ScribbleWindow : public TWindow
  38. {
  39. public:
  40.   HDC HandleDC;              // Display context for drawing.
  41.   BOOL ButtonDown;           // left-button-down flag
  42.   HPEN ThePen;               // Pen that is used for drawing in color
  43.   ScribbleWindow(PTWindowsObject AParent, LPSTR ATitle);
  44.   ~ScribbleWindow();
  45.   void GetWindowClass(WNDCLASS &AWndClass);
  46.  
  47.  
  48.   // Dynamic virtual member function that gets called when the left mouse
  49.   // button is clicked in the window.  This method sets up the window for
  50.   // scribbling by creating a display context.
  51.  
  52.   virtual void WMLButtonDown(RTMessage Msg)  = [WM_FIRST + WM_LBUTTONDOWN];
  53.  
  54.  
  55.   // Dynamic virtual member function that gets called when the left mouse
  56.   // button is released in the window.  This method releases the display 
  57.   // context that is used for drawing.
  58.  
  59.   virtual void WMLButtonUp(RTMessage Msg)    = [WM_FIRST + WM_LBUTTONUP];
  60.  
  61.  
  62.   // Dynamic virtual member function that gets called when the mouse is
  63.   // moved anywhere in the window. If the left mouse button is pressed,
  64.   // the window will be scribbled in.
  65.  
  66.   virtual void WMMouseMove(RTMessage Msg)    = [WM_FIRST + WM_MOUSEMOVE];
  67.  
  68.  
  69.   // Dynamic virtual member function that gets called when the right
  70.   // mouse button is clicked in the window.  It clears the window by
  71.   // invalidating the window, causing a WM_PAINT message to be sent.
  72.  
  73.   virtual void WMRButtonDown(RTMessage Msg)  = [WM_FIRST + WM_RBUTTONDOWN];
  74.  
  75.  
  76.   // Dynamic virtual member function that gets called when user selects
  77.   // Pen.Red from the menu bar. Disposes of the current pen and creates
  78.   // a red pen.
  79.  
  80.   virtual void SelectRedPen(RTMessage Msg)   = [CM_FIRST + CM_RED];
  81.  
  82.  
  83.   // Dynamic virtual member function that gets called when user selects
  84.   // Pen.Green from the menu bar. Disposes of the current pen and creates
  85.   // a green pen.
  86.  
  87.   virtual void SelectGreenPen(RTMessage Msg) = [CM_FIRST + CM_GREEN];
  88.  
  89.  
  90.   // Dynamic virtual member function that gets called when user selects
  91.   // Pen.Blue from the menu bar. Disposes of the current pen and creates
  92.   // a blue pen.
  93.  
  94.   virtual void SelectBluePen(RTMessage Msg)  = [CM_FIRST + CM_BLUE];
  95.  
  96.  
  97.   // Dynamic virtual member function that gets called when user selects
  98.   // Pen.Black from the menu bar. Disposes of the current pen and creates
  99.   // a black pen.
  100.  
  101.   virtual void SelectBlackPen(RTMessage Msg) = [CM_FIRST + CM_BLACK];
  102.  
  103.  
  104.  // Redeclares the ObjectWindows function SetupWindow so you
  105.  // can set a breakpoint and obtain the window handle.
  106.  
  107.   virtual void SetupWindow();
  108. };
  109.  
  110. ScribbleWindow::ScribbleWindow(PTWindowsObject AParent, LPSTR ATitle)
  111.                : TWindow(AParent, ATitle)
  112. {
  113.   AssignMenu((LPSTR)MAKEINTRESOURCE(MenuID));
  114.                          // Attach menu from resource file to window.
  115.  
  116.   ThePen = CreatePen(PS_SOLID, PenWidth, 0);
  117.                          // Initialize pen to black.
  118.   ButtonDown = FALSE;
  119. }
  120.  
  121. ScribbleWindow::~ScribbleWindow()
  122. {
  123.   DeleteObject(ThePen);  // Dispose of pen
  124. }
  125.  
  126. /**************************************************** 
  127.  * member function ScribbleWindow::GetWindowClass
  128.  *
  129.  * Changes the window icon to a custom icon
  130.  ****************************************************/
  131.  
  132. void ScribbleWindow::GetWindowClass(WNDCLASS &AWndClass)
  133. {
  134.   TWindow::GetWindowClass(AWndClass);
  135.   AWndClass.hIcon = LoadIcon(GetApplication()->hInstance, 
  136.                     MAKEINTRESOURCE(IconID));
  137. }
  138.  
  139. /********************************************************************** 
  140.  * member function ScribbleWindow::WMLButtonDown
  141.  *
  142.  * Process WM_LBUTTONDOWN messages by marking mouse as being pressed,
  143.  * telling Windows to send all mouse messages to window, and selecting
  144.  * a colored pen into the display context.
  145.  **********************************************************************/
  146.  
  147. void ScribbleWindow::WMLButtonDown(RTMessage Msg)
  148. {
  149.   if ( !ButtonDown )
  150.   {
  151.     ButtonDown = TRUE;           // Mark mouse button as being pressed so
  152.                                  // when mouse movement occurs, a line
  153.                                  // will be drawn.
  154.  
  155.     MoveTo(HandleDC, Msg.LP.Lo, Msg.LP.Hi);
  156.                                  // Move drawing point to location
  157.                                  // where mouse was pressed.
  158.  
  159.     SelectObject(HandleDC, ThePen);
  160.                                  // Select pen into display context.
  161.   }
  162. }
  163.  
  164. /*********************************************************** 
  165.  * member function ScribbleWindow::WMLButtonUp
  166.  *
  167.  * Process WM_LBUTTONUP messages by releasing the display
  168.  * context and marking the mouse button as not being pressed.
  169.  ************************************************************/
  170.  
  171. void ScribbleWindow::WMLButtonUp(RTMessage)
  172. {
  173.   if ( ButtonDown )
  174.   {
  175.     ReleaseDC(HWindow, HandleDC);
  176.     ButtonDown = FALSE;
  177.   }
  178. }
  179.  
  180. /*********************************************************************
  181.  * member function ScribbleWindow::WMRButtonDown
  182.  *
  183.  * Process WM_RBUTTONDOWN messages by erasing the window.
  184.  *********************************************************************/
  185.  
  186. void ScribbleWindow::WMRButtonDown(RTMessage)
  187. {
  188.   UpdateWindow(HWindow);
  189. }
  190.  
  191. /********************************************************
  192.  * member function ScribbleWindow::WM_Mousemove
  193.  *
  194.  * Process WM_MOUSEMOVE messages by drawing a line if the
  195.  * mouse button is marked as being pressed.
  196.  ********************************************************/
  197.  
  198. void ScribbleWindow::WMMouseMove(RTMessage Msg)
  199. {
  200.   if ( ButtonDown )
  201.     LineTo(HandleDC, Msg.LP.Lo, Msg.LP.Hi);
  202.                                    // Draw a line to where the mouse is presently
  203. }
  204.  
  205. /******************************************************************
  206.  * member function ScribbleWindow::SelectRedPen
  207.  *
  208.  * Create a red pen in response to a "Red" selection from Pen menu.
  209.  ******************************************************************/
  210. #pragma argsused
  211. void ScribbleWindow::SelectRedPen(RTMessage Msg)
  212. {
  213.   DeleteObject(ThePen);
  214.   ThePen = CreatePen(PS_SOLID, PenWidth, RGB(255, 0, 0));
  215. }
  216.  
  217. /********************************************************************** 
  218.  * member function ScribbleWindow::SelectGreenPen
  219.  *
  220.  * Create a green pen in response to a "Green" selection from Pen menu.
  221.  **********************************************************************/
  222. #pragma argsused
  223. void ScribbleWindow::SelectGreenPen(RTMessage Msg)
  224. {
  225.   DeleteObject(ThePen);
  226.   ThePen = CreatePen(PS_SOLID, PenWidth, RGB(0, 255, 0));
  227. }
  228.  
  229. /******************************************************************** 
  230.  * member function ScribbleWindow::SelectBluePen
  231.  *
  232.  * Create a blue pen in response to a "Blue" selection from Pen menu.
  233.  ********************************************************************/
  234. #pragma argsused
  235. void ScribbleWindow::SelectBluePen(RTMessage Msg)
  236. {
  237.   DeleteObject(ThePen);
  238.   ThePen = CreatePen(PS_SOLID, PenWidth, RGB(0, 0, 255));
  239. }
  240.  
  241. /**********************************************************************
  242.  * member function ScribbleWindow::SelectBlackPen
  243.  *
  244.  * Create a black pen in response to a "Black" selection from Pen menu.
  245.  **********************************************************************/
  246. #pragma argsused
  247. void ScribbleWindow::SelectBlackPen(RTMessage Msg)
  248. {
  249.   DeleteObject(ThePen);
  250.   ThePen = CreatePen(PS_SOLID, PenWidth, RGB(0, 0, 0));
  251. }
  252.  
  253. /*************************************************************
  254.  * member function ScribbleWindow::SetupWindow
  255.  *
  256.  * Redeclare the ObjectWindows function SetupWindow so you
  257.  * can set a breakpoint on it and obtain the window handle.
  258.  *************************************************************/
  259.  
  260. void ScribbleWindow::SetupWindow()
  261. {
  262.     TWindow::SetupWindow();
  263. }
  264.  
  265.  
  266. /*************************************************************
  267.  * member function CScribbleApplication::InitMainWindow
  268.  *
  269.  * Initialize a Color Scribble window for the main window.
  270.  *************************************************************/
  271.  
  272. void CScribbleApplication::InitMainWindow()
  273. {
  274.   MainWindow = new ScribbleWindow(NULL, Name);
  275. }
  276.  
  277.  
  278. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  279.   LPSTR lpCmdLine, int nCmdShow)
  280. {
  281.   CScribbleApplication MyApp("Color Scribble", hInstance, hPrevInstance,
  282.                lpCmdLine, nCmdShow);
  283.   MyApp.Run();
  284.   return MyApp.Status;
  285. }
  286.