/****************/ /* MopoCad.CPP */ /****************/ // Esimerkki Borland C++ 4.5:n OWL 2.5-kirjaston käytöstä. // Ohjelmalla piirretään viivaa kun hiiri pidetään pohjassa. #include #include #include #include #include #include #include "..\mopocad.rh" typedef TArrayAsVector PointArray; typedef TArrayAsVectorIterator PointIterator; 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; PointArray *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{//, public TVbxEventHandler { // Tässä tarpeeton, ks. TMainWinAbout! public: TDLG_AboutDlg(TWindow *Parent, TResId ResId): TDialog(Parent,ResId) {}; virtual void Ok(); //virtual void Cancel(); 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"); Points = new PointArray(100,0,10); } TMainWindow::~TMainWindow() { DeleteAllPoints(); delete Points; } void TMainWindow::Exit() { PostQuitMessage(0); } void TMainWindow::About() { GetModule()->ExecDialog(new TDLG_AboutDlg(this, "DLG_ABOUT")); // 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->Add(MovePoint); Points->Add(point); } void TMainWindow::EvMouseMove(UINT, TPoint& point) { if ( !ButtonDown ) return; HoldDC->LineTo(point); Points->Add(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 &) { PointIterator pi(*Points); TPoint p; for (pi.Restart(); pi != 0; pi++) { p = pi.Current(); if ( p == MovePoint ) { pi++; hdc.MoveTo(pi.Current()); } else hdc.LineTo(p); } } void TMainWindow::DeleteAllPoints() { Points->Flush(); } 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 argc,char far *argv[]) { #pragma argsused TMopoCadApp MopoCadApp("MopoCad"); int ret = MopoCadApp.Run(); return ret; }