[MFC] basic knowledge of program framework and mfc framework

Source: Internet
Author: User

[MFC] basic knowledge of program framework and mfc framework

1. First, paste a simple Win32 Hello World Program, which is the basis for learning MFC.

If you have not learned Win32, please add relevant knowledge on your own.

# Include <windows. h> lresult callback WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {static TCHAR lpszAppName [] = TEXT ("HelloWin"); HWND hwnd; MSG msg; WNDCLASS wc; wc. style = CS_HREDRAW | CS_VREDRAW; wc. lpfnWndProc = WndProc; wc. cbClsExtra = 0; wc. cbWndExtra = 0; wc. hInstance = hInstance; wc. hIcon = LoadIcon (NULL, IDI_APPLICATION); wc. hCursor = LoadCursor (NULL, IDC_ARROW); wc. hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc. lpszMenuName = NULL; wc. lpszClassName = lpszAppName; // register the window class if (! RegisterClass (& wc) {MessageBox (NULL, TEXT ("This program requires Windows NT! "), LpszAppName, MB_ICONERROR); return 0;} // create The application Main Window hwnd = CreateWindow (lpszAppName, TEXT (" The Hello Program "), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); // display window ShowWindow (hwnd, nShowCmd); UpdateWindow (hwnd); // while (GetMessage (& msg, NULL, 0, 0) {TranslateMessage (& msg); DispatchMessage (& msg);} return msg. wParam ;}// // Window procedure function // lresult callback WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {HDC hdc; PAINTSTRUCT ps; RECT rect; switch (message) {case WM_CREATE: return 0; case WM_PAINT: hdc = BeginPaint (hwnd, & ps); GetClientRect (hwnd, & rect); DrawText (hdc, TEXT ("Hello World! "),-1, & rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER); EndPaint (hwnd, & ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0 ;} return DefWindowProc (hwnd, message, wParam, lParam );}

2. MFC is an encapsulation of Win32 API by Microsoft.

After creating a simple MFC program, such as Test, the entire program initialization and running process is roughly as follows:

Define theApp Global Object in CTestApp

-> Execute the CWinApp constructor of the parent class.

-> Execute the constructor of the subclass itself.

-> Call the AfxWinMain function.

-> The Afx function calls the initInstance function, including the registration window class, creation window, and display window.

-> Message loop


3. Program Framework

When you create a single-document MFC sample program, the program will include:

CTestApp-application class

CMainFrame, CTestView -- form class, which can be seen as a subclass of the former

CTestDoc -- document class


4. Notes:

MFC is actually an encapsulation of Win32 API functions, such as the CWnd class. We can also simulate this encapsulation process on our own.

Below is a simulation package I wrote myself:

#include <windows.h>LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);class CMyWnd{public:BOOL MyCreateWindow(   LPCTSTR lpClassName,   LPCTSTR lpWindowName,   DWORD dwStyle,   int x,   int y,   int nWidth,   int nHeight,   HWND hWndParent,   HMENU hMenu,   HANDLE hInstance,   PVOID lpParam );BOOL MyShowWindow(int nCmdShow);BOOL MyUpdateWindow();HWND GetHwnd();private:HWND m_hwnd;};BOOL CMyWnd::MyCreateWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName, DWORD dwStyle, int x,int y, int nWidth, int nHeight, HWND hWndParent, HMENU hMenu, HANDLE hInstance, LPVOID lpParam){m_hwnd = ::CreateWindow(lpClassName,lpWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,hMenu,(HINSTANCE)hInstance,lpParam);if(NULL == m_hwnd)return FALSE;elsereturn TRUE;}BOOL CMyWnd::MyShowWindow(int nCmdShow){return ::ShowWindow(m_hwnd, nCmdShow);}BOOL CMyWnd::MyUpdateWindow(){return ::UpdateWindow(m_hwnd);}HWND CMyWnd::GetHwnd(){return this->m_hwnd;}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ){MSG msg;WNDCLASS wndclass;CMyWnd wnd;TCHAR szName[] = TEXT("Class Test");//HWND hwnd;wndclass.cbClsExtra = 0;wndclass.cbWndExtra = 0;wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);wndclass.hInstance = hInstance;wndclass.lpfnWndProc = WndProc;wndclass.lpszClassName = szName;wndclass.lpszMenuName = NULL;wndclass.style = CS_HREDRAW | CS_VREDRAW;if(!RegisterClass(&wndclass)){MessageBox(NULL, TEXT("Need Window NT"), TEXT("Title"), MB_OK);return 0;}//wnd = new CMyWnd;wnd.MyCreateWindow(szName, TEXT("Window Test"), WS_OVERLAPPEDWINDOW,    CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,   NULL, NULL, hInstance, NULL);//hwnd = wnd->GetHwnd();wnd.MyShowWindow(nShowCmd);wnd.MyUpdateWindow();while(GetMessage(&msg, NULL, 0, 0)){TranslateMessage(&msg);DispatchMessage(&msg);}return msg.wParam;}LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){switch(message){case WM_DESTROY:PostQuitMessage(0);return 0;}return DefWindowProc(hwnd, message, wParam, lParam);}
It can be seen that the CMyWnd window class has no direct relationship with the window. The connection between them is actually the existence of m_hwnd;

The window is destroyed without affecting the window class. The same is true for the CWnd class encapsulated by Microsoft MFC;

Of course, we only perform a simple simulation and it is not perfect. When the window class is destroyed, the corresponding window will be destroyed in MFC.


5. add controls to the MFC Program

This is relatively simple. You can define a CButton object in the CMainFrame class, and then initialize this member object in the constructor of this class.

Of course, you can also write in the CTestView class. You can write an OnCreate function for the corresponding WM_CRAETE;

First, add the following content to the CTestView header file:

private:CButton m_btn;
Then add the following in the CTestView definition file:

// CTest1View message handlersint CTest1View::OnCreate(LPCREATESTRUCT lpCreateStruct) {if (CView::OnCreate(lpCreateStruct) == -1)return -1;m_btn.Create(TEXT("World"), WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, CRect(0,0,50,20), this, 124);return 0;}
In fact, this process can be written using the built-in generation tool of VC. We do not need to edit it manually.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.