/////////////////////////////////////////////////////////// // Softwarebauelemente II, C2.1 // // author: Stephan Brumme // last changes: August 2, 2001 #include "stdafx.h" #include "C2_1.h" #include "C2_1Dlg.h" #include "PolymorphicSet.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CC2_1Dlg Dialogfeld CC2_1Dlg::CC2_1Dlg(CWnd* pParent /*=NULL*/) : CDialog(CC2_1Dlg::IDD, pParent) { //{{AFX_DATA_INIT(CC2_1Dlg) m_Date = 0; m_Time = 0; //}}AFX_DATA_INIT // Beachten Sie, dass LoadIcon unter Win32 keinen nachfolgenden DestroyIcon-Aufruf benötigt m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CC2_1Dlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CC2_1Dlg) DDX_Control(pDX, IDC_LIST, m_List); DDX_DateTimeCtrl(pDX, IDC_DATEPICKER, m_Date); DDX_DateTimeCtrl(pDX, IDC_TIMEPICKER, m_Time); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CC2_1Dlg, CDialog) //{{AFX_MSG_MAP(CC2_1Dlg) ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_BN_CLICKED(IDC_BUTTON_DELETE, OnDelete) ON_BN_CLICKED(IDC_BUTTON_LOAD, OnLoad) ON_BN_CLICKED(IDC_BUTTON_SAVE, OnSave) ON_LBN_DBLCLK(IDC_LIST, OnDblclkList) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CC2_1Dlg Nachrichten-Handler BOOL CC2_1Dlg::OnInitDialog() { CDialog::OnInitDialog(); // Symbol für dieses Dialogfeld festlegen. Wird automatisch erledigt // wenn das Hauptfenster der Anwendung kein Dialogfeld ist SetIcon(m_hIcon, TRUE); // Großes Symbol verwenden SetIcon(m_hIcon, FALSE); // Kleines Symbol verwenden // ZU ERLEDIGEN: Hier zusätzliche Initialisierung einfügen // initialize random generator srand((unsigned)time(NULL)); // set timer to refresh m_Time SetTimer(1, 1000, NULL); m_Date = m_Time = m_Moment; UpdateData(FALSE); return TRUE; // Geben Sie TRUE zurück, außer ein Steuerelement soll den Fokus erhalten } // Wollen Sie Ihrem Dialogfeld eine Schaltfläche "Minimieren" hinzufügen, benötigen Sie // den nachstehenden Code, um das Symbol zu zeichnen. Für MFC-Anwendungen, die das // Dokument/Ansicht-Modell verwenden, wird dies automatisch für Sie erledigt. void CC2_1Dlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // Gerätekontext für Zeichnen SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Symbol in Client-Rechteck zentrieren int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Symbol zeichnen dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // Die Systemaufrufe fragen den Cursorform ab, die angezeigt werden soll, während der Benutzer // das zum Symbol verkleinerte Fenster mit der Maus zieht. HCURSOR CC2_1Dlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } // a derived class, just used to show polymorphism // no new attributes and/or operations are introduced class CMomentDerived : public CMoment { DECLARE_SERIAL(CMomentDerived) public: CMomentDerived() {}; ~CMomentDerived() {}; }; IMPLEMENT_SERIAL(CMomentDerived, CMoment, 1) // insert element void CC2_1Dlg::OnOK() { CMomentDerived derived; CMoment* moment; // use derived class at random if (rand()SetYear (m_Date.GetYear()); moment->SetMonth (m_Date.GetMonth()); moment->SetDay (m_Date.GetDay()); moment->SetHour (m_Time.GetHour()); moment->SetMinute(m_Time.GetMinute()); moment->SetSecond(m_Time.GetSecond()); // add to the set if (m_Set.Insert(moment) != -1) UpdateListBox(); else AfxMessageBox("Zeitpunkt schon in der Menge enthalten !", MB_ICONSTOP); } // delete a entry from the listbox void CC2_1Dlg::OnDelete() { // save current selection int nSelected = m_List.GetCurSel(); if (nSelected < 0) return; // move cursor to the selection delete m_Set.GetFirst(); while (nSelected-- > 0) delete m_Set.GetNext(); // and delete m_Set.Scratch(); // redraw the listbox UpdateListBox(); } // deserialize void CC2_1Dlg::OnLoad() { CFileDialog dlg(TRUE); dlg.DoModal(); CFile file(dlg.GetPathName(), CFile::modeRead); CArchive ar (&file, CArchive::load); m_Set.Serialize(ar); // redraw the listbox UpdateListBox(); } // serialize void CC2_1Dlg::OnSave() { CFileDialog dlg(FALSE); dlg.DoModal(); CFile file(dlg.GetPathName(), CFile::modeCreate|CFile::modeWrite); CArchive ar (&file, CArchive::store); m_Set.Serialize(ar); } // redraw listbox void CC2_1Dlg::UpdateListBox() { // save index of selected item int nSelected = m_List.GetCurSel(); // delete all elements m_List.ResetContent(); int nElements = m_Set.Card(); // empty list ? if (nElements == 0) return; CMoment* obj = m_Set.GetFirst(); while (nElements > 0) { // display some info CString info; info.Format("%02d.%02d.%04d - %02d:%02d:%02d", obj->GetDay(), obj->GetMonth(), obj->GetYear(), obj->GetHour(), obj->GetMinute(), obj->GetSecond()); // look for derived classes (they prove polymorphism !) if (obj->IsKindOf(RUNTIME_CLASS(CMomentDerived))) info += " (abgeleitet)"; delete obj; m_List.AddString(info); nElements--; // if the end is not reached then get next element if (nElements > 0) obj = m_Set.GetNext(); } // set selection if (nSelected == -1 || nSelected > m_List.GetCount()) nSelected = m_List.GetCount()-1; m_List.SetCurSel(nSelected); } // change both pickers void CC2_1Dlg::OnDblclkList() { // save current selection int nSelected = m_List.GetCurSel(); if (nSelected < 0) return; // move cursor to the selection, delete all generated elements delete m_Set.GetFirst(); while (nSelected-- > 0) delete m_Set.GetNext(); // even CMomentDerived can be handled this way CMoment* moment = m_Set.GetCurrent(); m_Date = *moment; m_Time = *moment; delete moment; // update both pickers UpdateData(FALSE); #ifdef _DEBUG afxDump << m_Set; #endif }