VC++ Programming @ UESTC
1
Part II MFC View Class
Chapter 3
Getting Started with AppWizard
Chapter 4
Basic Event Handling,Mapping Modes and
a Scrolling View
Chapter 5
The Graphics Device Interface,Colors and Fonts
Chapter 6
The Modal Dialog and Windows Common Controls
VC++ Programming @ UESTC
2
Part II MFC View Class
Chapter 7
The Modeless Dialog and Windows Common
Dialogs
Chapter 8
Using ActiveX Controls
Chapter 9
Win32 Memory Management
Chapter 10 Bitmaps
Chapter 11
Windows Message Processing and Multithreaded
Programming
VC++ Programming @ UESTC
3
Chapter 3
Getting Started with AppWizard
This hands-on chapter shows you how to build a MFC
library application working with only the view element.
How to use Graphics Device Interface (GDI)
How to use Resource Editor
Debug Target vs,Release Target
Understand Diagnostic Macros
Understand the Precompiled Headers
VC++ Programming @ UESTC
4
What’s a View?
From a user’s standpoint,a view is a window where the
user can play with a mouse and keyboard; From a
programmer’s perspective,a view is a C++ object of a class
derived from a CView class.
We’ll make a single document application (SDI) to show
the details.
Chapter 3
Getting Started with AppWizard
VC++ Programming @ UESTC
5
Project Type
MFC AppWizard (exe)
Application Type
Single document
Files Generated
ex03aView.cpp and ex03aView.h
Compile and link
Test the application
Browse the application
Source Browser
Chapter 3
Getting Started with AppWizard
VC++ Programming @ UESTC
6
Simply Drawing inside the View Window
Window Reaction Mechanism
Window Changes? Invalidate()? OnDraw()
Use ClassView to add the OnDraw function
Add codes inside OnDraw() to do something
Chapter 3
Getting Started with AppWizard
VC++ Programming @ UESTC
7
Introduce the CDC class
Windows communicate with the hardware indirectly
through an abstraction layer called,device context”,which
is a C++ object of class CDC.
For instance,you use CDC member function TextOut()
to write text on the view.
Chapter 3
Getting Started with AppWizard
VC++ Programming @ UESTC
8
Void CEx03aView::OnDraw(CDC* pDC)
{ pDC->TextOut(0,0,"Hello,world!");
// prints in default font,size,top left corner
pDC->SelectStockObject(GRAY_BRUSH);
// selects a brush for the circle interior
pDC->Ellipse(CRect(0,20,100,120));
// draws a gray circle 100 units in diameter
}
Chapter 3
Getting Started with AppWizard
VC++ Programming @ UESTC
9
Contents of the Resource File ( ex03a.rc)
Accelerator
Dialog
Icon
Menu
String Table
Toolbar
Version
Chapter 3
Getting Started with AppWizard
VC++ Programming @ UESTC
10
Statements inside ex03a.rc to be reexamined
#include,afxres.h”
#include,afxrec.rc”
---contains MFC library resource common to all application
#include,resource.h”
--- contains special resource in this project
Chapter 3
Getting Started with AppWizard
VC++ Programming @ UESTC
11
Debug Target vs,Release Target
What is the purpose of using debug build option?
Executable size difference between the two options
Path to locate the target
Other related settings
Debug Enables the Diagnostic Macros
Use Tracer utility
Chapter 3
Getting Started with AppWizard
VC++ Programming @ UESTC
12
,StdAfx.h” file contains the MFC header files used in this
project.
switch setting and precompiled headers
Automatic precompiled headers /Yx vs,Manual
precompiled headers /Yc /Yu
Automatic switch stores compiler output in a database file.
/Yc,used only with stdAfx.cpp causes the creation of the
precompiled (PCH) file.
/Yu,used with all other source file,causes use of the existing
PCH file.
Chapter 3
Getting Started with AppWizard
VC++ Programming @ UESTC
13
Chapter 3
Getting Started with AppWizard
VC++ Programming @ UESTC
14
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
In this section,we’ll learn
How to use message map to handle events;
How to deal with the windows mapping mode;
How to use the more practical scrolling view window.
VC++ Programming @ UESTC
15
Start with OnDraw() function we discussed before
What are the Windows Events?
keystrokes,mouse clicks,window resize,
window close ….
OnDraw() responds to any window resize event
and connect the application framework to your
own codes.
what about the menu,toolbar event?
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
16
Event Handling Mechanism
A Message ID defines a message for the specific event.
In class header,h file,you must have prototype
afx_msg void OnLButtonDown(UINT nFlags,Cpoint point);
DECLARE_MESSAGE_MAP() SECTION
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
17
In class source code file (.cpp file),you must have
member function
OnLButtonDown(UINT nFlags,Cpoint point);
message map macro such as
BEGIN_MESSAGE_MAP(CMyView,Cview)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
18
At this point,we simply need to know that there’re
predefined windows message and user defined message
which will respond to different events.
Visual C++ provides a tool ClassWizard to help us make
this job easier.
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
19
Let’s look at the project ex04a to learn more
about the view class.
save the view state
initialize the view class data member
Know MFC class such as CPoint,CRect,CSize,
CRgn…..
understand the window’s client area
use MFC function to test if a point inside a
region
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
20
// ex04aView.h,interface of the CEx04aView class
//
/////////////////////////////////////////////////////////////////////////////
class CEx04aView,public CView
{
protected,// create from serialization only
CEx04aView();
DECLARE_DYNCREATE(CEx04aView)
// Attributes
public:
CEx04aDoc* GetDocument();
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
21
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CEx04aView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC,CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC,CPrintInfo* pInfo);
//}}AFX_VIRTUAL
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
22
// Implementation
public:
virtual ~CEx04aView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
//{{AFX_MSG(CEx04aView)
afx_msg void OnLButtonDown(UINT nFlags,CPoint
point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
23
private:
int m_nColor;
CRect m_rectEllipse;
};
#ifndef _DEBUG // debug version in ex04aView.cpp
inline CEx04aDoc* CEx04aView::GetDocument()
{ return (CEx04aDoc*)m_pDocument; }
#endif
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
24
////////////////////////////////////////////////////////////////////////////
// CEx04aView.CPP
IMPLEMENT_DYNCREATE(CEx04aView,CView)
BEGIN_MESSAGE_MAP(CEx04aView,CView)
//{{AFX_MSG_MAP(CEx04aView)
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT,CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT,CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW,CView::OnFilePrintPreview)
END_MESSAGE_MAP()
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
25
void CEx04aView::OnLButtonDown(UINT nFlags,
CPoint point)
{
if (m_rectEllipse.PtInRect(point)) {
if (m_nColor == GRAY_BRUSH) {
m_nColor = WHITE_BRUSH;
}
else {
m_nColor = GRAY_BRUSH;
}
InvalidateRect(m_rectEllipse);
}
}
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
26
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
27
Device Coordinates vs,Logical Coordinates
Device coordinates are associated with display pixels.
Logical coordinates are associated with mathematical
coordinates.
The mapping mode defines the unit of measure used to
convert logical units to device units; it also defines the
orientation of the device’s x- and y-axes,
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
28
The Mapping Mode can be set with
virtual int CDC::SetMapMode (int nMapMode );
nMapMode Specifies the new mapping mode,It can be any
one of the following values:
MM_ISOTROPIC
MM_ANISOTROPIC
MM_TEXT
MM_HIENGLISH
MM_HIMETRIC
MM_LOENGLISH
MM_LOMETRIC
MM_TWIPS
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
29
Variable-Scale Mapping Modes
MM_ANISOTROPIC Logical units are converted to arbitrary units with
arbitrarily scaled axes,
Setting the mapping mode to MM_ANISOTROPIC does not change the current
window or viewport settings,To change the units,orientation,and scaling,call
the SetWindowExt and SetViewportExt member functions.
MM_ISOTROPIC Logical units are converted to arbitrary units with
equally scaled axes; that is,1 unit along the x-axis is equal to 1 unit
along the y-axis.
Use the SetWindowExt and SetViewportExt member functions to specify the
desired units and the orientation of the axes,GDI makes adjustments as
necessary to ensure that the x and y units remain the same size.
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
30
Fixed-Scale Mapping Modes
Positive x is to the right; positive y is up.
MM_HIENGLISH Each logical unit is converted to 0.001 inch,
MM_HIMETRIC Each logical unit is converted to 0.01 millimeter,
MM_LOENGLISH Each logical unit is converted to 0.01 inch..
MM_LOMETRIC Each logical unit is converted to 0.1 millimeter,
MM_TWIPS Each logical unit is converted to 1/20 of a point,
(Because a point is 1/72 inch,a twip is 1/1440 inch.)
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
31
MM_TEXT Each logical unit is converted to 1 device
pixel,Positive x is to the right; positive y is down.
You’re required to work between logical and device
systems.
Windows GDI takes care of the logical-to-device translation.
– Using CDC:LPtoDP() and DPtoLP()
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
32
Let’s kook at the ex04c project to understand more about it.
void CEx04cView::OnLButtonDown(UINT nFlags,CPoint point)
{ CClientDC dc(this);
OnPrepareDC(&dc);
CRect rectDevice = m_rectEllipse;
dc.LPtoDP(rectDevice); //Rect is now in device
corordinates
if (rectDevice.PtInRect(point)) {
if (m_nColor == GRAY_BRUSH)
m_nColor = WHITE_BRUSH;
else
m_nColor = GRAY_BRUSH;
InvalidateRect(rectDevice);
}
}
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
33
CScrollView is derived from Cview and supports scrolling
from the scroll bar,But we need to add codes to support
keyboard scrolling.
A window is larger than a viewport so we need to scrolling
bar to traverse the whole window.
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
34
Again,let’s kook at the ex04c project
class CEx04cView,public CScrollView
IMPLEMENT_DYNCREATE(CEx04cView,CScrollView)
BEGIN_MESSAGE_MAP(CEx04cView,CScrollView)
//{{AFX_MSG_MAP(CEx04cView)
ON_WM_KEYDOWN()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT,CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT,CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW,CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
35
void CEx04cView::OnKeyDown(UINT nChar,UINT nRepCnt,UINT
nFlags)
{
switch (nChar) {
case VK_UP:
OnVScroll(SB_LINEUP,0,NULL);
break;
case VK_DOWN:
OnVScroll(SB_LINEDOWN,0,NULL);
break;
……………………………………..
default:
break;
}
}
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View
VC++ Programming @ UESTC
36
Other Windows Messages
WM_CREATE
WM_CLOSE
WM_WUERYENDSESSION
WM_DESTROY window and its children active
WM_NCDESTROY all children destroyed
Chapter 4 Basic Event Handling,Mapping
Modes and a Scrolling View