[MFC] Multilingual Toolbar

00. Table of Contents

01. Case overview

A good software is to be sold all over the world. If the software is in English, it will cause inconvenience to users who do not know English. Therefore, in the software development stage, prepare for multiple languages ​​and design corresponding resources for all languages. , Visual C++ created applications can dynamically change these resources, and the example realizes the dynamic change of toolbar resources. Results as shown below.

[External link image transfer failed. The origin site may have an anti-leech link mechanism. It is recommended to save the image and upload it directly (img-HnBjsv3i-1615024814211)(assets/image-20210306175955432.png)]

02. Development environment

System environment: Windows 10

Development environment: Visual Studio 2019

03. Key Technology

To realize the multi-language toolbar, you need to design the toolbar for each language separately. As shown in the figure below, the Chinese toolbar and the English toolbar are designed separately.
Insert picture description here

Not only should the icon of the toolbar button be designed in Chinese and English, but also the language attribute of the toolbar needs to be modified. The properties of the toolbar are shown in the figure below.

04. Programming

(1) Create a new dialog-based application.

(2) Add Chinese toolbar resource IDR_TOOLCHI and English toolbar resource IDR_TOOLEN to the project.
Insert picture description here

(3) The realization function OnEnglish of the button "english" realizes the switching of the English toolbar, the code is as follows.


void CMy6DialogDlg::OnBnClickedButtonchi()
{
    
    
	// TODO: 在此添加控件通知处理程序代码
	//加载中文工具栏
	m_ToolBar.LoadToolBar(IDB_TOOLCHI);

	RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
}


void CMy6DialogDlg::OnBnClickedButtoneng()
{
    
    
	// TODO: 在此添加控件通知处理程序代码
	m_ToolBar.LoadToolBar(IDB_TOOLEN);

	RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
}


BOOL CMy6DialogDlg::OnEraseBkgnd(CDC* pDC)
{
    
    
	 //TODO: 在此添加消息处理程序代码和/或调用默认值
	CRect rect;
	GetWindowRect(&rect);
	CDC memDC;
	CBitmap bitmap;
	CBitmap* bmp = NULL;
	bitmap.LoadBitmap(IDB_BITMAP1);//装载背景位图
	memDC.CreateCompatibleDC(pDC);
	bmp = memDC.SelectObject(&bitmap);
	pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
	if (bmp) memDC.SelectObject(bmp);

	return TRUE;
}

05. Secret Method of Mind

Resources for Visual C++

Using Visual C++ to develop applications, it is necessary to understand the concept of resources. In Visual C++, icons, bitmaps, dialog boxes, toolbars, and strings are all called resources. Resources have language attributes, and applications can call the same Different resources for language attributes.

06. Download the source code

Download: 【MFC】Multilingual Toolbar.rar

07. Appendix

Reference: "Visual C++ From Entry to Proficiency (Project Case Edition)"

Guess you like

Origin blog.csdn.net/dengjin20104042056/article/details/114449959