0% found this document useful (0 votes)
51 views4 pages

Intro To MFC What Is MFC?: Course Given at Stanford by Patrick Young

The document discusses Microsoft Foundation Classes (MFC), a C++ class library for developing Windows GUI programs. MFC uses an event-driven model where the user can provide input at any time by clicking buttons, closing windows, etc. and the program responds to these events. MFC provides common GUI components and handles the underlying event loop, allowing developers to focus on writing event handling code. It implements an application framework model where reusable framework code calls application-specific code.

Uploaded by

I src
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views4 pages

Intro To MFC What Is MFC?: Course Given at Stanford by Patrick Young

The document discusses Microsoft Foundation Classes (MFC), a C++ class library for developing Windows GUI programs. MFC uses an event-driven model where the user can provide input at any time by clicking buttons, closing windows, etc. and the program responds to these events. MFC provides common GUI components and handles the underlying event loop, allowing developers to focus on writing event handling code. It implements an application framework model where reusable framework code calls application-specific code.

Uploaded by

I src
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Intro to MFC What is MFC?

• Microsoft Foundation Classes (MFC) is a C++


• What is MFC?
class library for doing Windows programming
• Event-driven programming
• Other option is using Win32 API
• Well-mannered GUI programs
– API = application programmer interface
• Application frameworks
– also known as programming with SDK
• MVC • SDK = standard development kit
• Document-view architecture • Win32 API is a C-based library of functions /
types
Note: some of this presentation is adapted from lecture notes from an MFC • MFC is C++ based: uses classes, inheritance
course given at Stanford by Patrick Young.
• More on structure of MFC later ...

Intro to MFC [Bono] 1 Intro to MFC [Bono] 2

Console vs. event-driven programming Well-mannered GUI programs


• GUI programs have a fundamentally different • GUI model is: user should be able to give any input at any
time. E.g.,
structure than console-based programs
– click on a button
– GUI = graphical user interface – close a window
• console-based program: – drag to draw
ask user for some input; • So, don’t hog the processor.
do some processing; • Means your program can’t go off and do some 20 minute or
print some output;
ask user for some more input;
20 second operation.
etc. – for computation intensive operations, use time-slicing or threads

– application programmer controls when input and output • Means your program cannot block to wait for input (a la
can happen scanf or cin >> )
– only get input or send output via events
• GUI program model: the user is in control
Intro to MFC [Bono] 3 Intro to MFC [Bono] 4

Event-driven programming More on well-mannered GUI programs


• structure GUI programs to respond to user events
• Application is responsible for making sure display
• events are: mouse clicks, mouse moves, keystrokes, is up-to-date.
etc.
• For example:
– in MFC parlance, usually called messages – user moves another window in front of yours
• Main control structure is an event loop: – then moves it away again (yours is exposed)
while (1) { – unless you now draw something in your window,
wait for next event
formerly exposed part is blank.
dispatch event to correct GUI component
} • Event-driven:
– this code is always the same, so it’s handled by MFC – event: view became visible
• You just write the code to respond to the events. – application’s response: redraw stuff in the window
– functions to do this are called message handlers in MFC
Intro to MFC [Bono] 5 Intro to MFC [Bono] 6

1
GUI Libraries
Application Frameworks
• GUI programs involve a lot of code.
• But for many different applications much of the • Sometimes called software architectures
code is the same. • Reusable software for a particular domain of
• The common code is part of the library. E.g.: applications
– getting and dispatching events • Contrast with class library:
– telling you when user has resized windows, redisplayed
windows, etc. your code
Framework
– code for GUI components (e.g. look and feel of buttons,
menus, dialog boxes, etc.)
• But the library is upside-down: library code calls
your code. Library your code
– this is called an application framework
Intro to MFC [Bono] 7 Intro to MFC [Bono] 8

Application Frameworks (cont.) Application Frameworks: details

• In this case: • From Horstmann POD text, Ch 19


• general purpose set of classes with pure virtual
functions as hooks for more specific versions
MFC • plus, protocol for using the virtual functions:
Framework – overall control structure given by framework code
– application writer supplies exact functionality of the
methods
• organization of the objects given (data members of
application-specific framework classes or params to methods)
code

Intro to MFC [Bono] 9 Intro to MFC [Bono] 10

MFC vs. other libraries


Application Frameworks: examples
• All GUI libraries are top-down like this.
• GUI programming
– frameworks: MacApp, MFC, Java AWT
• Using an OO language means we can employ class
reuse, templates, and polymorphism.
• Graphical editors
– specific applications made from the framework: • MFC provides more in the framework than some
Drawing, musical composition, CAD other smaller GUI libraries.
• Compilers (various languages, machines) – e.g. “empty” application, get a bunch of menus, and a
• Financial modeling applications toolbar (example next page).
• Simulations – richer set of components: color-chooser dialog, file
browser, and much more.
(we’ll discuss later in the semester)

Intro to MFC [Bono] 11 Intro to MFC [Bono] 12

2
MFC vs. other libraries (cont.)
Empty MFC application • Advantages to application framework:
– less code for you to write:
• application gets built faster
• including less low-level tedious code
– more reuse of code between applications:
• we can focus on what’s different about our application
– uniform look and feel to applications produced
• less frustration for users/customers
• Disadvantages to application framework
– larger learning curve
– may produce a slower application
– may be harder to do exactly what you want
(only part of the window shown) • e.g., you want a different look-and feel, or you want a new
component

Intro to MFC [Bono] 13 Intro to MFC [Bono] 14

Model-View-Controller Architecture MVC (cont.)

• Model-View-Controller (MVC) • Model classes maintain the data of the application


– example of an OO design pattern • View classes display the data to the user
– started with Smalltalk • Controller classes allow user to
– a way to organize GUI programs – manipulate data in the model
• Main idea: separate the GUI code from the rest of – or to change how a view is displayed
the application. • Modified version: controllers and views combined
• Why? (MFC does this)
– more readable code
– more maintainable code (more details later)

Intro to MFC [Bono] 15 Intro to MFC [Bono] 16

MVC structure MVC example: Bank account

change change
Controller Make Make
deposit withdrawal
change
update update
View View change
Current balance update Plot of balance
update
Model view over last month
Bank
getData getData account
getData getData
model view controller
maintains displays current user manipulates
data state to user data in a model
or how view displayed

Intro to MFC [Bono] 17 Intro to MFC [Bono] 18

3
Document-view architecture SDI and MDI
• MFC has two flavors of applications
• In MFC version of Model-View-Controller:
– SDI = Single document interface
– Models are called Document objects
– MDI = Multiple document interface
– Views and Controllers are called View objects
• Examples
• Example: in Microsoft Word
– Word uses MDI
– Views: • can have multiple documents open simultaneously
• multiple windows open displaying same document
• may see multiple smaller windows in the larger window
• different types of views (normal, page layout, outline views)
– Notepad uses SDI
– Document: • only can have one document open at a time
• same data regardless of the view above
• view fills up frame of window
• contains text/formatting of Word document
• We’ll focus on SDI
Intro to MFC [Bono] 19 Intro to MFC [Bono] 20

SDI classes Four classes of SDI Application


• Every SDI application has the following four
classes: • Instances:
– CApp – Always one App
– CDoc – Always one MainFrame
– CView – Always one Document
– CMainFrame – May have multiple views on Same Document
• Our application will have classes derived from these • Key part of learning MFC:
classes – familiarize yourself with these four classes
– AppWizard will create them automatically when we ask
– learn what each one does
for an SDI MFC application
– learn when and where to customize each of them
• The relationship between these classes is defined by
the framework.

Intro to MFC [Bono] 21 Intro to MFC [Bono] 22

Examples of Customization Benefits of Document/View


• Views • Recall organization:
– OnDraw handles most output (you write; MFC calls)
– respond to input (write message handlers; MFC calls them)
– GUI stuff is in View classes
– non-GUI stuff is in Document (and related) classes
• Document
– stores data • Benefits: modifiability and readability
– most of the (non-GUI) meat of the application will be in this object – Can add new Views fairly easily
or objects accessible from here • would be difficult if data were closely coupled with its view
• CMainFrame • Examples:
– OnCreate is used to set up control bars – spreadsheet: have a grid of cells view; add a bar graph view
– target a different platform (with different GUI primitives)
– (rarely need to customize in practice)
– Can develop each part independently
• CWinApp • clear interface between the two parts
– can use to store application-wide data
– (rarely need to customize in practice)
Intro to MFC [Bono] 23 Intro to MFC [Bono] 24

You might also like