Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class.

Similar presentations


Presentation on theme: "Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class."— Presentation transcript:

1 Chapter 6: FILE I/O and Serialize CFile class

2 FILE I/O Serialization and the CArchive Class

3 3 Serialization basics Concept of serialization CArchive ar(...); ar << a << b; CArchive ar(...); ar >> a >> b; CArchive ObjectCFile Object Local disk a, b

4 4 Serialization basics CArchive Class constructor: –pFile Pointer to CFile object –nMode CArchive::load or CArchive::store –nBufSize Buffer size (don’t need to change it) –lpBuf Buffer address (don’t need to change it) CArchive::CArchive (CFile* pFile, UINT nMode, int nBufSize = 4096, void* lpBuf = NULL) ;

5 5 Serialization basics Data types ready for the serialization Data types Basic data types BYTE, WORD, LONG, DWORD, float, double, int, short, char, wchar_t, unsigned, bool, ULONGLONG, LONGLONG MFC data types RECT, POINT, SIZE, CRect, CPoint, CSize, CString, CTime, CTimeSpan, COleVariant, COleCurrency, COleDateTime, COleDataTimeSpan

6 6 Serialization basics Read data by serialization CFile file; CFileException e; if(!file.Open("mytest.dat", CFile::modeReadWrite|CFile::modeCreate, &e)) { e.ReportError(); return; } int a = 100; int b = 200; CArchive ar (&file, CArchive::store); ar << a << b;

7 7 Serialization basics Write data by serialization CFile file; CFileException e; if(!file.Open("mytest.dat", CFile::modeRead, &e)) { e.ReportError(); return; } int a, b; CArchive ar (&file, CArchive::load); ar >> a >> b; TRACE("a = %d, b = %d\n", a, b);

8 Coding practice II Draw many circles by using mouse left clicks. Add menu ‘Save’ and save the information of the circles as “circle.dat” file Add menu ‘Load’ and load the data from the file “circle.dat”.

9 9 Serialization Implementation (1/5) How to make your own class to support the serialization? class CMyData { public: CString m_str; COLORREF m_color; public: CMyData(CString &str, COLORREF &color) { m_str = str; m_color = color; } virtual ~CMyData(); };

10 10 Serialization Implementation (2/5) It will not support any serialization by default void CYourProgram::SaveOrLoad(CArchive& ar) { if (ar.IsStoring()) { ar << m_data; } else { ar >> m_data; } X

11 11 Serialization Implementation (3/5) Change your class to support the serialization // in your header file class CMyData : public CObject ① { DECLARE_SERIAL(CMyData) ② public: CString m_str; COLORREF m_color; public: CMyData() { } ③ CMyData(CString &str, COLORREF &color) { m_str = str; m_color = color; } virtual ~CMyData(); void Serialize(CArchive& ar); ④ };

12 12 Serialization Implementation (4/5) Change your class to support the serialization (cont'd) // In your cpp file CMyData::~CMyData() { } IMPLEMENT_SERIAL(CMyData, CObject, 1) ⑤ void CMyData::Serialize (CArchive& ar) ⑥ { CObject::Serialize(ar); if(ar.IsStoring()) ar << m_str << m_color; else ar >> m_str >> m_color; }

13 13 Serialization Implementation (5/5) Now your own class support serialization void CYourProgram ::SaveOrLoad(CArchive& ar) { if (ar.IsStoring()) { m_data.Serialize(ar); } else { m_data.Serialize(ar); } O

14 Downside of the Serialize The whole file should be read The whole file should be written Extra information should be stored.

15 Chapter 7 Controls

16 16 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium of windows programming and promote a consistent user interface

17 Introduction Example: push button control –Don’t need to draw a rectangle on the screen –Don’t need to process the mouse message –Everything is almost done. Just add it!!! –Notification will be given when clicked.

18 18 Introduction MFC Classic Controls Control TypeWNDCLASSMFC Class Buttons"BUTTON"CButton List boxes"LISTBOX"CListBox Edit controls"EDIT"CEdit Combo boxes"COMBOBOX"CComboBox Scroll bars"SCROLLBAR"CScrollBar Static controls"STATIC"CStatic

19 19 How it works Controls and its parent window –Notification Message When its states change Example) A button is being clicked –Member Functions of the control Change or get the state of the control. Example) Change a button to be inactive Controls (Child Window) Controls (Child Window) Parent Window ① Notification ② function call

20 20 MFC Class Hierarchy A control is also a window!

21 21 Button Control (1/8) Various kinds of the Button Control

22 22 Button Control Two ways of creating a control ① Create one by hand (coding) ② Create one using the dialog edit tool

23 23 Button control by hand Procedure: –1. Adding CButton object as a member variable –2. Call CButton‘s Create(..) member function to create Ex) Call it inside CChildView::OnCreate() function m_button.Create( _T(“Push Here!“), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, CRect(100, 100, 200, 130), this, 101); In CChildView class : ChildView.h CButton m_button; // adding CButton object

24 24 Button Control CButton::Create() member function –Caption : title string –Style : Style as a button and as a window Ex) WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON –Rect : position and size –pParentWnd : pointer to parent window –UINT nID : ID of the control (ex, 101, 102…) BOOL CButton::Create (Caption, Style, rect, pParentWindow, ID) ; Windows styleCButton style

25 25 Styles of CButton StyleDescription BS_PUSHBUTTONCreates a standard push button control BS_DEFPUSHBUTTONCreates a default push button; used in dialog boxes to identify the push button that's clicked if Enter is pressed BS_CHECKBOXCreates a check box control BS_AUTOCHECKBOXCreates a check box control that checks and unchecks itself when clicked BS_3STATECreates a three-state check box control BS_AUTO3STATECreates a three-state check box control that cycles through three states—checked, unchecked, and indetermin ate—when clicked BS_RADIOBUTTONCreates a radio button control BS_AUTORADIOBUTTO N Creates a radio button control that, when clicked, checks itself and unchecks other radio buttons in the group BS_GROUPBOXCreates a group box control

26 Additional Styles of CButton StyleDescription BS_LEFTTEXTMoves the text accompanying a radio button or check box c ontrol from the button's right (the default) to its left BS_RIGHTBUTTONSame as BS_LEFTTEXT BS_LEFTLeft justifies the button text in the control rectangle BS_CENTERCenters the button text in the control rectangle BS_RIGHTRight justifies the button text in the control rectangle BS_TOPPositions the button text at the top of the control rectangle BS_VCENTERPositions the button text in the center of the control rectangl e vertically BS_BOTTOMPositions the button text at the bottom of the control rectangl e BS_MULTILINEAllows text too long to fit on one line to be broken into two or more lines

27 When clicking a button Windows sends Notification Message Message Handler for the notification 1.Add message handler MACRO in massage map 2.Implement the message handler function Controls (Child Window) Controls (Child Window) Parent Window Notification

28 28 Adding Message Handler 1. Add message handler MACRO in massage map 2. Implement the message handler function ON_BN_CLICKED( ID, function name) Ex) ON_BN_CLICKED( 101, OnButtonClicked) // in massage map void CChildView::OnButtonClicked( ) // message handler { AfxMessageBox(_T(“Button is clicked.")); }

29 When changing state of CButton Call member functions of CButton –Change(Set) the current state –Acquire(Get) the current state Controls (Child Window) Controls (Child Window) Parent Window function call

30 30 Member functions of CButton 1.Change the state of the check box 2.Check the state of the check box m_button.SetCheck(BST_CHECKED); m_button.SetCheck(BST_UNCHECKED); if ( m_button.GetCheck() == BST_CHECKED ) AfxMessageBox(_T(“Button is checked”));

31 Radio button Only one at a time is checked. They are connected. You can make it manually // Radio Button Manual Creation Example void CChildView::OnRadioButton1Clicked () { m_wndRadioButton1.SetCheck (BST_CHECKED); m_wndRadioButton2.SetCheck (BST_UNCHECKED); m_wndRadioButton3.SetCheck (BST_UNCHECKED); } // Radio Button Manual Creation Example void CChildView::OnRadioButton1Clicked () { m_wndRadioButton1.SetCheck (BST_CHECKED); m_wndRadioButton2.SetCheck (BST_UNCHECKED); m_wndRadioButton3.SetCheck (BST_UNCHECKED); }

32 Auto Radio Button WS_GROUP connects Radio buttons m_RadioButton1.Create (_T ("COM1"), WS_CHILD ¦ WS_VISIBLE ¦ WS_GROUP ¦ BS_AUTORADIOBUTTON, rect1, this, IDC_COM1); m_RadioButton2.Create (_T ("COM2"), WS_CHILD ¦ WS_VISIBLE ¦ BS_AUTORADIOBUTTON, rect2, this, IDC_COM2); m_RadioButton3.Create (_T ("COM3"), WS_CHILD ¦ WS_VISIBLE ¦ BS_AUTORADIOBUTTON, rect3, this, IDC_COM3); m_RadioButton4.Create (_T (“Student1"), WS_CHILD ¦ WS_VISIBLE ¦ WS_GROUP ¦ BS_AUTORADIOBUTTON, rect4, this, IDC_COM1); m_RadioButton5.Create (_T (" Student2"), WS_CHILD ¦ WS_VISIBLE ¦ BS_AUTORADIOBUTTON, rect5, this, IDC_COM2); m_RadioButton1.Create (_T ("COM1"), WS_CHILD ¦ WS_VISIBLE ¦ WS_GROUP ¦ BS_AUTORADIOBUTTON, rect1, this, IDC_COM1); m_RadioButton2.Create (_T ("COM2"), WS_CHILD ¦ WS_VISIBLE ¦ BS_AUTORADIOBUTTON, rect2, this, IDC_COM2); m_RadioButton3.Create (_T ("COM3"), WS_CHILD ¦ WS_VISIBLE ¦ BS_AUTORADIOBUTTON, rect3, this, IDC_COM3); m_RadioButton4.Create (_T (“Student1"), WS_CHILD ¦ WS_VISIBLE ¦ WS_GROUP ¦ BS_AUTORADIOBUTTON, rect4, this, IDC_COM1); m_RadioButton5.Create (_T (" Student2"), WS_CHILD ¦ WS_VISIBLE ¦ BS_AUTORADIOBUTTON, rect5, this, IDC_COM2);

33 Coding Practice Creating the following program using CButtons


Download ppt "Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class."

Similar presentations


Ads by Google