C++ win32 从资源加载字符串 [英] C++ win32 loading strings from resource

查看:34
本文介绍了C++ win32 从资源加载字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我最近决定将应用程序中的每个字符串都放入一个 STRINGTABLE 中,这样我就可以轻松地翻译成不同的语言.我知道如何使用 LoadString() api,但这涉及到我要加载的每个字符串都有一个不同的变量,如果我的应用程序有 100 个字符串,那就是很多变量.这是最好的方法吗?或者我应该创建一个全局变量作为缓冲区来根据需要加载字符串吗?此外,由于无法知道我的字符串有多大,我应该创建一个足够大的缓冲区来保存我可能拥有的任何字符串,还是有更好的方法来做到这一点?

Alright so I have recently made the decision to put every string in my application into a STRINGTABLE, so I can easily translate to different languages. I know how to use the LoadString() api, but this involves me having a different variable for every string I want to load, and if my application has 100 strings, thats alot of variables. Is this the best way to do this? Or should i create a global variable thats used as a buffer to load the strings as needed? Also since there's no way of knowing how big my string is should I just create a big enough buffer to hold any string i could possible have, or is there a better method of doing this?

是否还根据需要加载字符串对性能不利?有什么办法可以预加载它们吗?

Also is loading the strings as needed bad for performance? Is there any way i can preload them?

RE:好的,我已经尝试创建一个大小为 256 字节的缓冲区并根据需要将字符串加载到该缓冲区中,但我遇到了一个小问题......

RE: Okay i've tried creating a buffer 256 bytes in size and loading the strings into that as needed, though i ran into a little problem...

这是我的代码,显示错误消息,错误是分配内存时出错!"

Here's my code thats displaying an error message, the error is "Error allocating memory!"

LoadString(g_hInst, IDS_ERROR_MEMORY, szBuffer, sizeof(szBuffer)/sizeof(TCHAR));
MessageBox(NULL, szBuffer, TEXT("Error"), MB_OK | MB_ICONERROR);
ExitProcess(1);

我将缓冲区作为全局变量:TCHAR szBuffer[256];

And I have my buffer as a global variable: TCHAR szBuffer[256];

这可行,但是,id 还想将错误"文本存储到字符串表中,并在我想显示错误时加载它,问题是这需要我有 2 个全局变量来加载字符串,然后有些地方我需要一次加载更多.

This works but, id like to also store the "Error" text into the string table and load that when I want to display the error, problem is that would require me to have 2 global variables to load the strings, and there are some places where i need to load even more then that at a time.

有没有比使用多个全局变量更好的解决方案?

Is there a better solution then having multiple global variables?

推荐答案

如果需要,当然可以预加载它们.您只需要创建一个字符串指针数组并将每个字符串加载到该数组中.或者你可以使用哈希映射或类似的东西.

You can certainly preload them if you want. You'd just need to create an array of string pointers and load each string into that array. Or you could use a hash map or something similar.

对性能不利?这取决于.如果您在用户界面中将这些字符串显示为提示,我看不出根据需要加载每个字符串将如何成为性能问题.操作系统无论如何都会进行一些智能缓存,因此您不会为需要显示的每个字符串访问磁盘.另一方面,如果您要在紧密循环中处理这些字符串,那么最好将它们预加载到内存中,这样您就不必一直调用 LoadString.

Bad for performance? It depends. If you're displaying those strings as prompts in a user interface, I don't see how loading each string as it's needed is going to be a performance problem. The operating system is going to do some intelligent caching anyway, so it's not like you'll be hitting the disk for every string that you need to display. On the other hand, if you're going to be working with those strings in a tight loop, then it's probably best to preload them into memory so you don't have to call LoadString all the time.

就缓冲区而言,我总是分配一个缓冲区,该缓冲区与我希望在资源文件中拥有的最大字符串一样大.考虑到用户界面字符串通常非常小,256 字节的缓冲区就足够了.任何比这更大的事情,我要么在启动时预加载到内存中,这样我就可以保留它,或者我编写了一个单独的方法,在加载时分配一个字符串,而不是保留一个缓冲区.

As far as buffers go, I always allocated a buffer that was as large as the largest string I expected to have in my resource file. Considering that user interface strings are typically very small, a 256 byte buffer was more than sufficient. Anything larger than that, I'd either pre-load into memory at startup so I could keep it around, or I wrote a separate method that would allocate a string at load time rather than keeping a buffer around.

附加信息:

与其为字符串定义全局变量,不如考虑编写一个函数来加载资源字符串、制作它的副本并返回该副本.即:

Rather than defining global variables for your strings, consider writing a function that loads a resource string, makes a copy of it, and returns that copy. That is:

char * LoadStringFromResource(uint id)
{
    // szBuffer is a globally pre-defined buffer of some maximum length
    LoadString(ghInst, id, szBuffer, bufferSize);
    // yes, I know that strdup has problems. But you get the idea.
    return strdup(szBuffer);
}

你的代码,然后变成:

char* errMem = LoadStringFromResource(IDS_ERROR_MEMORY);
char* errText = LoadStringFromResource(IDS_ERROR_TEXT);
MessageBox(NULL, errMem, errText, MB_OK | MB_ICONERROR);
free(errMem);
free(errText);

以上是 C 代码,但您可以轻松转换为 C++.特别是,您可能希望修改包装器函数,使其返回一个 C++ 字符串——当它超出范围时会自动释放(使用智能指针或任何现代等效物).

The above is C code, but you can easily convert to C++. In particular, you probably want to to modify the wrapper function so that it returns a C++ string--something that will be automatically deallocated when it goes out of scope (using smart pointers or whatever the modern equivalent is).

这篇关于C++ win32 从资源加载字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆