www.pudn.com > C_modem16.zip > A


//---------------------------------------------------------------------------- 
// ObjectWindows - (C) Copyright 1991, 1994 by Borland International 
//   Tutorial application -- step06.cpp 
//---------------------------------------------------------------------------- 
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include "step06.rc" 
 
typedef TArray TPoints; 
typedef TArrayIterator TPointsIterator; 
 
class TDrawWindow : public TWindow { 
  public: 
    TDrawWindow(TWindow* parent = 0); 
   ~TDrawWindow() 
    { 
      delete DragDC; 
      delete Pen; 
      delete Line; 
    } 
 
    void SetPenSize(int newSize); 
 
  protected: 
    TDC* DragDC; 
    int PenSize; 
    TPen* Pen; 
    TPoints* Line; // To store points in line. 
 
    // 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&); 
    void Paint(TDC&, bool, TRect&); 
    void CmFileNew(); 
    void CmFileOpen(); 
    void CmFileSave(); 
    void CmFileSaveAs(); 
    void CmAbout(); 
 
  DECLARE_RESPONSE_TABLE(TDrawWindow); 
}; 
 
DEFINE_RESPONSE_TABLE1(TDrawWindow, TWindow) 
  EV_WM_LBUTTONDOWN, 
  EV_WM_RBUTTONDOWN, 
  EV_WM_MOUSEMOVE, 
  EV_WM_LBUTTONUP, 
  EV_COMMAND(CM_FILENEW, CmFileNew), 
  EV_COMMAND(CM_FILEOPEN, CmFileOpen), 
  EV_COMMAND(CM_FILESAVE, CmFileSave), 
  EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs), 
  EV_COMMAND(CM_ABOUT, CmAbout), 
END_RESPONSE_TABLE; 
 
TDrawWindow::TDrawWindow(TWindow* parent) 
{ 
  Init(parent, 0, 0); 
  DragDC  = 0; 
  PenSize = 1; 
  Pen     = new TPen(TColor::Black, PenSize); 
  Line    = new TPoints(10, 0, 10); 
} 
 
bool 
TDrawWindow::CanClose() 
{ 
  return MessageBox("Do you want to save?", "Drawing has changed", 
                    MB_YESNO | MB_ICONQUESTION) == IDNO; 
} 
 
void 
TDrawWindow::EvLButtonDown(uint, TPoint& point) 
{ 
  Line->Flush(); 
  Invalidate(); 
 
  if (!DragDC) { 
    SetCapture(); 
    DragDC = new TClientDC(*this); 
    DragDC->SelectObject(*Pen); 
    DragDC->MoveTo(point); 
    Line->Add(point); 
  } 
} 
 
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); 
  } 
} 
 
void 
TDrawWindow::EvMouseMove(uint, TPoint& point) 
{ 
  if (DragDC) { 
    DragDC->LineTo(point); 
    Line->Add(point); 
  } 
} 
 
void 
TDrawWindow::EvLButtonUp(uint, TPoint&) 
{ 
  if (DragDC) { 
    ReleaseCapture(); 
    delete DragDC; 
    DragDC = 0; 
  } 
} 
 
void 
TDrawWindow::SetPenSize(int newSize) 
{ 
  delete Pen; 
  PenSize = newSize; 
  Pen     = new TPen(TColor::Black, PenSize); 
} 
 
void 
TDrawWindow::Paint(TDC& dc, bool, TRect&) 
{ 
  bool first = true; 
  TPointsIterator i(*Line); 
 
  dc.SelectObject(*Pen); 
 
  while (i) { 
    TPoint p = i++; 
 
    if (!first) 
      dc.LineTo(p); 
    else { 
      dc.MoveTo(p); 
      first = false; 
    } 
  } 
} 
 
void 
TDrawWindow::CmFileNew() 
{ 
  Line->Flush(); 
  Invalidate(); 
} 
 
void 
TDrawWindow::CmFileOpen() 
{ 
  MessageBox("Feature not implemented", "File Open", MB_OK); 
} 
 
void 
TDrawWindow::CmFileSave() 
{ 
  MessageBox("Feature not implemented", "File Save", MB_OK); 
} 
 
void 
TDrawWindow::CmFileSaveAs() 
{ 
  MessageBox("Feature not implemented", "File Save As", MB_OK); 
} 
 
void 
TDrawWindow::CmAbout() 
{ 
  MessageBox("Feature not implemented", "About Drawing Pad", MB_OK); 
} 
 
class TDrawApp : public TApplication { 
  public: 
    TDrawApp() : TApplication() {} 
 
    void InitMainWindow() 
    { 
      SetMainWindow(new TFrameWindow(0, "Drawing Pad", new TDrawWindow)); 
      GetMainWindow()->AssignMenu("COMMANDS"); 
    } 
}; 
 
int 
OwlMain(int /*argc*/, char* /*argv*/ []) 
{ 
  return TDrawApp().Run(); 
}