问题描述
什么?
我有一个从资源 DLL 加载的 DLGTEMPLATE,如何以编程方式在运行时更改分配给控件的字符串?
I have a DLGTEMPLATE loaded from a resource DLL, how can I change the strings assigned to the controls at runtime programmatically?
我希望能够在创建对话框之前执行此操作,这样我就可以知道显示的字符串来自资源 DLL,而不是对话框初始化时对 SetWindowText 的调用.
I want to be able to do this before the dialog is created, such that I can tell that the strings on display came from the resource DLL, and not from calls to SetWindowText when the dialog is initialized.
Google 已经找到了在代码中创建 DLGTEMPLATE 的示例,或者摆弄简单的样式位,但没有在内存中编辑字符串.
Google has found examples of creating DLGTEMPLATE in code, or twiddling simple style bits but nothing on editing the strings in memory.
如何?
我是通过挂钩对话框/属性表创建 API 来实现的.这使我可以在创建实际对话框之前以及在具有 HWND 之前访问 DLGTEMPLATE.
I am doing this by hooking the Dialog/Property Sheet creation API's. Which gives me access to the DLGTEMPLATE before the actual dialog is created and before it has a HWND.
为什么?
我希望能够进行运行时本地化和本地化测试.我已经为加载字符串(包括 MFC 7.0 包装器)、菜单和加速器表实现了这个,但我正在努力处理对话框/属性表的创建.
I want to be able to do runtime localization, and localization testing. I already have this implemented for loading string (including the MFC 7.0 wrapper), menus and accelerator tables, but I am struggling to handle dialog/property sheet creation.
代码示例将是完美的答案,理想情况下是一个环绕 DLGTEMPLATE 的类,如果我制定出自己的解决方案,我会发布它.
Code examples would be the perfect answer, ideally a class to wrap around the DLGTEMPLATE, if I work out my own solution I will post it.
推荐答案
您无法编辑内存中的字符串.DLGTEMPLATE 结构是资源 dll 的相关字节的直接文件映射.那是只读的.
You can't edit the strings in memory. The DLGTEMPLATE structure is a direct file mapping of the relevent bytes of the resource dll. Thats read only.
您将需要处理整个 DLGTEMPLATE 结构并用更改后的长度字符串写出一个新结构.
You are going to need to process the entire DLGTEMPLATE structure and write out a new one with the altered length strings.
坦率地说,与构建 DLGTEMPLATE 编写器相比,通过与控件交互来挂钩 WM_INITDIALOG 并更改字符串会更容易.因为周围的人不多.除非您有额外的要求将更改的对话框资源作为原始 .res 文件实际保存到磁盘(或尝试就地修改 .dll),否则我真的建议您避免这种方法.
It will frankly be easier to just hook the WM_INITDIALOG and alter the strings by interacting with the controls than building a DLGTEMPLATE writer. Because there arn't a lot of those around. Unless you have an additional requirement to actually save altered dialog resources to disk as raw .res files (or attempt to modify the .dll inplace) Id really recommend you avoid this approach.
你说你已经在为加速器表和菜单字符串做这件事——如果你能保证修补的字符串会更短,那么只需制作 DLGTEMPLATE 结构的二进制副本,并编写非平凡的扫描代码必须找到每个字符串,以便您可以修补副本.
You say you are already doing this for accellerator tables and menu strings - if you can guarantee that the patched in strings are going to be shorter, then just make a binary copy of the DLGTEMPLATE struct, and write the non trivial scanning code necessary to find each string so you can patch the copy in place.
这篇关于如何以编程方式操作 DLGTEMPLATE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!