利用CString::FormatMessage格式化资源ID

Formated the string resource identifier that contains the unformatted message text by CString::FormatMessage(UINT nFormatID, [, argument]…);

项目需要通过MFC resource 的String Table进行多语言翻译操作, 但在代码中时常会遇到这种多数据拼接的消息

CString message;
//Sheldon于2019年4月14日申请2000元优惠
message = m_user + L"于 " + m_year + L"年" + m_month + L"月" + m_day + L"日申请 " + m_ammount_money + L"元优惠"; 

如果一个字一个字放到String Table中
①是用起来很难受;
②是切换到其他语种翻译起来就比较怪 eg…“sheldon 2018 year 05 month 12 day request 3000 yuan Discount”
所以就想将消息按一整条放到 String Table中, 然后进行数据格式化即可

ID标题
IDS_STRING102103%1于%2!d!年%3!d!月%4!d!日申请%5!d!元优惠
ID标题
IDS_STRING102103%1 applied for a $ %2!d! discount on %3!d! %4!d!, %5!d!

这样在代码中只需要

CString message;
message.FormatMessage(IDS_STRING102, L"sheldon", 2019, 2, 3, 20000);
CDHtmlDialog::SetElementText(L"DialogContent", str.GetBuffer()); //设置ID为DialogContent的HTML元素text为str
str.ReleaseBuffer();

需要重点看一下字符串格式化部分,CString::FormatMessage格式化写法同 ::FormatMessage
FormatMessage Remarks 部分,它要求格式化部分为 : %n!format string!
n 定义参数序号, 取值范围为 [1, 99]
format string 中一般只需参数类型, 也可以指明参数的width 和 precision, 默认是 !s! 字符串类型

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值