TDrawWindow::TDrawWindow(TWindow *parent) { Init(parent, 0, 0); DragDC = 0; PenSize = 1; Pen = new TPen(TColor::Black, PenSize); }The TColor::Black object in the TPen constructor is an enum defined in the owl\color.h header file. This makes the pen black. You'll learn more about this parameter of the TPen constructor later on in Step 9.
You can use SelectObject to select a variety of objects into a device context, including brushes, fonts, palettes, and pens. You need to call SelectObject before you begin to draw. Add the call in the EvLButtonDown function immediately after you create the device context:
void TDrawWindow::EvLButtonDown(uint, TPoint& point) { Invalidate(); if (!DragDC) { SetCapture(); DragDC = new TClientDC(*this); DragDC->SelectObject(*Pen); DragDC->MoveTo(point); } }Notice that Pen is dereferenced in the SelectObject call. This is because the SelectObject function takes a TPen & for its parameter, and Pen is a TPen *. Dereferencing the pointer makes Pen comply with SelectObject's type requirements.
Once the user has indicated that he or she wants to change the pen width by pressing the right mouse button, you need to find some way to let the user enter the new pen width. For this, you can pop up a TInputDialog, in which the user can input the pen size.
TInputDialog(TWindow* parent, const char far* title, const char far* prompt, char far* buffer, int bufferSize, TModule* module = 0);where:
The Execute function for TInputDialog can return two important values, IDOK and IDCANCEL. The value that is returned depends on which button the user presses. If the user presses the OK button, Execute returns IDOK. If the user presses the Cancel button, Execute returns IDCANCEL. So when you execute the input dialog box, you need to make sure that the return value is IDOK before changing the pen size. If it's not, then leave the pen size the same as it is.
If the call to Execute does return IDOK, the new value for PenSize is in the string passed in for the dialog's buffer. Before this can be used as a pen size, it must be converted to an int. Then you should make sure that the value you get from the buffer is a valid pen width. Finally, once you're sure that the input from the user is acceptable, you can change the pen size. TDrawWindow now has a function called SetPenSize that you can use to change the pen size. The reason for doing it this way, instead of directly modifying the pen, is explained in the next section.
The EvRButtonDown function should now look something like this:
void TDrawWindow::EvRButtonDown(uint, TPoint&) { char inputText[6]; wsprintf(inputText, "%d", PenSize); if ((TInputDialog(this, "Line Thickness", "Input a new thickness:", inputText, sizeof(inputText))).Execute() == IDOK) { int newPenSize = atoi(inputText); if (newPenSize < 0) newPenSize = 1; SetPenSize(newPenSize); } }
For TDrawWindow, you have the public SetPenSize function. The SetPenSize function takes one parameter, an int that contains the new width for the pen. After opening the input dialog box, processing the input, and checking the validity of the result, all you need to do is call SetPenSize.
SetPenSize is a fairly simple function. To resize the pen, you must first delete the existing pen object. Then set PenSize to the new size. Finally construct a new pen object with the new pen size. The function should look something like this:
void TDrawWindow::SetPenSize(int newSize) { delete Pen; PenSize = newSize; Pen = new TPen(TColor(0,0,0), PenSize); }
class TDrawWindow : public TWindow { public: TDrawWindow(TWindow *parent = 0); ~TDrawWindow() {delete DragDC; delete Pen;} void SetPenSize(int newSize); protected: TDC *DragDC; int PenSize; TPen *Pen; // Override member function of TWindow bool CanClose(); // Message response functions void EvLButtonDown(uint, TPoint&); void EvRButtonDown(uint, TPoint&); void EvMouseMove(uint, TPoint&); void EvLButtonUp(uint, TPoint&); DECLARE_RESPONSE_TABLE(TDrawWindow); };