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

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