Creating and calling interfaces in MFC, MFC DLL, MFC ACTIVEX for beginners

The learning of MFC is mainly based on

http://www.jizhuomi.com/software/257.html

Contents of the MFC message mapping mechanism

What is a message: A window message generally consists of three parts: 1. an unsigned integer, which is the message value; (2) a parameter of the WPARAM type attached to the message; (3) a parameter of the LPARAM type attached to the message. In fact, what we generally call a message is a message value in a narrow sense, that is, an unsigned integer, which is often defined as a macro.

Message mapping mechanism: MFC uses a message mapping mechanism to process messages. The performance in the application framework is a message mapping table with one-to-one correspondence between messages and message processing functions, as well as the declaration and implementation codes of message processing functions. When the window receives a message, it will look up the message processing function corresponding to the message in the message mapping table, and then the message processing function will perform corresponding processing. When programming the SDK, you need to judge the message values ​​one by one in the window process and process them accordingly. In contrast, MFC's message mapping mechanism is much more convenient and easy to use.

Message classification under Windows:

 Let's talk about the classification of Windows messages first. Windows messages are divided into system messages and user-defined messages. There are three types of Windows system messages:

       1. Standard Windows messages. Messages starting with WM_ except WM_COMMAND are standard messages. For example, WM_CREATE, WM_CLOSE.

       2. Command message. The message is named WM_COMMAND, and the identifier ID is attached to the message to distinguish which menu, toolbar button or accelerator key the message is from.

       3. Notification message. The notification message is generally sent to the parent window by the child window such as the list box, and the message name is also WM_COMMAND, which is accompanied by the control notification code to distinguish the control.

       CWnd-derived classes can receive standard Windows messages, notification messages, and command messages. Command messages can also be received by document classes and the like.

       User-defined message is actually a macro defined by the user as a message. The value of this macro should be greater than or equal to WM_USER, and then this macro can be used like system messages, and its handler function can be defined in the window class.

add message handler

       How to add message handler function? There are three steps to adding either automatically or manually:

       1. Add the function declaration of the message processing function to the class definition, pay attention to start with afx_msg. For example, the function declaration of the message processing function of WM_CREATE in MainFrm.h: afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);.

       2. Add the message map entry of the message in the message map table of the class. For example, the message map entry of WM_CREATE: ON_WM_CREATE().

       3. Add the function implementation of the message processing function in the class implementation. For example, the implementation of the message handling function of WM_CREATE in MainFrm.cpp:

          int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
         {
                  ......
         }

       After passing the above three steps, messages such as WM_CREATE can be processed by the message processing function in the window class.

One: Create a dialog-based application framework

  1. Select the menu item File->New->Project to pop up the "New Project" dialog box.

       2. Select MFC under Visual C++ of Installed Templated in the left panel, select MFC Application in the middle window, and then type the project name in the Name edit box below. In this example, name it "Addition", and set the project's location in the Location edit box. save route. Click "OK".

       3. Click "Next" to the "Application Type" dialog box, select Dialog based under Application type, use the default settings for others, click "Finish".

After these three steps, the basic dialog template has come out. At this point, you need to set the properties of the dialog box, right-click and select Dialog-Properties. The following is a description of the basic properties:

  1.ID: Dialog ID, which uniquely identifies the dialog resource and can be modified. IDD_ADDITION_DIALOG here, we don't modify it.

       2.Caption: the title of the dialog box. The default here is Addition, we will modify it to "Addition Calculator".

       3.Border: border type. There are four types: None, Thin, Resizing and Dialog Frame. We use the default Dialog Frame.

       4.Maximize: Whether to use the maximize button. We use the default False.

       5.Minimize: Whether to use the minimize button. Again we use the default False.

       6.Style: dialog type. There are three types: Overlapped (overlapping windows), Popup (pop-up windows) and Child (child windows). Pop-ups are more common. We use the default Popup type.

       7. System Menu: Whether there is a system menu in the upper left corner of the title bar, including menu items such as move and close. We use the default True.

       8.Title Bar: Whether with a title bar. We use the default True.

       9.Font(Size): font type and font size. If you change it to a non-system font, Use System automatically changes to False. And if Use System was originally False and changed it to True, Font(Size) is automatically set to the system font. Here we use the default system font.

Here you can set the style of the dialog box you want.

Two: add controls to the dialog

Find the Static Text box in the Toolbox , which is used to display characters.  


Right-click the static text box - properties to find the caption to modify the content. Then modify the ID, also called the identifier. This ID is mainly used for the unique value of the control during message delivery.

Find the Edit Control in the Toolbox  for entering data

 

Modify the ID value, and then right-click the edit box - add a variable. At this time, since I plan to be a calculator, the variable added here is of the value class and double type. Then use the class wizard to map the control and variable message-passing form.

At this point there is a function:

void CMFCApplication1Dlg::DoDataExchange(CDataExchange* pDX)
{ CDialogEx::DoDataExchange(pDX
); //Handle MFC default data exchange

// handle data exchange between controls and variables
DDX_Text(pDX, IDC_SUMMAND_EDIT, m_editSummand);
DDX_Text(pDX, IDC_ADDEND_EDIT, m_editAddend);
DDX_Text(pDX, IDC_SUM_EDIT, m_editSum);
}

There is also a function to control the direction of message passing, from control to variable or variable to control.

UpdateData(TRUE); The control is passed to the variable.

UpdateData(FALSE); The control is passed to the variable.

At this point, a simple MFC framework is ready. Double-click the button and write the button operation function.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324750927&siteId=291194637