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

查看:47
本文介绍了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天全站免登陆