Chapter 4 Menu and Chapter 6 File I/O

Slides:



Advertisements
Similar presentations
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
Advertisements

1 of 6 This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS DOCUMENT. © 2007 Microsoft Corporation.
Introduction to Microsoft Windows MFC Programming: the Application/Window Approach Lecture 4.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
:56 EasyPreparation without Barcode Reader.
Microsoft Visual Basic 2012 CHAPTER TWO Program and Graphical User Interface Design.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Automating Tasks with Visual Basic. Introduction  When can’t find a readymade macro action that does the job you want, you can use Visual Basic code.
Bertrand Bellenot ROOT Users Workshop Mar ROOT GUI Builder Status & Plans ROOT & External GUI World MFC, FOX, Qt, PVSS… Snapshot of the Future.
Adding User Interactivity – Lesson 51 Adding User Interactivity Lesson 5.
Visual C++ Lecture 11 Friday, 29 Aug Windows Graphic User Interface l Event driven programming environment l Windows graphic libraries (X11 on Unix,
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
MFC Windows Programming: Document/View Approach More detailed notes at: 360/notes-html/class15.htm.
Overview of Previous Lesson(s) Over View  Microsoft Foundation Classes (MFC)  A set of predefined classes upon which Windows programming with Visual.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Classic Controls Trần Anh Tuấn A. Week 1 How to create a MFC project in VS 6.0 How to create a MFC project in VS 6.0 Introduction to Classic Controls.
Microsoft Foundation Classes. What is MFC? Set of C++ classes written by MS Simplifies writing complex programs Covers many areas: – GUI – I/O – O/S interfaces.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 3 The mouse and the Keyboard. Getting input from the Mouse.
Chapter 7 Controls.
Microsoft Outlook 2010 Chapter 3 Managing Contacts and Personal Contact Information with Outlook.
Lecture 7 Menu, controls, scroll bars. Menu Menu item sends WM_COMMAND message to the application window Menu is attached to window when the window is.
Object Oriented Programming Dr. Ennis-Cole CECS 5100.
Bitmap (Chapter 15).
Chapter 6: FILE I/O and Serialize CFile class. 2 Introduction (1/2) How to read/write a file –Usual way of File I/O Use CFile class Use member functions:
Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
Chapter2: Drawing a Window
Tutorial 3 Creating Animations. XP Objectives Learn the different elements of animation Create frames and layers Organize frames and layers using the.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Chapter 7 Controls. 2 Introduction (1/4) Control –Special kind of window –Designed to convey information to the user or to acquire input –Reduce the tedium.
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class.
Customizing Menus and Toolbars CHAPTER 12 Customizing Menus and Toolbars.
Chapter 7 Controls. List box control 3 List Box Control(1/8) Listbox control: –Display lists of text strings called items –Optionally sort the items.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Microsoft Visual Basic 2010 CHAPTER FOUR Variables and Arithmetic Operations.
CS212: Object Oriented Analysis and Design
Message Handling in MFC
Chapter 2: The Visual Studio .NET Development Environment
Working in the Forms Developer Environment
Steps to Build Frame Window Recipe Application
Chapter 17 Creating the Document and Improving the View
Building a User Interface with Forms
Variables and Arithmetic Operations
Introduction to Classes
Chapter 5 Classes.
Cataloging introductory flow
Chapter 4 MS ACCESS DATABASE.
Microsoft Excel 2003 Illustrated Complete
Program and Graphical User Interface Design
Variables and Arithmetic Operations
Topics Introduction to File Input and Output
Chapter 5 The MFC Collection Classes
Access Lesson 2 Creating a Database
DREAMWEAVER MX 2004 Chapter 3 Working with Tables
Chapter 5 The MFC Collection Classes
How to organize and document your classes
19.
CodePainter Revolution Trainer Course
Topics Introduction to File Input and Output
Steps to Build Frame Window Recipe Application
Chapter 17 Creating the Document and Improving the View
Presentation transcript:

Chapter 4 Menu and Chapter 6 File I/O

Back to MENU – Chapter 4

Chapter 4-3: Menu Magic 2 Ways for creating a menu: Use the Resource view Use CMenu class and create it manually

Create a menu using CMenu class Design a popup menu Add(attach) the popup menu to the main menu 2. Add it to the main menu 1. Design a popup menu

Procedure Design a popup menu Attach it to the main menu Create a new PopupMenu: CreatePopupMenu() Adding menu items to the menu: AppendMenu() Attach it to the main menu Get the main menu: GetMenu() Attach a menu to the existing menu: AppendMenu()

Coding practice In the CMainFrame::OnCreate function int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { // … ... CMenu Popup; Popup.CreatePopupMenu(); Popup.AppendMenu(MF_STRING, 201, “Red(&R)"); Popup.AppendMenu(MF_STRING, 202, “Green(&G)"); Popup.AppendMenu(MF_STRING, 203, “Blue(&B)"); CMenu * pMenuMain = GetMenu(); pMenuMain->AppendMenu(MF_POPUP, (UINT_PTR) Popup.Detatch(), “Color(&C)”); }

Modifying menus by using CMenu Function Description AppendMenu Adds an item to the end of a menu InsertMenu Inserts an item into a menu at a specified location ModifyMenu Changes the command ID, text, or other characteristics of a menu item DeleteMenu Deletes a menu item and the submenu associated with it, if any RemoveMenu Deletes a menu item Example of deleting menu CMenu* pMenu = GetMenu ()->GetSubMenu (1); pMenu->DeleteMenu (2, MF_BYPOSITION); // Delete by position pMenu->DeleteMenu (ID_SHAPE_CIRCLE, MF_BYCOMMAND); // Delete by ID

Summary CMenu class: store the menu properties AppendMenu() member function Flag: MF_STRING (when adding a normal menu item) MF_POPUP (when attaching a popup menu) MF_SEPARATOR ID: In case of MF_STRING : Command ID In case of MF_POPUP : Pointer to the popup menu (use CMenu::Detatch() function) bool CMenu::AppendMenu( Flag, ID, Caption )

Context menu Context menu

Context menu message handler 1. When click the mouse right button or key 2. Windows sends WM_CONTEXTMENU message 3. Add the message handler

Prototype of the handler WM_CONTEXTMENU message handler pWnd – window where the mouse cursor is located pos – the position of the mouse cursor afx_msg void OnContextMenu (CWnd* pWnd, CPoint pos) ;

Coding Practice Add the WM_CONTEXTMENU handler AfxMessageBox(_T(“Context Menu”));

Show the menu in the handler Use CMenu::TrackPopupMenu() function nFlags TPM_LEFTALIGN, TPM_CENTERALIGN, TPM_RIGHTALIGN TPM_LEFTBUTTON, TPM_RIGHTBUTTON BOOL TrackPopupMenu (UINT nFlags, int x, int y, CWnd* pWnd, LPCRECT lpRect = 0) ;

Show the menu in the handler Use CMenu::TrackPopupMenu() function x, y Position where the menu will be located(스크린 좌표) pWnd Window which will get the WM_COMMAND message Usually use AfxGetMainWnd() function to get the CMainFrame lpRect The rectangle region of the menu. Give 0 usually. BOOL TrackPopupMenu (UINT nFlags, int x, int y, CWnd* pWnd, LPCRECT lpRect = 0) ;

An example to show the context menu Create a menu and show it: void CChildView::OnContextMenu(CWnd* pWnd, CPoint point) { CMenu menuPopup; menuPopup.CreatePopupMenu(); menuPopup.AppendMenu(MF_STRING, 201, "Red (&R)"); menuPopup.AppendMenu(MF_STRING, 202, "Green (&G)"); menuPopup.AppendMenu(MF_STRING, 203, "Blue (&B)"); menuPopup.TrackPopupMenu( TPM_LEFTALIGN|TPM_LEFTBUTTON, point.x, point.y, AfxGetMainWnd()); }

More example Using existing menu as a context menu void CChildView::OnContextMenu(CWnd* pWnd, CPoint point) { CMenu menu; menu.LoadMenu(IDR_MAINFRAME); CMenu* pMenu = menu.GetSubMenu(4); pMenu->TrackPopupMenu( TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, AfxGetMainWnd()); }

Chapter 6: FILE I/O and Serialize CFile class

Introduction (1/2) How to read/write a file Usual way of File I/O Use CFile class Use member functions: CFile::Read() and CFile::Write() Universal use. Low level file I/O Serialize Use CArchive class Use operators: << and >> Easy to use Limited functionality

Introduction (2/2) MFC Class Hierarchy Basic File I/O Derived Classes for specific purposes

CFile class Basic functionality Open or create a file(Open). Read data at the file pointer(Read). Write data at the file pointer(Write). Change the file pointer (Seek). Close the file(Close).

CFile: Two ways to Open a file Use CFile::Open member function Use a constructor CFile file; file.Open( filename, mode, error ); CFile file ( filename, mode );

CFile class (1/6) CFile file; file.Open( filename, mode, error ); Open and Create: Use CFile::Open(..) CFile file; file.Open( filename, mode, error ); CFile file; if( file.Open("mytest.txt", CFile::modeRead) == false) AfxMessageBox(_T(“Error”));

CFile class (1/6) CFile file; file.Open( filename, mode, error ); Open and Create: Use CFile::Open(..) Get errors and related information CFile file; file.Open( filename, mode, error ); CFile file; CFileException e; if(!file.Open("mytest.txt", CFile::modeReadWrite, &e)) e.ReportError();

CFile class (1/6) CFile file ( filename, mode ); Open and Create : Use the constructor CFile file ( filename, mode ); try { CFile file("mytest.txt", CFile::modeReadWrite); } catch (CFileException* e) e->ReportError(); e->Delete();

CFile class (2/6) Access modes of CFile flags meaning CFile::modeCreate Directs the constructor to create a new file. If the file exists already, it is truncated to 0 length CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. CFile::modeRead Requests read access only CFile::modeReadWrite Requests read and write access CFile::modeWrite Requests write access only CFile::shareDenyNone Opens the file nonexclusively CFile::shareDenyRead Denies read access to other parties. CFile::shareDenyWrite Denies write access to other parties. CFile::shareDenyExclusive Denies both read and write access to other parties (default).

CFile class(3/6) Close a file: Destructor closes it automatically void CExFileView::OnLButtonDblClk(UINT nFlags, CPoint point) { CFile file; CFileException e; if(!file.Open("mytest.txt", CFile::modeReadWrite|CFile::modeCreate, &e)) e.ReportError(); return; } // skip ... } // -> CFile::~CFile() is called and close the file automatically.

CFile class (4/6) Close a file: CFile::Close member function When opening a multiple files void CExFileView::OnLButtonDblClk(UINT nFlags, CPoint point) { CFile file; CFileException e; if(!file.Open("mytest.txt“, CFile::modeReadWrite|CFile::modeCreate| CFile::modeNoTruncate, &e)) e.ReportError(); return; } file.Close();

CFile class (5/6) Read and Write Change the file pointer UINT CFile::Read (void* lpBuf, UINT nCount) ; void CFile::Write (const void* lpBuf, UINT nCount) ; ULONGLONG CFile::Seek (LONGLONG lOff, UINT nFrom) ; nFrom meaning CFile::begin Move the file pointer by lOff bytes from the beginning CFile::current Move the file pointer by lOff bytes from the current position CFile::end Move the file pointer by lOff bytes from the end

CFile class: Write Data void CFile::Write (const void* lpBuf, UINT nCount) ; CFile file(_T(“test.txt“), CFile::modeCreate|CFile::modeWrite); int a = 30; int b = 20; file.Write(&a, sizeof(a)); file.Write(&b, sizeof(b));

CFile class: Read Data UINT CFile::Read (void* lpBuf, UINT nCount) ; CFile file; CFileException e; if(!file.Open(_T("test.txt“), CFile::modeRead, &e)) { e.ReportError(); return; }; int a,b; file.Read(&a, sizeof(a)); file.Read(&b, sizeof(b));

CFile class: Read Data UINT CFile::Read (void* lpBuf, UINT nCount) ; CFile file; CFileException e; if(!file.Open(_T("test.txt“), CFile::modeRead, &e)){ e.ReportError(); return; }; int a,b; file.Read(&a, sizeof(a)); file.Read(&b, sizeof(b)); CString str; str.Format(_T("a=%d b=%d"), a, b); AfxMessageBox(str);

Coding Practice Create variables “a” and “b” When clicking mouse left button, create “test.txt” file, and store the values of “a” and “b” variables When clicking mouse right button, read “test.txt” file, and read the values of “a” and “b” variables Show the data by using AfxMessageBox function

CFile class (6/6) Miscellaneous functions CFile::GetLength(), CFile::SetLength() Get or change the file size. CFile::GetPosition() Get the file position. CFile::LockRange(), CFile::UnlockRange() Lock or Unlock the file. If lock a file, other process can not access the file. CFile::GetFilePath(), CFile::GetFileName() Get full path or file name as a CString