/****************/ /* MopoCad.CPP */ /****************/ // Esimerkki Borland C++ 5.0:n OWL 5.0-kirjaston käytöstä. // Ohjelmalla piirretään viivaa kun hiiri pidetään pohjassa. #include #include #include #include #include #include "..\mopocad.rh" #include #pragma warn -aus using namespace std; typedef deque cPoints; typedef cPoints::iterator cPointsIterator; TPoint MovePoint(-2000,-2000); // Merkki taulukkoon siirrosta /************************ Luokat **********************************************/ class TMopoCadApp : public TApplication { public: TMopoCadApp(const char far *name = 0) : TApplication(name) {}; virtual void InitMainWindow(); virtual void InitInstance(); }; class TMainWindow : public TFrameWindow { TClientDC* HoldDC; BOOL ButtonDown; cPoints Points; protected: LPSTR GetClassName() { return "MopoCadWndClass"; } void DeleteAllPoints(); void ClearWindow(); public: TMainWindow(TWindow *Parent, LPSTR ATitle); ~TMainWindow(); virtual void Exit(); virtual void About(); void EvLButtonDown(UINT modKeys, TPoint& point); void EvLButtonUp(UINT modKeys, TPoint& point); void EvMouseMove(UINT modKeys, TPoint& point); void EvRButtonDown(UINT modKeys, TPoint& point); void Paint(TDC &hdc,bool erase, TRect &rect); DECLARE_RESPONSE_TABLE(TMainWindow); }; /********************* Pääikkunan tapahtumataulu ******************************/ DEFINE_RESPONSE_TABLE1(TMainWindow,TFrameWindow) EV_COMMAND(CM_FILE_EXIT, Exit), EV_COMMAND(CM_HELP_ABOUT, About), EV_WM_LBUTTONDOWN, EV_WM_LBUTTONUP, EV_WM_MOUSEMOVE, EV_WM_RBUTTONDOWN, EV_WM_PAINT, END_RESPONSE_TABLE; class TDLG_AboutDlg : public TDialog{ // Tässä tarpeeton, ks. TMainWin::About! public: TDLG_AboutDlg(TWindow *Parent, TResId ResId): TDialog(Parent,ResId) {}; virtual void Ok(); DECLARE_RESPONSE_TABLE(TDLG_AboutDlg); }; DEFINE_RESPONSE_TABLE1(TDLG_AboutDlg,TDialog) EV_COMMAND(IDOK, Ok), END_RESPONSE_TABLE; /********************* TDLG_AboutDlg metodit: *********************************/ void TDLG_AboutDlg::Ok() { Destroy(); } /********************* TMainWindow metodit ************************************/ TMainWindow::TMainWindow(TWindow *Parent, LPSTR ATitle) : TFrameWindow(Parent, ATitle) { ButtonDown = FALSE; SetIcon(GetModule(),"IKONI"); Attr.AccelTable = "PIKA"; AssignMenu("PaaMenu"); } TMainWindow::~TMainWindow() { DeleteAllPoints(); } void TMainWindow::Exit() { PostQuitMessage(0); } void TMainWindow::About() { // GetModule()->ExecDialog(new TDLG_AboutDlg(this, "DLG_ABOUT")); TDialog(this, "DLG_ABOUT").Create(); // TDialog(this, "DLG_ABOUT").Execute(); // Tosin voitaisiin kutsua suoraan geneeristä TDialog ja jättää // koko TDLG_AboutDlg luokka pois! // GetModule()->ExecDialog(new TDialog(this, "DLG_ABOUT")); } void TMainWindow::EvLButtonDown(UINT, TPoint& point) { if ( ButtonDown) return; ButtonDown = TRUE; SetCapture(); // Direct all subsequent mouse input to this window HoldDC = new TClientDC(HWindow); HoldDC->MoveTo(point); Points.push_back(MovePoint); Points.push_back(point); } void TMainWindow::EvMouseMove(UINT, TPoint& point) { if ( !ButtonDown ) return; HoldDC->LineTo(point); Points.push_back(point); } void TMainWindow::EvLButtonUp(UINT, TPoint&) { if ( !ButtonDown ) return; ReleaseCapture(); delete HoldDC; HoldDC = 0; ButtonDown = FALSE; } void TMainWindow::EvRButtonDown(UINT, TPoint&) { if ( ButtonDown ) return; ClearWindow(); } void TMainWindow::Paint(TDC &hdc,bool, TRect &) { cPointsIterator pi; for (pi=Points.begin(); pi != Points.end(); pi++) if ( *pi == MovePoint ) hdc.MoveTo(*++pi); else hdc.LineTo(*pi); } void TMainWindow::DeleteAllPoints() { Points.erase(Points.begin(),Points.end()); } void TMainWindow::ClearWindow() { DeleteAllPoints(); Invalidate(TRUE); } /********************* TMopoCadApp metodit ************************************/ void TMopoCadApp::InitInstance() { TApplication::InitInstance(); } void TMopoCadApp::InitMainWindow() { MainWindow = new TMainWindow(NULL, Name); } /********************* Pääohjelma *********************************************/ int OwlMain(int ,char far *[]) { /* Huom! varmista että edellä on char *[], minulla meni 2h kun ei ollut! */ TMopoCadApp MopoCadApp("MopoCad"); int ret = MopoCadApp.Run(); return ret; }