/****************/ /* MopoCad.CPP */ /****************/ // Esimerkki Borland C++ 3.1:n OWL-kirjaston käytöstä. // Ohjelmalla piirretään viivaa kun hiiri pidetään pohjassa. #include #include #include "..\mopocad.rh" class MPoint { // Piste joka on murtoviivan osa. int x; int y; public: MPoint(POINT p) { x = p.x; y = p.y; } MPoint(int ix,int iy) { x = ix; y = iy; } MPoint(LPARAM lp=0) { x = LOWORD(lp); y = HIWORD(lp); } int operator==(MPoint &p) { return ( p.x == x && p.y == y ); } void lineto(HDC hdc) { LineTo(hdc,x,y); } void moveto(HDC hdc) { MoveTo(hdc,x,y); } }; MPoint MovePoint(-2000,-2000); // Merkki taulukkoon siirrosta typedef BI_ArrayAsVector PointArray; typedef BI_ArrayAsVectorIterator PointIterator; /************************ Luokat *******************************************/ class TMopoCadApp : public TApplication { public: TMopoCadApp(LPSTR AName, HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) : TApplication(AName, hInstance, hPrevInstance, lpCmdLine, nCmdShow) {}; virtual void InitMainWindow(); virtual void InitInstance(); }; class TMainWindow : public TWindow { HDC HoldDC; int ButtonDown; // Onko hiiren nappi alhaalla PointArray *Points; protected: LPSTR GetClassName() { return "MopoCadWndClass"; } void GetWindowClass(WNDCLASS _FAR & AWndClass); void DeleteAllPoints(); void ClearWindow(); public: TMainWindow(PTWindowsObject AParent, LPSTR ATitle); ~TMainWindow(); virtual void Exit(RTMessage Msg) = [CM_FIRST + CM_FILE_EXIT ]; virtual void About(RTMessage Msg) = [CM_FIRST + CM_HELP_ABOUT ]; virtual void WMLButtonDown(RTMessage Msg) = [WM_FIRST + WM_LBUTTONDOWN]; virtual void WMMouseMove(RTMessage Msg) = [WM_FIRST + WM_MOUSEMOVE ]; virtual void WMLButtonUp(RTMessage Msg) = [WM_FIRST + WM_LBUTTONUP ]; virtual void WMRButtonDown(RTMessage Msg) = [WM_FIRST + WM_RBUTTONDOWN]; void Paint(HDC hdc,PAINTSTRUCT &ps); }; class TDLG_AboutDlg : public TDialog { // Tässä tarpeeton, ks. TMainWinAbout! public: TDLG_AboutDlg(PTWindowsObject AParent, LPSTR AName): TDialog(AParent, AName) {}; //~TDLG_ABoutDlg(); virtual void Ok(RTMessage Msg) = [ID_FIRST + IDOK]; //virtual void Cancel(RTMessage Msg) = [ID_FIRST + IDCANCEL]; }; /********************* TDLG_AboutDlg metodit: ******************************/ void TDLG_AboutDlg::Ok(RTMessage) { Destroy(); } /********************* TMainWindow metodit *********************************/ TMainWindow::TMainWindow(PTWindowsObject AParent, LPSTR ATitle) : TWindow(AParent, ATitle) { AssignMenu("PaaMenu"); ButtonDown = FALSE; Points = new PointArray(100,0,10); } TMainWindow::~TMainWindow() { DeleteAllPoints(); delete Points; } void TMainWindow::GetWindowClass(WNDCLASS _FAR & AWndClass) { TWindow::GetWindowClass(AWndClass); // AWndClass.hbrBackground = CreateSolidBrush(0xFFFFFFL); AWndClass.hIcon = LoadIcon(AWndClass.hInstance, "IKONI"); } void TMainWindow::Exit(RTMessage msg) { CMExit(msg); } void TMainWindow::About(RTMessage) { 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::WMLButtonDown(RTMessage Msg) { if ( ButtonDown ) return; ButtonDown = TRUE; SetCapture(HWindow); HoldDC = GetDC(HWindow); MPoint point(Msg.LParam); point.moveto(HoldDC); Points->add(MovePoint); Points->add(point); } void TMainWindow::WMLButtonUp(RTMessage) { ButtonDown = FALSE; ReleaseCapture(); ReleaseDC(HWindow,HoldDC); } void TMainWindow::WMMouseMove(RTMessage Msg) { if ( !ButtonDown ) return; MPoint point(Msg.LParam); point.lineto(HoldDC); Points->add(point); } void TMainWindow::Paint(HDC hdc,PAINTSTRUCT &) { PointIterator pi(*Points); MPoint p; BOOL move = TRUE; for (pi.restart(0,Points->getItemsInContainer()); pi != 0; pi++) { p = pi.current(); if ( p == MovePoint ) { move = TRUE; continue; } if ( move ) p.moveto(hdc); else p.lineto(hdc); move = FALSE; } } void TMainWindow::WMRButtonDown(RTMessage) { if ( ButtonDown ) return; ClearWindow(); } void TMainWindow::DeleteAllPoints() { Points->flush(); } void TMainWindow::ClearWindow() { DeleteAllPoints(); InvalidateRect(HWindow,NULL,TRUE); } /********************* TMopoCadApp metodit *********************************/ void TMopoCadApp::InitInstance() { TApplication::InitInstance(); HAccTable = LoadAccelerators(hInstance, "PIKA"); } void TMopoCadApp::InitMainWindow() { MainWindow = new TMainWindow(NULL, Name); } /********************* Pääohjelma ******************************************/ int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmd, int nCmdShow) { TMopoCadApp MopoCadApp("MopoCad",hInstance,hPrevInstance, lpCmd,nCmdShow); MopoCadApp.Run(); return MopoCadApp.Status; }