对于软件系统,我们可以常看到其支持做种语言系统,比如中文、英文等等。那么在MFC中如何实现呢?其实很简单。制作流程如下。一下以最简单的对话框为列做个示范,大系统同样是该流程。(这里IDE使用 VC6.0 )
1、创建简单的对话框应用程序Dlg,同时添加Static文本控件。
2、创建动态库项目S_Chinese(空项目则可),然后把步骤一中所说的项目下的res文件夹、.rc文件、.resource.h、stdafx.h、stdafx.cpp文件拷贝到创建的动态库项目中,同时添加进工程,如下图所示
并将要修改的资源在此处进行修改。
3、S_Chinese.h 和S_Chinese.cpp文件可以直接从上面对话框项目的Dlg.h Dlg.cpp 并做一下修改便可。
Dlg.h
// Dlg.h : main header file for the DLG application // #if !defined(AFX_DLG_H__D1CC9B6F_8745_458B_9432_BB3DD26912FF__INCLUDED_) #define AFX_DLG_H__D1CC9B6F_8745_458B_9432_BB3DD26912FF__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols ///////////////////////////////////////////////////////////////////////////// // CDlgApp: // See Dlg.cpp for the implementation of this class // class CDlgApp : public CWinApp { public: CDlgApp(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CDlgApp) public: virtual BOOL InitInstance(); //}}AFX_VIRTUAL // Implementation //{{AFX_MSG(CDlgApp) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_DLG_H__D1CC9B6F_8745_458B_9432_BB3DD26912FF__INCLUDED_)Dlg.cpp
// Dlg.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "Dlg.h" #include "DlgDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CDlgApp BEGIN_MESSAGE_MAP(CDlgApp, CWinApp) //{{AFX_MSG_MAP(CDlgApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlgApp construction CDlgApp::CDlgApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CDlgApp object CDlgApp theApp; ///////////////////////////////////////////////////////////////////////////// // CDlgApp initialization BOOL CDlgApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif HINSTANCE hInst; hInst=LoadLibrary("S_Chinese.dll"); if(hInst!=NULL) AfxSetResourceHandle(hInst); CDlgDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }
S_Chinese.h
// S_Chinese.h : main header file for the S_Chinese DLL // #if !defined(AFX_S_Chinese_H__434B3D70_1ED9_44C1_B0B4_E51CB2EF620C__INCLUDED_) #define AFX_S_Chinese_H__434B3D70_1ED9_44C1_B0B4_E51CB2EF620C__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols ///////////////////////////////////////////////////////////////////////////// // CS_ChineseApp // See S_Chinese.cpp for the implementation of this class // class CS_ChineseApp : public CWinApp { public: CS_ChineseApp(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CS_ChineseApp) //}}AFX_VIRTUAL //{{AFX_MSG(CS_ChineseApp) // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional declarations immediately before the previous line. #endif // !defined(AFX_S_Chinese_H__434B3D70_1ED9_44C1_B0B4_E51CB2EF620C__INCLUDED_)
// S_Chinese.cpp : Defines the initialization routines for the DLL. // #include "stdafx.h" #include "S_Chinese.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif // // Note! // // If this DLL is dynamically linked against the MFC // DLLs, any functions exported from this DLL which // call into MFC must have the AFX_MANAGE_STATE macro // added at the very beginning of the function. // // For example: // // extern "C" BOOL PASCAL EXPORT ExportedFunction() // { // AFX_MANAGE_STATE(AfxGetStaticModuleState()); // // normal function body here // } // // It is very important that this macro appear in each // function, prior to any calls into MFC. This means that // it must appear as the first statement within the // function, even before any object variable declarations // as their constructors may generate calls into the MFC // DLL. // // Please see MFC Technical Notes 33 and 58 for additional // details. // ///////////////////////////////////////////////////////////////////////////// // CS_ChineseApp BEGIN_MESSAGE_MAP(CS_ChineseApp, CWinApp) //{{AFX_MSG_MAP(CS_ChineseApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CS_ChineseApp construction CS_ChineseApp::CS_ChineseApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CS_ChineseApp object CS_ChineseApp theApp;
; S_Chinese.def : Declares the module parameters for the DLL. LIBRARY "S_Chinese" DESCRIPTION 'S_Chinese Windows Dynamic Link Library' EXPORTS ; Explicit exports can go here5、编译得到S_Chinese.dll S_Chinese.lib文件,如果只是动态加载的话 ,只需要.dll文件
6、在Dlg项目中初始化对话框(Dlg.cpp文件)前面添加加载资源的句柄,如下所示
// Dlg.cpp : Defines the class behaviors for the application. // #include "stdafx.h" #include "Dlg.h" #include "DlgDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CDlgApp BEGIN_MESSAGE_MAP(CDlgApp, CWinApp) //{{AFX_MSG_MAP(CDlgApp) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG ON_COMMAND(ID_HELP, CWinApp::OnHelp) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlgApp construction CDlgApp::CDlgApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance } ///////////////////////////////////////////////////////////////////////////// // The one and only CDlgApp object CDlgApp theApp; ///////////////////////////////////////////////////////////////////////////// // CDlgApp initialization BOOL CDlgApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif HINSTANCE hInst; hInst=LoadLibrary("S_Chinese.dll"); if(hInst!=NULL) AfxSetResourceHandle(hInst); CDlgDlg dlg; m_pMainWnd = &dlg; int nResponse = dlg.DoModal(); if (nResponse == IDOK) { // TODO: Place code here to handle when the dialog is // dismissed with OK } else if (nResponse == IDCANCEL) { // TODO: Place code here to handle when the dialog is // dismissed with Cancel } // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }