sources:
C2_1.cpp (2.2k)
C2_1.h (1.4k)
C2_1Dlg.cpp (6.8k)
C2_1Dlg.h (1.7k)
Moment.cpp (5.7k)
Moment.h (2.7k)
PolymorphicSet.h (9.8k)
StdAfx.cpp (208 bytes)
StdAfx.h (1.1k)
resource.h (774 bytes)


binaries:
Release/C2_1.exe (28.0k)


website:
more info here
studies/bauelemente/Softwarebauelemente-CodeC2-1/C2_1Dlg.cpp
download file

  1 ///////////////////////////////////////////////////////////
  2 // Softwarebauelemente II, C2.1
  3 //
  4 // author: Stephan Brumme
  5 // last changes: August 2, 2001
  6
  7
  8 #include "stdafx.h"
  9 #include "C2_1.h"
 10 #include "C2_1Dlg.h"
 11 #include "PolymorphicSet.h"
 12
 13 #ifdef _DEBUG
 14 #define new DEBUG_NEW
 15 #undef THIS_FILE
 16 static char THIS_FILE[] = __FILE__;
 17 #endif
 18
 19 /////////////////////////////////////////////////////////////////////////////
 20 // CC2_1Dlg Dialogfeld
 21
 22 CC2_1Dlg::CC2_1Dlg(CWnd* pParent /*=NULL*/)
 23     : CDialog(CC2_1Dlg::IDD, pParent)
 24 {
 25     //{{AFX_DATA_INIT(CC2_1Dlg)
 26     m_Date = 0;
 27     m_Time = 0;
 28     //}}AFX_DATA_INIT
 29     // Beachten Sie, dass LoadIcon unter Win32 keinen nachfolgenden DestroyIcon-Aufruf benötigt
 30     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 31 }
 32
 33 void CC2_1Dlg::DoDataExchange(CDataExchange* pDX)
 34 {
 35     CDialog::DoDataExchange(pDX);
 36     //{{AFX_DATA_MAP(CC2_1Dlg)
 37     DDX_Control(pDX, IDC_LIST, m_List);
 38     DDX_DateTimeCtrl(pDX, IDC_DATEPICKER, m_Date);
 39     DDX_DateTimeCtrl(pDX, IDC_TIMEPICKER, m_Time);
 40     //}}AFX_DATA_MAP
 41 }
 42
 43 BEGIN_MESSAGE_MAP(CC2_1Dlg, CDialog)
 44     //{{AFX_MSG_MAP(CC2_1Dlg)
 45     ON_WM_PAINT()
 46     ON_WM_QUERYDRAGICON()
 47     ON_BN_CLICKED(IDC_BUTTON_DELETE, OnDelete)
 48     ON_BN_CLICKED(IDC_BUTTON_LOAD, OnLoad)
 49     ON_BN_CLICKED(IDC_BUTTON_SAVE, OnSave)
 50     ON_LBN_DBLCLK(IDC_LIST, OnDblclkList)
 51     //}}AFX_MSG_MAP
 52 END_MESSAGE_MAP()
 53
 54 /////////////////////////////////////////////////////////////////////////////
 55 // CC2_1Dlg Nachrichten-Handler
 56
 57 BOOL CC2_1Dlg::OnInitDialog()
 58 {
 59     CDialog::OnInitDialog();
 60
 61     // Symbol für dieses Dialogfeld festlegen. Wird automatisch erledigt
 62     // wenn das Hauptfenster der Anwendung kein Dialogfeld ist
 63     SetIcon(m_hIcon, TRUE);            // Großes Symbol verwenden
 64     SetIcon(m_hIcon, FALSE);        // Kleines Symbol verwenden
 65    
 66     // ZU ERLEDIGEN: Hier zusätzliche Initialisierung einfügen
 67
 68     // initialize random generator
 69     srand((unsigned)time(NULL));
 70
 71     // set timer to refresh m_Time
 72     SetTimer(1, 1000, NULL);
 73
 74     m_Date = m_Time = m_Moment;
 75     UpdateData(FALSE);
 76
 77     return TRUE;  // Geben Sie TRUE zurück, außer ein Steuerelement soll den Fokus erhalten
 78 }
 79
 80 // Wollen Sie Ihrem Dialogfeld eine Schaltfläche "Minimieren" hinzufügen, benötigen Sie
 81 // den nachstehenden Code, um das Symbol zu zeichnen. Für MFC-Anwendungen, die das
 82 // Dokument/Ansicht-Modell verwenden, wird dies automatisch für Sie erledigt.
 83
 84 void CC2_1Dlg::OnPaint()
 85 {
 86     if (IsIconic())
 87     {
 88         CPaintDC dc(this); // Gerätekontext für Zeichnen
 89
 90         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
 91
 92         // Symbol in Client-Rechteck zentrieren
 93         int cxIcon = GetSystemMetrics(SM_CXICON);
 94         int cyIcon = GetSystemMetrics(SM_CYICON);
 95         CRect rect;
 96         GetClientRect(&rect);
 97         int x = (rect.Width() - cxIcon + 1) / 2;
 98         int y = (rect.Height() - cyIcon + 1) / 2;
 99
100         // Symbol zeichnen
101         dc.DrawIcon(x, y, m_hIcon);
102     }
103     else     {
104         CDialog::OnPaint();
105     }
106 }
107
108 // Die Systemaufrufe fragen den Cursorform ab, die angezeigt werden soll, während der Benutzer
109 // das zum Symbol verkleinerte Fenster mit der Maus zieht.
110 HCURSOR CC2_1Dlg::OnQueryDragIcon()
111 {
112     return (HCURSOR) m_hIcon;
113 }
114
115
116 // a derived class, just used to show polymorphism
117 // no new attributes and/or operations are introduced
118 class CMomentDerived : public CMoment {
119     DECLARE_SERIAL(CMomentDerived)
120
121 public:
122     CMomentDerived() {};
123     ~CMomentDerived() {};
124 };
125 IMPLEMENT_SERIAL(CMomentDerived, CMoment, 1)
126
127
128
129 // insert element
130 void CC2_1Dlg::OnOK()
131 {
132     CMomentDerived derived;
133     CMoment* moment;
134
135     // use derived class at random
136     if (rand()<RAND_MAX/3)
137         moment = &derived;
138     else         moment = &m_Moment;
139
140     // actually set time/date
141     UpdateData(TRUE);
142     moment->SetYear  (m_Date.GetYear());
143     moment->SetMonth (m_Date.GetMonth());
144     moment->SetDay   (m_Date.GetDay());
145     moment->SetHour  (m_Time.GetHour());
146     moment->SetMinute(m_Time.GetMinute());
147     moment->SetSecond(m_Time.GetSecond());
148
149     // add to the set
150     if (m_Set.Insert(moment) != -1)
151         UpdateListBox();
152     else         AfxMessageBox("Zeitpunkt schon in der Menge enthalten !", MB_ICONSTOP);
153 }
154
155
156 // delete a entry from the listbox
157 void CC2_1Dlg::OnDelete()
158 {
159     // save current selection
160     int nSelected = m_List.GetCurSel();
161     if (nSelected < 0)
162         return;
163
164     // move cursor to the selection
165     delete m_Set.GetFirst();
166     while (nSelected-- > 0)
167         delete m_Set.GetNext();
168        
169     // and delete
170     m_Set.Scratch();
171
172     // redraw the listbox
173     UpdateListBox();
174 }
175
176
177 // deserialize
178 void CC2_1Dlg::OnLoad()
179 {
180     CFileDialog dlg(TRUE);
181     dlg.DoModal();
182
183     CFile    file(dlg.GetPathName(), CFile::modeRead);
184     CArchive ar  (&file, CArchive::load);
185
186     m_Set.Serialize(ar);
187
188     // redraw the listbox
189     UpdateListBox();
190 }
191
192
193 // serialize
194 void CC2_1Dlg::OnSave()
195 {
196     CFileDialog dlg(FALSE);
197     dlg.DoModal();
198
199     CFile    file(dlg.GetPathName(), CFile::modeCreate|CFile::modeWrite);
200     CArchive ar  (&file, CArchive::store);
201
202     m_Set.Serialize(ar);
203 }
204
205
206 // redraw listbox
207 void CC2_1Dlg::UpdateListBox()
208 {
209     // save index of selected item
210     int nSelected = m_List.GetCurSel();
211
212     // delete all elements
213     m_List.ResetContent();
214
215     int nElements = m_Set.Card();
216
217     // empty list ?
218     if (nElements == 0)
219         return;
220
221     CMoment* obj = m_Set.GetFirst();
222     while (nElements > 0)
223     {
224         // display some info
225         CString info;
226         info.Format("%02d.%02d.%04d - %02d:%02d:%02d",
227                     obj->GetDay(),  obj->GetMonth(),  obj->GetYear(),
228                     obj->GetHour(), obj->GetMinute(), obj->GetSecond())
;
229
230         // look for derived classes (they prove polymorphism !)
231         if (obj->IsKindOf(RUNTIME_CLASS(CMomentDerived)))
232             info += " (abgeleitet)";
233
234         delete obj;
235         m_List.AddString(info);
236
237         nElements--;
238         // if the end is not reached then get next element
239         if (nElements > 0)
240             obj = m_Set.GetNext();
241     }
242
243     // set selection
244     if (nSelected == -1 || nSelected > m_List.GetCount())
245         nSelected = m_List.GetCount()-1;
246     m_List.SetCurSel(nSelected);
247 }
248
249
250 // change both pickers
251 void CC2_1Dlg::OnDblclkList()
252 {
253     // save current selection
254     int nSelected = m_List.GetCurSel();
255     if (nSelected < 0)
256         return;
257
258     // move cursor to the selection, delete all generated elements
259     delete m_Set.GetFirst();
260     while (nSelected-- > 0)
261         delete m_Set.GetNext();
262        
263     // even CMomentDerived can be handled this way
264     CMoment* moment = m_Set.GetCurrent();
265     m_Date = *moment;
266     m_Time = *moment;
267     delete moment;
268
269     // update both pickers
270     UpdateData(FALSE);
271
272 #ifdef _DEBUG
273     afxDump << m_Set;
274 #endif
275 }
276
277