You are on page 1of 54

Building Applications Using VC++

Overview of OOP

OOP is about trying to represent the ‘Objects’ that we find in the real
world ( or at least that part of which programs address ) in software.

WHAT IS OBJECT ?

Objects are the elements through which we perceive the world around us,
which have recognizable identities and particular behaviors.

These ‘Objects’ may be of various types, ranging from the physical to the
more conceptual.

In OOP there is mainly two kinds of Objects :

Those that directly represent the ideas used to describe the application

Those that are used as programming tools to implement code

Object = (private) data + (public) processes

Objects have a ‘private’ part of hidden internal detail, and a

‘public’ interface which clearly defines the set of possible

behaviors of the object.

Areas of Object technology

The general field of object technology can be seen as falling into four area
of application :

OOP

OOD

OOA

OODBs
Key Features of OOP

Abstraction / encapsulation

Inheritance

Polymorphism

Abstraction / encapsulation

It the representation of all the essential features of an object, which


means its possible states & behaviors.

Encapsulation is the practice of including in an object everything it needs,


(both data & processes) hidden from other objects in the system. The
internal state of an object is not directly accessible from outside, and
cannot be altered by external changes to the application.

Changes to the internal implementation details can be made without


affecting the external interface.

Inheritance

Inheritance means that one class inherits the characteristics of another class
as part of its definition. It appropriate when one class is ‘a kind of’ other
class (e.g. A is a kind of B – ‘text window’ is a kind of ‘window’). These types
of hierarchies are fundamental to OOP. Some classes may inherit from more
than one other class, (multiple inheritance).

Inheritance has two complementary roles :

1. To allow class to be extended so that its existing functionality can be


built on for new applications.

2. To allow similar objects to share their common properties & allowed


behaviors.

3. Polymorphism

4. It means ‘having many forms’. The general description of


polymorphism is that it allows different objects to respond to the same
message in different ways, the response specific to the type of
object.This is achieved by various types of ‘overloading’ - allowing a
symbol ( operator like +) to have more than one meaning depending
on the context in which it is used.
5. Polymorphism is particularly important when object oriented programs
are dynamically creating & destroying objects in a classification
hierarchy at run time.

MFC Introduction

 Microsoft created a group called the AFX group (stands for


(A)pplication (F)ramework(X)).

 They came up with a class library that presented a very high


abstraction from the Windows API.

 The AFX group was actually responsible for two things: the
MFC library and the IDE's support for MFC (namely, the
resource editor and the wizards).

MFC Classes

CObject CException CBrush CListBox

CWinApp CMemoryException CFont CComboBox

CMenu CNotSupportedExce CBitmap CScrollBar


ption

CArchive CArchiveException CPalette

CDumpCont CFileException CRgn


ext

CRuntimeCla CResourceException CWnd


ss

CMemorySta CFile CFrameWnd


te

CFileStatus CDC CMDIChildWnd

CString CClientDC CMDIFrameWnd


CTime CWindowDC CDialog

CTimeSpan CPaintDC CModalDialog

CPoint CMetaFileDC CStatic

CRect CGdiObject CButton

CSize CPen CEdit

CObject CException CBrush CListBox

CWinApp CMemoryException CFont CComboBo


x

CMenu CNotSupportedExce CBitmap CScrollBar


ption

CArchive CArchiveException CPalette

CDumpCont CFileException CRgn


ext

CRuntimeCla CResourceException CWnd


ss

CMemorySta CFile CFrameWnd


te

CFileStatus CDC CMDIChildWnd

CString CClientDC CMDIFrameWnd

CTime CWindowDC CDialog

CTimeSpan CPaintDC CModalDialog

CPoint CMetaFileDC CStatic


CRect CGdiObject CButton

CSize CPen CEdit

The Visual C++ Development Environment

 The Workspace

 When you start Visual C++ for the first time, an area on the left side of
Developer Studio looks like it is taking up a lot of real estate and
providing little to show for it. This area is known as the workspace, and
it is your key to navigating the various pieces and parts of your
development projects. The workspace allows you to view the parts of
your application in three different ways:

 Class View allows you to navigate and manipulate your source code
on a C++ class level.

 Resource View allows you to find and edit each of the various
resources in your application, including dialog window designs, icons,
and menus.

 File View allows you to view and navigate all the files that make up
your application.
The Output Pane

 The Output pane might not be visible when you start Visual C++ for
the first time. After you compile your first application, it appears at the
bottom of the Developer Studio environment and remains open until
you choose to close it.

 The Output pane is where Developer Studio provides any information


that it needs to give you; where you see all the compiler progress
statements, warnings, and error messages; and where the Visual C++
debugger displays all the variables with their current values as you
step through your code.

 After you close the Output pane, it reopens itself when Visual C++ has
any message that it needs to display for you.

Starting Your First Project

 Creating the Project Workspace

 Every application development project needs its own project


workspace in Visual C++. The workspace includes the
directories where the application source code is kept, as well
as the directories where the various build configuration files
are located. You can create a new project workspace by
following these steps:

 1. Select File | New. This opens the New Wizard

 2. On the Projects tab, select MFC AppWizard (exe).

 3. Type a name for your project, such as Hello, in the


Project Name field.

 4. Click OK.

This causes the New Wizard to do two things: create a project


directory (specified in the Location field) and then start the
AppWizard.
 Using the Application Wizard to Create the Application Shell

 The AppWizard asks you a series of questions about what type


of application you are building and what features and
functionality you need. It uses this information to create a
shell of an application that you can immediately compile and
run. This shell provides you with the basic infrastructure that
you need to build your application around. You will see how
this works as you follow these steps:

 In Step 1 of the AppWizard, specify that you want to


create a Dialog-based application. Click Next at the
bottom of the wizard.
 In Step 2 of the AppWizard, the wizard asks you about a
number of features that you can include in your application.

 You can uncheck the option for including support for ActiveX
controls if you will not be using any ActiveX controls in your
application.

 Because you won't be using any ActiveX controls in

application, go ahead and uncheck this box.


 The final step of the AppWizard shows you the C++
classes that the AppWizard will create for your
application.

 Click Finish to let AppWizard generate your application


shell.

 Before AppWizard creates your application shell, it


presents you with a list of what it is going to put into the
application shell, based on the options you selected when
going through the AppWizard.

Click OK and AppWizard generates your application.


 After the AppWizard generates your application shell, you are returned
to the Developer Studio environment.

 You will notice that the workspace pane now presents you with a tree
view of the classes in your application shell.

 You might also be presented with the main dialog window in the editor
area of the Developer Studio area.
To Build and Execute the project

 Select Build | Build Hello.exe to compile your application.

 As the Visual C++ compiler builds your application, you see


progress and other compiler messages scroll by in the Output
pane. After your application is built, the Output pane should
display a message telling you that there were no errors or
warnings.

 The Output pane displays ( if )any compiler errors.

 Select Build | Execute Hello.exe to run your application.

 Your application presents a dialog with a TODO message and OK


and Cancel buttons
 You can click either button to close the application.

 Adding Code to Your Application

 You can attach code to your dialog through the Visual C++ Class
Wizard.

 You can use the Class Wizard to build the table of Windows messages
that the application might receive, including the functions they should
be passed to for processing, that the MFC macros use for attaching
functionality to window controls.

 You can attach the functionality for this first application by following
these steps:

 To attach some functionality to the Hello button, right-click over


the button and select Class Wizard from the pop-up menu.
 With IDHELLO selected in the Object ID list, select BN_CLICKED in
the list of messages and click Add Function. This opens the Add
Member Function dialog

 This dialog contains a suggestion for the function name. Click OK


to create the function and add it to the message map.

 Finishing Touches

 Now that your application is functionally complete, you can still add a
few details to finish off the project. Those finishing touches include

 Creating the dialog box icon

Adding maximize and minimize buttons

Creating the Dialog Box Icon


 In the tree view of your application resources in the workspace pane,
expand the icon branch and select the IDR_MAINFRAME icon.

 This brings the application icon into the editor area of the Developer
Studio.

 Using the painting tools provided, repaint the icon to display an image
that you want to use to represent your application,

 Adding Maximize and Minimize Buttons

 In the dialog editor, where you design your application window, you
can add the minimize and maximize buttons to the title bar of your
application window by following these steps:

 Select the dialog window itself as if you were going to resize the
window.
 Using the pop-up menu (from right-clicking the mouse), select
the dialog properties.

 Select the Styles tab

 After you turn on the minimize and maximize boxes, you can
compile and run your application. The minimize and maximize
buttons appear on the title bar
File Types in Project

 dsw :Workspace file

 .dsp :Project file

 .clw :ClassWizard file

 .ncb :No Compile Browser file(Contains details used by


Classview & Classwizard Toolbar)

 .opt :Options file(Stores cusomized workspace display)

 .cpp :C++ source file (Implementation Code)

 .h :C++ header file(definition & class declaration code)

 .rc :Resource Script file(details of resources)

 .rc2 :Resource Script file(used to include resourcesin to other


projects)
Understanding the Components of a Dialog-Based Application

 Dialog applications are much simpler than SDI and MDI


applications. Create one called FirstDialog, with an About box,
no Help, 3D controls, no automation, ActiveX control support,
no sockets, source file comments, and MFC as a shared DLL. In
other words, accept all the default options.

 Three classes have been created for you for the application
called FirstDialog:

 CAboutDlg, a dialog class for the About dialog box

 CFirstDialogApp, a CWinApp class for the entire application

 CFirstDialogDlg, a dialog class for the entire application.

All the familiar Windows controls are available for dialog boxes

 Static text. Not really a control, this is used to label other


controls such as edit boxes.

 Edit box. Single line or multi line, this is a place for users to
type strings or numbers as input to the program. Read-only
edit boxes are used to display text.

 Button. Every dialog box starts with OK and Cancel buttons,


but you can add as many of your own as you want.

 Check box. You use this control to set options on or off; each
option can be selected or deselected independently.

 Radio button. You use this to select only one of a number of


related options. Selecting one button deselects the rest.

 List box. You use this box type to select one item from a list
hard coded into the dialog box or filled in by the program as
the dialog box is created. The user cannot type in the selection
area.
 Combo box. A combination of an edit box and a list box, this
control enables users to select from a list or type their
response, if the one they want isn't on the list.

The data types that go with each control type

 Edit box. Usually a string but also can be other data types,
including int, float, and long

 Check box. int

 Radio button. int

 List box. String

 Combo box. String

 Scrollbar. int

Access Values from a Control

 To access values from control in VC++ you have to create


“Member Variable” ( It gives bound values connectivity to
control )

OR

 You have to create Constructer derived from Control


Classes( to access values from control dynamically &
unbound )

 To create Member Variable (bound) use following the steps :

 Select a control & Right Click and select Class Wizard or press
CTRL + W.

 Then Select ‘Member Variables’ Tag


Then choose its type or category from given options
Data Validation
You can give the limitations to the variables

To store the Values in variable

 Use UpdateData( ) function to clear or store the values in


member variable.

 To clear or restore the previous value use UpdateData( 0 )

 To store the value for display in control use

UpdateData( 1 ).

 Creating Constructer Variable :


 You have to create reference from base class of the related
control

CONTROL BASE CLASS

 Buttons CButton

 List boxes CListBox

 Edit controls CEdit

 Combo boxes CComboBox

 Scroll bars CScrollBar

 Static controls CStatic

Way of Creation

 CEdit* ed = (CEdit*)GetDlgItem(IDC_EDIT1);

 Where GetDlgItem() contains the name of Control;

 CEdit is the base class for EDIT BOX control & ‘ ed ‘ is the
constructer for the control

 ‘ed’ will represent all the properties supported by the EDIT


BOX

To add the local variable/functions

 To create local variable for your project choose following steps

 Right click on the dlg class from class view

 Select Add Member Variable or Add Member Function option


(all the type of variable or function is same as C++)
File Types in Project

 dsw : Workspace file

 .dsp :Project file


 .clw :ClassWizard file

 .ncb :No Compile Browser file(Contains details used by


Classview & Classwizard Toolbar)

 .opt :Options file(Stores cusomized workspace display)

 .cpp :C++ source file (Implementation Code)

 .h :C++ header file(definition & class declaration code)

A Single Document Interface Application

 An SDI application has menus that the user uses to open one
document at a time and work with that document. This section
presents the code that is generated when you create an SDI
application with no database or compound document support,
with a toolbar, a status bar, Help, 3D controls, source file
comments, and with the MFC library as a shared DLL in other
words when you accept all the AppWizard defaults after Step
1.

 Five classes have been created for you. For the application
FirstSDI, they are as follows:

 CAboutDlg, a dialog class for the About dialog box

 CFirstSDIApp, a CWinApp class for the entire application

 CFirstSDIDoc, a document class

 CFirstSDIView, a view class

 CMainFrame, a frame class

 In Last Step (6) you can select the base class for your SDI
application.
SDI Application Run time view
Understanding a Multiple Document Interface Application

 A multiple document interface application also has menus, and


it enables the user to have more than one document open at
once. This section presents the code that is generated when
you choose an MDI application with no database or compound
document support, but instead with a toolbar, a status bar,
Help, 3D controls, source file comments, and the MFC library
as a shared DLL. As with the SDI application, these are the
defaults after Step 1. The focus here is on what differs from
the SDI application in the previous section.Six classes have
been created for you :

 CAboutDlg, a dialog class for the About dialog box

 CFirstMDIApp, a CWinApp class for the entire application

 CFirstMDIDoc, a document class

 CFirstMDIView, a view class


 CMainFrame, a frame class

 CChildFrame, a child frame class

MDI Application Design view

MDI Application Run time view


Database Connectivity

 With Simple Dialog Box

 Create ODBC DSN from Control Panel for your Table

 First include Header file named : “afxdb.h” in project.

 Then create Member Variable with type of “CDatabase” (db)for


main database (eg. *.mdb);

After creating database, create Member Variable with type of


“CRecordset”(rs)

To Connect Database with DSN created by ODBC

 db.OpenEx("DSN= DSN name ;UID= User name ");

Connect the Recordset (rs) with Database


 rs.m_pDatabase=&db;

To Connect the Particular Table

 rs.Open(CRecordset::snapshot,"select * from tablename ");


^ Recordset type

 To get the field value of the field

rs.GetFieldValue(“fieldname",variable);

 To get First Record : rs.MoveFirst();

 To get Next Record : rs.MoveNext();

 To get Prev Record : rs.MovePrev();

 To get Last Record : rs.MoveLast();

 To Add the Record :

 CString query;

 query = “insert into tablename ‘ “ +


fieldValue+ “ ’ ”

 db.ExecuteSQL(query);

 rs.Requery();

 To delete the record

 CString query;

 query="delete from table name where


field name=‘ "+field value+"'";

 db.ExecuteSQL(query);

 rs.Requery();

 To Edit the record :


 CString query;

 query="update table name set field


name='"+field value+"‘;

 db.ExecuteSQL(query);

 rs.Requery();

 Other functions :

IsEOF, IsBOF etc.

Database Connectivity

o With SDI Application

While Creating Application :

1. In Step 2 Select “ Database View without file support ” option

2. Click on “ Data Source “ Button & Select ODBC

1. Give particular DSN

2. Then Select Particular Table from dialog box

In Step 4 Deselect “ Printing & print preview “ option

 VC++ automatically creates the public variable with database


utility named : m_pSet

 Take Edit Boxes as per your database’s field

 Create Member Variables for each edit box.

 In the “ Member Variable Name “ Combo box There is


the list of Field variables per your

database fields

 Select appropriate Field variable for each edit box


e.g. : m_pSet->m_Fieldname for an edit box

variable

 It will display the record value in appropriate edit box. (run


time)

 You can move the record from toolbar or menu by selecting


appropriate option

 To Add Record :

 m_pSet->AddNew();

 UpdateData(1);

 m_pSet->Update();
 To Edit Record :

 m_pSet->Edit();

 UpdateData(1);

 m_pSet->Update();

 To Delete Record :

m_pSet->Delete();

UpdateData(1);

 Other functions :

IsEOF, IsBOF etc.

Database Connectivity

 With OLEDB

 In Step 2 Select Database view without file support

 Then Click on the “DataSource” Button

 Select OLEDB option & select ODBC provider & give DSN or
build connection string

 Then click OK & select the required table.

 It will automatically create the variable named m_pSet

 Use all the commands( of ODBC connectivity ) to utilize the


database.

With DAO

[Window Title]

Reserved Battery Level


[Main Instruction]

Plug in or find another power source

[Content]

Your battery power is low (7%). If you don't plug in your computer
soon, it will hibernate automatically.

 #include <stdio.h> #include <afx.h> #include <afxwin.h>

 #include <iostream.h>

 Create CFile object eg. :

 CFile f;

 Then use following syntax :

f.Open(“filename",CFile::modeCreate|CFile::modeWrite );

 To Write the Data into file use following :

CString sz =“It the CFile Text”;

f.Write(sz, strlen(sz));

f.Close();

 CArchive

 Archive is associated with a file and permits the buffered


writing and reading of data to and from storage

 You must create a CFile object before you can create a


CArchive object.

 The archive’s load/store status is compatible with the file’s


open mode. You are limited to one active archive per file.
 When you construct a CArchive object, you attach it to an
object of class CFile (or a derived class) that represents an
open file.

 CArchive also supports programming with the MFC Windows


Sockets classes CSocket and CSocketFile. The IsBufferEmpty
member function supports that usage

 Example :

 CFile myFile(“Note.txt", CFile::modeCreate |


CFile::modeWrite);

 CArchive ar(&myFile, CArchive::store);

 ar.WriteString( “This from Archive" );

 // Flush all of the data to the file :

 ar.Flush();

ar.Close();

 Exception Handling

 CException CException is the base class for all exceptions.

 Some derived classes :

 CMemoryException Out-of-memory exception

 CNotSupportedException Request for an unsupported


operation

 CArchiveException Archive-specific exception

 CFileException File-specific exception

 CDBException Database exception (based on ODBC)

 CDaoException Data access object exception (that is,


exception conditions arising for DAO classes)

 CInternetException Internet exception (for Internet classes).


 These exceptions are intended to be used with the

TRY, CATCH, AND_CATCH, and END_CATCH macros

 Simple Example

CFile tmpFile;

CFileException ex;

if (! tmpFile.Open("\\Tmp\\temp.txt", CFile::modeRead, &ex))

TCHAR tmp[255];

CString str;

ex.GetErrorMessage(tmp, 255);

str = “The data file could not be opened because : ";

strr += tmp;

MessageBox(str);

else

tmpFile.Close();

 TRY

 Use this macro to set up a TRY block.

 A TRY block identifies a block of code that might throw


exceptions.

 Those exceptions are handled in the following CATCH and


AND_CATCH blocks.

 CATCH

 Use this macro to define a block of code that catches the first
exception type thrown in the preceding TRY block.
 CATCH ( exception_class, exception_object_pointer_name )

Where

 exception_class : Specifies the exception type to test for.

 exception_object_pointer_name : Specifies a name for an


exception-object pointer that will be created by the macro.
You can use the pointer name to access the exception object
within the CATCH block.

 Example :

CFile* pFile = NULL;

TRY

pFile = new CFile("\\Too\\temp.txt", CFile::modeRead);

CATCH (CFileException, Ex)

Ex->ReportError(MB_OK,0);

END_CATCH

 AND_CATCH

 AND_CATCH( exception_class, exception_object_pointer_name )

Where :

 exception_class

 Specifies the exception type to test for.

 exception_object_pointer_name
 A name for an exception-object pointer that will be created by
the macro. You can use the pointer name to access the
exception object within the AND_CATCH block.

 END_CATCH

 Marks the end of the last CATCH or AND_CATCH block.

 spawnl(var,”exe file”,”arg1”,path,NULL);

This function is used to start new process or application

 _P_OVERLAY

Overlays calling process with new process,destroying the calling


process

 _P_WAIT

Suspends calling thread until execution of new process is


complete

 _P_NOWAIT

Continues to execute calling process concurrently with new


process

 _P_DETACH

Continues to execute the calling process; new process is run in


the background with no access to the console or keyboard.

For Example to start doc file in MS Word

_spawnl(_P_NOWAIT,”C:windows\\notepad.exe”,”*.doc”,
”C:\\temp.txt”,NULL)
CFileDialog

#include <afxdlgs.h>

The CFileDialog class encapsulates the Windows common file dialog box.
Common file dialog boxes provide an easy way to implement File Open and
File Save As dialog boxes (as well as other file-selection dialog boxes) in a
manner consistent with Windows standards.

CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL,


LPCTSTR lpszFileName = NULL, DWORD dwFlags =
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter =
NULL, CWnd* pParentWnd = NULL );

Where :

bOpenFileDialog

Set to TRUE to construct a File Open dialog box or FALSE to construct a File
Save As dialog box.

lpszDefExt

The default filename extension. If the user does not include an extension in
the Filename edit box, the extension specified by lpszDefExt is automatically
appended to the filename. If this parameter is NULL, no file extension is
appended.

lpszFileName

The initial filename that appears in the filename edit box. If NULL, no
filename initially appears.

dwFlags

A combination of one or more flags that allow you to customize the dialog
box.

lpszFilter

A series of string pairs that specify filters you can apply to the file. If you
specify file filters, only selected files will appear in the Files list box. See the
Remarks section for more information on how to work with file filters.

pParentWnd

A pointer to the file dialog-box object’s parent or owner window.

Example :

CFileDialog FDlg(1,"htm","hello.htm",1,"Html Files (*.htm)|*.htm|All Files |


*.*||");
FDlg.DoModal();

CFontDialog

The CFontDialog class allows you to incorporate a font-selection dialog box


into your application. A CFontDialog object is a dialog box with a list of
fonts that are currently installed in the system. The user can select a
particular font from the list, and this selection is then reported back to the
application.
Once a CFontDialog object has been constructed, you can use the m_cf
structure to initialize the values or states of controls in the dialog box. The
m_cf structure is of type CHOOSEFONT.

CFontDialog( LPLOGFONT lplfInitial = NULL, DWORD dwFlags =


CF_EFFECTS | CF_SCREENFONTS, CDC* pdcPrinter = NULL, CWnd*
pParentWnd = NULL );

Where :

lplfInitial

A pointer to a LOGFONT data structure that allows you to set some of the
font’s characteristics.

dwFlags

Specifies one or more choose-font flags. One or more preset values can be
combined using the bitwise OR operator.

pdcPrinter

A pointer to a printer-device context. If supplied, this parameter points to a


printer-device context for the printer on which the fonts are to be selected.

Example :

Show Font Dialog with default settings :

CFontDialog dlg;

dlg.DoModal();

Sub Functions :

DoModal Displays the dialog and allows the user to make a selection.

GetCurrentFont Retrieves the name of the currently selected font.

GetFaceName Returns the face name of the selected font.

GetStyleName Returns the style name of the selected font.

GetSize Returns the point size of the selected font.

GetColor Returns the color of the selected font.


GetWeight Returns the weight of the selected font.

IsStrikeOut Determines whether the font is displayed with strikeout.

IsUnderline Determines whether the font is underlined.

IsBold Determines whether the font is bold.

IsItalic Determines whether the font is italic.

CColorDialog

The CColorDialog class allows you to incorporate a color-selection dialog


box into your application. A CColorDialog object is a dialog box with a list of
colors that are defined for the display system. The user can select or create
a particular color from the list, which is then reported back to the application
when the dialog box exits.
Once the dialog box has been constructed, you can set or modify any values
in the m_cc structure to initialize the values of the dialog box’s controls. The
m_cc structure is of type CHOOSECOLOR.

CColorDialog( COLORREF clrInit = 0, DWORD dwFlags = 0, CWnd*


pParentWnd = NULL );

Where :

clrInit

The default color selection. If no value is specified, the default is RGB(0,0,0)


(black).

dwFlags

A set of flags that customize the function and appearance of the dialog box.
pParentWnd

A pointer to the dialog box’s parent or owner window.

Example :

CColorDialog Coldlg; Color dialog with all the default settings.

Coldlg.DoModal();

Sub Functions :

DoModal Displays a color dialog box and allows the user to make a
selection.

GetColor Returns a COLORREF structure containing the values of the


selected color.

GetSavedCustomColors Retrieves custom colors created by the user.

SetCurrentColor Forces the current color selection to the specified color.


CPageSetupDialog

The CPageSetupDialog class encapsulates the services provided by the


Windows common OLE Page Setup dialog box with additional support for
setting and modifying print margins. This class is designed to take the place
of the Print Setup dialog box.

To use a CPageSetupDialog object, first create the object using the


CPageSetupDialog constructor. Once the dialog box has been constructed,
you can set or modify any values in the m_psd data member to initialize the
values of the dialog box's controls. The m_psd structure is of type
PAGESETUPDLG.

Example :

CPageSetupDialog Psdlg;
Psdlg.DoModal();

Attributes & Sub Functions :

m_psd A structure used to customize a CPageSetupDialog


object.

DoModal Displays the dialog box and allows the user make a selection.

CreatePrinterDC Creates a device context for printing.

GetDeviceName Returns the device name of the printer.

GetDevMode Returns the current DEVMODE of the printer.

GetDriverName Returns the driver used by the printer.

GetMargins Returns the current margin settings of the printer.

GetPortName Returns the output port name.

GetPaperSize Returns the paper size of the printer.


CPrintDialog

The CPrintDialog class encapsulates the services provided by the Windows


common dialog box for printing. Common print dialog boxes provide an easy
way to implement Print and Print Setup dialog boxes in a manner consistent
with Windows standards.

To use a CPrintDialog object, first create the object using the CPrintDialog
constructor. Once the dialog box has been constructed, you can set or
modify any values in the m_pd structure to initialize the values of the dialog
box’s controls. The m_pd structure is of type PRINTDLG.
Example :

CPrintDialog PrtDlg(0); or PrtDlg(1);

PrtDlg.DoModal();

Sub Functions

CreatePrinterDC Creates a printer device context without displaying the


Print dialog box

DoModal Displays the dialog box and allows the user to make a selection.

GetCopies Retrieves the number of copies requested.

GetDefaultsRetrieves device defaults without displaying a dialog box.

GetDeviceName Retrieves the name of the currently selected printer


device.

GetDevMode Retrieves the DEVMODE structure.

GetDriverName Retrieves the name of the currently selected printer driver.

GetFromPage Retrieves the starting page of the print range.

GetToPage Retrieves the ending page of the print range.

GetPortName Retrieves the name of the currently selected printer port.

GetPrinterDC Retrieves a handle to the printer device context.

PrintAll Determines whether to print all pages of the document.

PrintCollate Determines whether collated copies are requested.

PrintRange Determines whether to print only a specified range of pages.

PrintSelection Determines whether to print only the currently selected


items.
CFileFind

The MFC class CFileFind performs local file searches and is the base class
for CGopherFileFind and CFtpFileFind, which perform Internet file
searches. CFileFind includes member functions that begin a search, locate
a file, and return the title, name, or path of the file. For Internet searches,
the member function GetFileURL returns the file’s URL.

Example :

CFileFind findexe;

BOOL stat = findexe.FindFile("*.exe");

while (stat)

stat = findexe.FindNextFile();
MessageBox(findexe.GetFileName());

_chmod, _wchmod ( include <io.h> , <wchar.h> )

Change the file-permission settings.

_chmod( const char *filename, int pmode );

Where :

filename Name of existing file

pmode Permission setting for file

_S_IWRITE Writing permitted

_S_IREAD Reading permitted

_S_IREAD | _S_IWRITE Reading and writing permitted

_access, _waccess ( include <io.h> , <wchar.h> )

Determine file-access permission.

int _access( const char *path, int mode );

Where :

path File or directory path

mode Permission setting

mode ValueChecks File For

00 Existence only

02 Write permission

04 Read permission

06 Read and write permission

To Get or Set the Current Directory


TCHAR cur[MAX_PATH]; used to get system buffer path

MAX_PATH is system variable to access maximum path

SetCurrentDirectory("C:\\windows");

GetCurrentDirectory(MAX_PATH,cur);

MessageBox(cur);

To get the SIZE of the file

CFile::GetStatus

CFileStatus st;

CFile::GetStatus("f:\\drk.zip",st);

CString str;

str.Format("%ld",st.m_size);

MessageBox(str);

You might also like