程序運(yùn)行效果圖:
VC中實(shí)現(xiàn)多語菜單非常容易,下面以英漢對(duì)照菜單為例說明多語菜單實(shí)現(xiàn)的方法。
一、建立工程項(xiàng)目文件
選擇語言為中文,其余采用默認(rèn),添加中英文切換菜單IDD_MENU_CHANGE
二、用文本形式打開資源文件,找到IDR_MAINFRAME的菜單文本內(nèi)容如下:
IDR_MAINFRAME MENU PRELOAD DISCARDABLE
BEGIN
POPUP "文件(&F)"
BEGIN
MENUITEM "新建(&N)\tCtrl+N", ID_FILE_NEW
MENUITEM "打開(&O)...\tCtrl+O", ID_FILE_OPEN
MENUITEM "保存(&S)\tCtrl+S", ID_FILE_SAVE
MENUITEM "另存為(&A)...", ID_FILE_SAVE_AS
MENUITEM SEPARATOR
MENUITEM "打印(&P)...\tCtrl+P", ID_FILE_PRINT
MENUITEM "打印預(yù)覽(&V)", ID_FILE_PRINT_PREVIEW
MENUITEM "打印設(shè)置(&R)...", ID_FILE_PRINT_SETUP
MENUITEM SEPARATOR
MENUITEM "最近文件", ID_FILE_MRU_FILE1, GRAYED
MENUITEM SEPARATOR
MENUITEM "退出(&X)", ID_APP_EXIT
END
POPUP "編輯(&E)"
BEGIN
MENUITEM "撤消(&U)\tCtrl+Z", ID_EDIT_UNDO
MENUITEM SEPARATOR
MENUITEM "剪切(&T)\tCtrl+X", ID_EDIT_CUT
MENUITEM "復(fù)制(&C)\tCtrl+C", ID_EDIT_COPY
MENUITEM "粘貼(&P)\tCtrl+V", ID_EDIT_PASTE
END
POPUP "查看(&V)"
BEGIN
MENUITEM "工具欄(&T)", ID_VIEW_TOOLBAR
MENUITEM "狀態(tài)欄(&S)", ID_VIEW_STATUS_BAR
MENUITEM "彈出CD", ID_VIEW_FULLSCREEN
MENUITEM "英文", ID_MENU_CHANGE
END
POPUP "幫助(&H)"
BEGIN
MENUITEM "關(guān)于 FullScreen(&A)...", ID_APP_ABOUT
END
END
在此后加入英文菜單資源復(fù)制上述文本,把其中的中文改成英文,并作些改動(dòng)IDR_MAINFRAME_ENGLISH MENU PRELOAD DISCARDABLE
BEGIN
POPUP "File(&F)"
BEGIN
MENUITEM "New(&N)\tCtrl+N", ID_FILE_NEW
MENUITEM "Open(&O)...\tCtrl+O", ID_FILE_OPEN
MENUITEM "Save(&S)\tCtrl+S", ID_FILE_SAVE
MENUITEM "Save as(&A)...", ID_FILE_SAVE_AS
MENUITEM SEPARATOR
MENUITEM "Print(&P)...\tCtrl+P", ID_FILE_PRINT
MENUITEM "Print Preview(&V)", ID_FILE_PRINT_PREVIEW
MENUITEM "Print Setting(&R)...", ID_FILE_PRINT_SETUP
MENUITEM SEPARATOR
MENUITEM "Recent Files", ID_FILE_MRU_FILE1, GRAYED
MENUITEM SEPARATOR
MENUITEM "Exit(&X)", ID_APP_EXIT
END
POPUP "Edit(&E)"
BEGIN
MENUITEM "Undo(&U)\tCtrl+Z", ID_EDIT_UNDO
MENUITEM SEPARATOR
MENUITEM "Cut(&T)\tCtrl+X", ID_EDIT_CUT
MENUITEM "Copy(&C)\tCtrl+C", ID_EDIT_COPY
MENUITEM "Paste(&P)\tCtrl+V", ID_EDIT_PASTE
END
POPUP "View(&V)"
BEGIN
MENUITEM "ToolBar(&T)", ID_VIEW_TOOLBAR
MENUITEM "StatusBar(&S)", ID_VIEW_STATUS_BAR
MENUITEM "Eject CD", ID_VIEW_FULLSCREEN
MENUITEM "Chinese", ID_MENU_CHANGE
END
POPUP "Help(&H)"
BEGIN
MENUITEM "About FullScreen(&A)...", ID_APP_ABOUT
END
END
修改完成后保存資源。
三、在 CMainFrame中加入變量CMenu m_englistmenu 和m_chinesemenu保存中英文菜單資源
加入變量enum Enum{e,c}m_current 保存當(dāng)前菜單
四、由于默認(rèn)為中文菜單在 CMainFrame::CMainFrame()中設(shè)置m_current=c;表示當(dāng)前為中文菜單,在CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)中裝入英文菜單資源 m_englistmenu.LoadMenu("IDR_MAINFRAME_ENGLISH");
HMENU m_hMenu=::GetMenu(this->m_hWnd); // 保存中文菜單
m_chinesemenu.Attach(m_hMenu);
五、加入菜單函數(shù) void CMainFrame::OnMenuChange()
{
if(m_current==e)
{
SetMenu(&m_chinesemenu);
m_current=c;
}
else
{
SetMenu(&m_englistmenu);
m_current=e;
}
}
編譯運(yùn)行程序您就可以獲得一個(gè)具有英漢對(duì)照菜單的應(yīng)用程序。