1. What is MFC, and what are its advantages?
The Microsoft Foundation Class (MFC) library is an object-oriented framework for building Windows applications. It simplifies GUI development by providing ready-made classes for window management, message handling, and user input. MFC is advantageous because it allows developers to create complex applications without dealing directly with Windows API. The framework’s abstraction layer makes coding faster and more efficient.
// Example: Creating a simple MFC application
class CMyApp : public CWinApp
{
BOOL InitInstance()
{
CFrameWnd* Frame = new CFrameWnd();
Frame->Create(NULL, _T("My MFC Application"));
m_pMainWnd = Frame;
Frame->ShowWindow(SW_NORMAL);
return TRUE;
}
};
CMyApp theApp;
2. Explain the document/view architecture in MFC.
MFC uses the document/view architecture to separate data handling (document) from its presentation (view). This model makes it easier to maintain and extend code, especially in applications that require different ways of presenting data. The document class manages the data, while the view class handles user interaction and data display. MFC provides prebuilt classes such as `CDocument` and `CView` to support this architecture.
3. What is the purpose of the CWinApp
class?
The `CWinApp` class represents the core of an MFC application. It is responsible for initializing the application, creating the main window, handling command-line arguments, and managing the message loop. The `CWinApp` object is the first to be created and the last to be destroyed in an MFC application. It ensures that all resources are properly initialized before the application starts and cleaned up when it closes.
// Example: CWinApp class usage
class MyApp : public CWinApp
{
BOOL InitInstance()
{
CFrameWnd* Frame = new CFrameWnd();
Frame->Create(NULL, _T("CWinApp Example"));
m_pMainWnd = Frame;
Frame->ShowWindow(SW_NORMAL);
return TRUE;
}
};
MyApp theApp;
4. How do you handle messages in MFC?
In MFC, messages are handled through a message map. Windows messages, like button clicks or key presses, are routed to the appropriate class methods using macros in the message map. The `ON_COMMAND()` and `ON_WM_PAINT()` macros are commonly used to link messages to their handlers. Developers override these handler functions to define the application’s response to specific events.
5. What is the role of the message map in MFC?
The message map in MFC serves as a lookup table that links Windows messages to their corresponding handler functions. It is implemented using macros like `BEGIN_MESSAGE_MAP()` and `END_MESSAGE_MAP()`. When a message is received, MFC checks the message map to find the appropriate function to call. This architecture simplifies the message-handling process, making code more modular and easier to maintain.
6. Explain the difference between ON_WM_PAINT()
and ON_COMMAND()
.
`ON_WM_PAINT()` handles the `WM_PAINT` message, which is sent when a window’s client area needs to be redrawn. Developers use this macro to override the painting behavior of a window. `ON_COMMAND()`, on the other hand, is used to handle command messages such as button clicks or menu selections. It routes user interface commands to their corresponding handler functions.
// Example: Message map in MFC
BEGIN_MESSAGE_MAP(CMyWnd, CFrameWnd)
ON_WM_PAINT()
ON_COMMAND(ID_FILE_NEW, &CMyWnd::OnFileNew)
END_MESSAGE_MAP()
7. What are the main differences between modal and modeless dialogs?
In MFC, modal dialogs require user interaction before proceeding to other windows, while modeless dialogs allow users to interact with other windows simultaneously. Modal dialogs are often used for critical tasks, such as confirmation messages, whereas modeless dialogs are used for tools that stay open throughout the application’s life.
8. How do you create a toolbar in MFC?
In MFC, toolbars are created using the `CToolBar` class. You first define the toolbar in a resource file, and then load it in the main window class using `LoadToolBar()`. Toolbars provide quick access to frequently used commands and are typically located beneath the menu bar.
// Example: Creating a toolbar in MFC
if (!m_wndToolBar.CreateEx(this) || !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbarn");
return -1; // fail to create
}
9. What is the use of UpdateData(TRUE)
and UpdateData(FALSE)
?
The `UpdateData()` function is used to transfer data between variables and controls in MFC dialogs. `UpdateData(TRUE)` retrieves data from the controls and stores it in corresponding variables, while `UpdateData(FALSE)` updates the controls with data from the variables.
10. Explain the concept of device context (DC) in MFC.
The device context (DC) is an object in MFC that represents the drawing surface of a window or a device like a printer. It is used to perform graphical operations, such as drawing lines, text, and shapes. To paint on the window, developers first retrieve the device context using `GetDC()` and then use various drawing functions to render content on the window.
// Example: Drawing in a device context
CDC* pDC = GetDC();
pDC->MoveTo(10, 10);
pDC->LineTo(200, 200);
ReleaseDC(pDC);
Leave a Reply