+ All Categories
Home > Documents > Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows...

Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows...

Date post: 19-Jan-2018
Category:
Upload: geraldine-gray
View: 217 times
Download: 0 times
Share this document with a friend
Description:
Common File Dialog Box What is it? –This is the Windows standard way to select a file name –There are two flavors: open existing or create new one Where: Dialogs tab There are two of importance today: –wxOpenFileDialog and wxSaveFileDialog Neither will open a file –But they will obtain a file name Copyright © Curt Hill
21
Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs
Transcript
Page 1: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Copyright © 2004-2013 - Curt Hill

Common Dialogs

Easily Obtaining File Names in DevC++ Windows Programs

Page 2: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Programs that Use Files• Some programs use exactly the

same file each time– Usually an initialization file in the

same place every time• Most programs can use any file of

the proper format• Typing the file name into a text

box is awkward and error prone• Let Windows show us where it is

with a common dialog boxCopyright © 2004-2013 - Curt Hill

Page 3: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Common File Dialog Box• What is it?

– This is the Windows standard way to select a file name

– There are two flavors: open existing or create new one

• Where: Dialogs tab• There are two of importance today:

– wxOpenFileDialog and wxSaveFileDialog

• Neither will open a file– But they will obtain a file name

Copyright © 2004-2013 - Curt Hill

Page 4: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

What you can do• A common dialog box allows the

user to avoid typing and• Change directory or disk• Choose a file• Change the filter• Accept the chosen file or cancel

Copyright © 2004-2013 - Curt Hill

Page 5: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Example from PowerPoint

Copyright © 2004-2013 - Curt Hill

Page 6: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

General• Unlike most components they are

non-visual• They can be seen on the form

during design time• Invisible at execution time• They are not seen until executed

Copyright © 2004-2013 - Curt Hill

Page 7: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

DevC++ Design Time

Copyright © 2004-2013 - Curt Hill

Page 8: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

After Drop

Copyright © 2004-2013 - Curt Hill

Page 9: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Using• Once put on form we still have to

use it• General process is:• Display box• Determine if open or cancel was

done• If Open get the file name • Convert this file to a C style string

pass it to the file

Copyright © 2004-2013 - Curt Hill

Page 10: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Modal Dialog Boxes• In Windows there are two styles of

dialog box:– Modal– Non-modal

• A non-modal may be displayed, but it does not interfere with the parent window

• A Modal dialog box prevents any input to the parent– It must be dismissed before the parent

is usableCopyright © 2004-2013 - Curt Hill

Page 11: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

ShowModal Method• The ShowModal method takes no

parameters and shows the dialog• ShowModal returns an integer that

tells what happened• If this is equal to wxID_OK then the

user approved the file name– Otherwise they cancelled– All file processing should be done only

if a wxID_OK was returned

Copyright © 2004-2013 - Curt Hill

Page 12: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Design Properties• Message – The contents of the title

bar when Common dialog is displayed

• Name – The name of the component– This will always be referenced from

code• Most other things need to be

referenced using methods

Copyright © 2004-2013 - Curt Hill

Page 13: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

GetPath Method• This is used to obtain the filename

– It also contains the entire directory path– GetFilename only gives the file name

without the path• It returns a wxString• ifstreams and ofstreams need a C

style string• This needs to be converted in order

to pass to a constructor or open method

• How?Copyright © 2004-2013 - Curt Hill

Page 14: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

String Conversion• This is relatively easy• Use the wxString method: c_str()

– ifstream inf( OpenDialog1-> GetPath ().c_str());

• You may do in two steps:– wxString name = OpenDialog->GetPath();

– ifstream inf(name.c_str());

Copyright © 2004-2013 - Curt Hill

Page 15: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Sample Code• Suppose that OpenDial is the object• The following code will open a file: int ret = OpenDialog->ShowModal();if(ret == wxID_OK){

• ifstream inf( OpenDial->GetPath() .c_str()); while(inf) { … } } // end of if

Copyright © 2004-2013 - Curt Hill

Page 16: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Executed

Copyright © 2004-2013 - Curt Hill

Page 17: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Filters• By default a common dialog box

will show every file in the current directory– The directory may be changed

• Often we are not interested in every kind of file– A filter allows us to only observe the

files of interest• DevC++ does not allow the filter

to be set at design time– Done with SetWildcard method

Copyright © 2004-2013 - Curt Hill

Page 18: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Filter Pairs• Each pair consists of two pieces

– A name to be displayed– An expression that describes what to

show• The two are separated by a

vertical bar• The expression may include ? or *

– ? any one character– * any number of any characters

Copyright © 2004-2013 - Curt Hill

Page 19: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Format• Thus we set:• OpenDialog1->SetWildcard( "Data|*.dat|All|*.* ");

• The odd entries are display• The even entries are the actual filters• See next screen for image

Copyright © 2004-2013 - Curt Hill

Page 20: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Show Filters

Copyright © 2004-2013 - Curt Hill

Page 21: Copyright © 2004-2013 - Curt Hill Common Dialogs Easily Obtaining File Names in DevC++ Windows Programs.

Last Thoughts• There are many other options

– Most of these we do not need• The FileName is persistent• It will show as the last one used in

subsequent executions

Copyright © 2004-2013 - Curt Hill


Recommended