好的,我已经处理了两天了,我找不到解决方案。
问题:我正在尝试使用 Winapi为文件选择对话框设置过滤器。我正在使用GetOpenFileName
函数来做到这一点。此函数使用结构来设置选项,例如文件扩展名过滤器。这个结构的成员调用lpstrFilter
需要一定的字符串格式。我完全按照 Winapi 的指示设置该字符串,但由于某种原因,该字符串的值发生了变化。
我有这个静态 const char *:
//This contains string "JPG"
static const char * extensionFilter = v->trabajo10.C_JMV_SelectFile_FileExtension7.GetString();
//This forms a filter string which applies to OPENFILENAME structure.
string sFilter;
sFilter.append("Format: ");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
sFilter.append("*.");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
const char * filter = sFilter.c_str();
ofn.lpstrFilter = filter; //This sets: --> Format: JPG\0*.JPG\0
//This opens the file selection dialog
if (GetOpenFileName(&ofn)==TRUE){
...
文件选择对话框看起来正确,如下所示:
笑话来了,我这样修改代码:
//This contains string "JPG"
static const char * extensionFilter = v->trabajo10.C_JMV_SelectFile_FileExtension7.GetString();
if(1){
//This forms a filter string which applies to OPENFILENAME structure.
string sFilter;
sFilter.append("Format: ");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
sFilter.append("*.");
sFilter.append(extensionFilter);
sFilter.push_back('\0');
const char * filter = sFilter.c_str();
ofn.lpstrFilter = filter; //This sets: --> Format: JPG\0*.JPG\0
}
//This opens the file selection dialog
if (GetOpenFileName(&ofn)==TRUE){
...
这就是结果,问题:
过滤器字符串被修改???