I recently wrote an MFC program, and I feel that it has been developing for many years and should be well-developed. The specific manifestation is that the relevant documentation is comprehensive, the Demo of related functions can be easily accessed by google. Therefore, it is okay to do some basic functions on MFC.
MFC documentation
The documentation on MSDN is also comprehensive. For more information, see MFC Reference and Controls (MFC ). Some of the features mentioned in this article are not only related to MFC. Now that it is used, remember it together to facilitate query.
Function
Tray Icon
In the tray in the lower right corner of windows, the icon is displayed. This function is common. The reference code is as follows:
[Cpp]
NOTIFYICONDATAW m_IconData;
M_IconData.cbSize = sizeof (policyicondataw );
M_IconData.uCallbackMessage = m_messageId; // The Message ID of the Try icon
M_IconData.dwInfoFlags = NIIF_INFO;
M_IconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; // set the message received by the icon.
M_IconData.uID = ID_TRAY_ICON; // ID of the icon to be distinguished
M_IconData.hWnd = hWnd; // message receiving window
M_IconData.hIcon = m_hIconOffLine; // IDI_TRAY is the ID of the ICON defined in the resource file, which can be generated using a built-in tool in the resource sheet.
CMsg info (IDS_TRAY_VERSION );
Wcscpy (m_IconData.szTip, info );
Shell_policyicon (NIM_ADD, & m_IconData );
NOTIFYICONDATAW m_IconData;
M_IconData.cbSize = sizeof (policyicondataw );
M_IconData.uCallbackMessage = m_messageId; // The Message ID of the Try icon
M_IconData.dwInfoFlags = NIIF_INFO;
M_IconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; // set the message received by the icon.
M_IconData.uID = ID_TRAY_ICON; // ID of the icon to be distinguished
M_IconData.hWnd = hWnd; // message receiving window
M_IconData.hIcon = m_hIconOffLine; // IDI_TRAY is the ID of the ICON defined in the resource file, which can be generated using a built-in tool in the resource sheet.
CMsg info (IDS_TRAY_VERSION );
Wcscpy (m_IconData.szTip, info );
Shell_policyicon (NIM_ADD, & m_IconData); for more details, see NOTIFYICONDATA structure and shell_policyicon function.
Multiple Languages
A simple implementation method for multiple languages is to define a DLL project. In addition to creating a new rc file, add all other resource files and modify the strings in the rc file. Then, load it to the running program in the following way:
[Cpp]
M_hInsChs = LoadLibrary (_ T ("LangChs. dll"); // LangChs. dll is the dll corresponding to a language.
AfxSetResourceHandle (m_hInsChs );
M_hInsChs = LoadLibrary (_ T ("LangChs. dll"); // LangChs. dll is the dll corresponding to a language.
AfxSetResourceHandle (m_hInsChs); refer to the article Internationalization and Multiple Language Support, which contains a runable and tested Demo.
Property page dialog box
In MFC, the properties page dialog box can be used to create the configuration page and Wizard Page. The associated classes are CPropertySheet and CPropertyPage. The define attribute page dialog box is similar to a common dialog box, which is not described here.
To modify some properties in the properties page dialog box, see Hacking the CPropertySheet.
The displayed property page dialog box is displayed. You can refer to the following statements:
[Cpp]
If (! M_initializeDialog.m_hWnd) {// determines whether the dialog box is created.
M_initializeDialog.SetWizardMode (); // switch to the wizard mode.
M_initializeDialog.Create (); // create and display
} Else {
M_initializeDialog.SetForegroundWindow (); // if it has been created, it is displayed
}
If (! M_initializeDialog.m_hWnd) {// determines whether the dialog box is created.
M_initializeDialog.SetWizardMode (); // switch to the wizard mode.
M_initializeDialog.Create (); // create and display
} Else {
M_initializeDialog.SetForegroundWindow (); // if it has been created, it is displayed
}
Keep folder
The so-called folder Persistence means to control the folder in the program to prevent users from deleting the folder outside the program. Refer to the following code:
[Cpp]
HANDLE hDir = CreateFileW (dirpath,
GENERIC_READ,
File_pai_read | file_pai_write,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
NULL );
HANDLE hDir = CreateFileW (dirpath,
GENERIC_READ,
File_pai_read | file_pai_write,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_BACKUP_SEMANTICS,
NULL); For more information, see CreateFile function.
Static text box with color
For more information, see Using colors in CEdit and CStatic. This article uses a class to inherit and extend the functions of the CStatic control.
Hyperlink
In MFC, there is a Hyperlink control, and it does not feel very useful. So I found a CStatic-based custom control Article Hyperlink control. To use this control, you may need to slightly modify its source code.
Select folder dialog box
You can directly refer to the CFolderDialog-Selecting Folders article, which encapsulates Windows APIs. For details, refer to the source code.
MD5 Transcoding
Use the solution in the article "Use Windows Crypto API to calculate a MD5 string. However, this is non-Unicode encoded and should be noted during use.
Additional Console
This function is used for debugging when the MFC program is started, and the console is displayed for printing. For this part, refer to the following statement:
[Cpp]
Int hCrt;
FILE * hf;
// Create a new console
AllocConsole ();
// Bind to standard output
HCrt = _ open_osfhandle (long) GetStdHandle (STD_OUTPUT_HANDLE), _ O_TEXT );
Hf = _ fdopen (hCrt, "w ");
// Replace stdout with the standard output to make printf available
M_stdoutOld = * stdout;
* Stdout = * hf;
// Clear the output Cache
Setvbuf (stdout, NULL, _ IONBF, 0 );
// Test
Printf ("my console is running from printf \ n ");
Std: cout <"my console is running from std: cout" <std: endl;
Int hCrt;
FILE * hf;
// Create a new console
AllocConsole ();
// Bind to standard output
HCrt = _ open_osfhandle (long) GetStdHandle (STD_OUTPUT_HANDLE), _ O_TEXT );
Hf = _ fdopen (hCrt, "w ");
// Replace stdout with the standard output to make printf available
M_stdoutOld = * stdout;
* Stdout = * hf;
// Clear the output Cache
Setvbuf (stdout, NULL, _ IONBF, 0 );
// Test
Printf ("my console is running from printf \ n ");
Std: cout <"my console is running from std: cout" <std: endl;
Callback
In MFC, it is inevitable to use the callback mechanism. You can simply use the sigslot library, which has only one header file. For detailed usage, see Signal mechanism, one of libjingle source code analysis.