MICROSOFT FOUNDATION CLASSES
MFC:
CObject is the base class for most of the MFC Classes.
CObject is the base class for most of the MFC Classes.
Features:
i) RUNTIME_CLASS
ii) Serialize
i) RUNTIME_CLASS
ii) Serialize
What is a Message Map?
a) Message Maps are the way by which MFC handles the application messages.
b) Gets message from the OS, translates the message and dispatches the message to the window procedure.
c) In MFC, 'Run' method, starts the message map.
d) Classes derived from 'CCmdTarget' can contain a message map.
b) Gets message from the OS, translates the message and dispatches the message to the window procedure.
c) In MFC, 'Run' method, starts the message map.
d) Classes derived from 'CCmdTarget' can contain a message map.
.h -> DECLARE_MESSAGE_MAP()
a) Declares MessageMap Structure.
.cpp -> BEGIN_MESSAGE_MAP() -> Initializes MessageMap Structure
... -> Fills MessageEntries[] array
END_MESSAGE_MAP() -> Terminates MessageEntries[] array
b) Tells the application that the class in which this is called is going to have a
messagemap and handle messages.
c) Adds three members to the class declaration.
i) MessageEntries[] array (AFX_MSGMAP_ENTRY)
ii) MessageMap Structure
iii) GetMessageMap() -> Virtual Function that returns MessageMap's address
.cpp -> BEGIN_MESSAGE_MAP() -> Initializes MessageMap Structure
... -> Fills MessageEntries[] array
END_MESSAGE_MAP() -> Terminates MessageEntries[] array
e.g:
BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
ON_WM_LBUTTONDOWN() --> Macro, which declares that MyFrame is going to handle left button
END_MESSAGE_MAP()
CFrameWnd -> The base class
MyFrame -> The class name which implements the message map
Refer: Application Messages
MessageMap Macros:
ON_COMMAND_RANGE(id1, id2,...., eventhandlers)
Difference between PostMessage and SendMessage functions:
PostMessage:
Posts the message to the application queue(follows FIFO) -> asynchronous call
SendMessage:
Sends the message to the particular control or window -> synchronous call
Serialization in MFC:
To make a class serializable it is necessary to add two macros.
.h -> DECLARE_SERIAL(<classname>)
.cpp -> IMPLEMENT_SERIAL(<classname>, CObject)
Serializing: (application -> disk)
void <classname>::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
...
}
DeSerializing: (disk -> application)
void <clasname>::DeleteContents()
{
CDocument::DeleteContents();
...
}
Types of Applications in MFC:
a) Document Interface
SDI -> Single Document Interface
Supports one open document at a time.
MDI -> Multi Document Interface
Supports two or more documents open simultaneously.
Document/View Architecture in MFC:
a) In a Doc/View application, the application data is represented by a document object.
b) Views of that data are represented by one or more view objects
CDocument class is the base class for the document objects.
CView class and its derivates are base classes for view objects.
UpdateAllViews
GetFirstViewPosition
GetNextView
b) Dialog based
DDX -> Data Exchange
DDV -> Data Verification
UpdateData(TRUE) -> data from controls to variables
UpdateData(FALSE) -> data from variables to control
Dialogs: (CDialog -- Derived from CWnd)
Initialization --> OnInitDialog()
Closing --> EndDialog()
Modal : Without closing the dialog, the user cannot activate the parent window.
DoModal() - is used to display a modal dialog box.
Modeless : Modeless dialog is a container component that appears on top of the main
content without blocking user interaction with the underlying page.
ShowWindow() - ShowWindow function sets the specified window's show state. Both are derived from CDialog.
Collection Classes in MFC:
MFC provides collection classes of three categories:
A) MFC Array Classes
B) MFC List Classes
C) MFC Map Classes
MFC provides collection classes of three categories:
A) MFC Array Classes
B) MFC List Classes
C) MFC Map Classes
A) MFC Array Classes: can be accessed using index.
a) CByteArray -> 8 bit bytes
b) CWordArray -> 16 bit bytes
c) CUIntArray -> unsigned integers
d) CStringArray -> CString
e) CPtrArray -> void pointers
f) CObArray -> CObject pointers
a) CByteArray -> 8 bit bytes
b) CWordArray -> 16 bit bytes
c) CUIntArray -> unsigned integers
d) CStringArray -> CString
e) CPtrArray -> void pointers
f) CObArray -> CObject pointers
important functions:
SetAt(), SetSize(), GetSize(), GetUpperBound(), Copy(), RemoveAt(), RemoveAll()
B) MFC List Classes:
a) CObList -> CObject pointers
b) CPtrList -> void pointers
c) CStringList -> CString
datatype: POSITION -> pointer to a CNode
important functions:
AddHead(), RemoveHead(), RemoveTail(), GetAt(), SetAt(), GetPrev(),
GetHeadPosition, GetTailPosition(), RemoveAt(), InsertAfter(), InsertBefore()
AddHead(), RemoveHead(), RemoveTail(), GetAt(), SetAt(), GetPrev(),
GetHeadPosition, GetTailPosition(), RemoveAt(), InsertAfter(), InsertBefore()
C) MFC Map Classes: [Key-Value pair]
a) CMapWordToPtr -> void pointers keyed by WORDs
b) CMapPtrToWord -> WORDs keyed by void pointers
c) CMapPtrToPtr -> void pointers keyed by other void pointers
d) CMapWordToOb -> Object pointers keyed by WORDs
e) CMapStringToOb -> CObject pointers keyed by strings
f) CMapStringToPtr -> void pointer keyed by strings
g) CMapStringToString -> Strings keyed by Strings
important functions:
Lookup(), GetStartPosition(), GetNextAssoc(), RemoveKey(), RemoveAll(), GetCount(), IsEmpty()
Classes which are not derived from CObject:
MFC Simple Value Types and Typed Template Collection Classes are not derived from CObject.
MFC Simple Value Types and Typed Template Collection Classes are not derived from CObject.
Simple Value Types: CPoint, CRect, CSize, CString, CTime, CTimeSpan
Typed Template Collection Classes: CTypedPtrArray, CTypedPtrList, CTypedPtrMap
Typed Template Collection Classes: CTypedPtrArray, CTypedPtrList, CTypedPtrMap
DeviceContext:
a) Provides access to GdiObjects.
b) Provides a logical display area for drawing.
-OS transfers it to the actual device drawing.
CDC:
CPaintDC -> draws in Windows Client Area
sends WM_ERASEBKGND (marks the invalid region to valid)
CClientDC -> draws in Windows Client Area
CWindowDC -> draws in client and non-client area
CMetaFileDC -> draws in meta file
CGdiObject:
CBitMap, CBrush, CFont, CPalette, CPen, CRegn
SelectObject : Selects the current object, returns the address of the passed object
DeleteObject : Deletes the selected Object
User Interface thread and Worker thread in MFC:
CWinThread is the base class for threads.
CWinThread is derived from CCmdTarget.
UI Thread : The main thread of MFC is an UI thread.
UI threads can handle user messages.
WorkerThread: Background threads are called as Worker Threads.
AfxBeginThread() - AfxEndThread()
BeginThread() - EndThread()
BeginThreadEx() - EndThreadEx()
Synchronization methods in MFC:
CSyncObject: A pure virtual class that provides functionality common to the synchronization objects in Win32.
CCriticalSection : A synchronization object that allows one thread at a time to access a resource or section of code (Mutual Exclusion).
CMutex : A synchronization object that allows one thread mutually exclusive access to a resource(Mutual Exclusion - multiple applications).
CSemaphore : Semaphores are useful in controlling access to a shared resource that can only support a limited number of users (Limited Thread).
CEvent : Represents an event, which is a synchronization object that enables one thread to notify another that an event has occurred.
SetEvent() - Releases the thread (signaled state)
ReleaseEvent() - Resets to non-signaled state
WinSock:
Sockets are used to communicate with different applications running in the same machine or on a different machine.
(i) network communication
(ii) client/server application
client-server application:
(i) Initialize Sockets, create/listen/accept/connection.
(ii) The address of the computer on which the application is running.
(iii) The port on which it is listening.
a) requests for connection
b) establishes connection
c) sends/receives information
d) closes the connection
Socket Classes:
a) CAsynSocket
b) CSocket
Methods:
a) OnAccept
b) OnClose
c) OnConnect
d) OnReceive
e) OnSend
Create -> Creates the socket
Connect -> Connects the server
Listen -> Server Listens
Accept -> Accepts socket communication
Send -> Sending information
Receive -> Receiving information
Close -> Closing Connection
Client Server
Create Create
Connect Listen
Send/Receive Accept
Send/Receive
Close Close
Stream/Datagram Protocols:
TCP/IP -> Transmission Control Protocols
[establishes a connection,
retains the connection throughout the communication]
UDP -> Datagram Protocols
[receiver sends back the acknowledgement]
MFC DLLs:
(i) MFC Extension DLLs:
Can a used by other MFC application.
Add AFX_EXT_CLASS to the class declaration.
(ii) Regular DLLs:
To export a function
a) extern "C"
b) AFX_MANAGE_STATE(AfxGetStaticModuleStatic())
used to make the exported functions threads safe
AfxGetStaticModuleStatic - returns AFX_MODULE_STATE
b) Module Definition File(.def)
Contains the exported functions in a dll, with its ordinals.