Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ OPENFILENAME:通过转换的字符串设置lpstrTitle不会';不工作;输出大量'Ì';s和其他特殊字符_C++_Windows - Fatal编程技术网

C++ OPENFILENAME:通过转换的字符串设置lpstrTitle不会';不工作;输出大量'Ì';s和其他特殊字符

C++ OPENFILENAME:通过转换的字符串设置lpstrTitle不会';不工作;输出大量'Ì';s和其他特殊字符,c++,windows,C++,Windows,我目前正在创建一个类,以便更方便地使用GetOpenFileName。现在,我遇到了一个问题: 我包含了一个字符串作为构造函数参数设置为窗口标题。但当使用它时,窗口只输出许多特殊字符 这是我的代码: OpenFileDialog(HWND hwndOwner = NULL, std::string WindowTitle = "", std::string FilterDescription = "Text Files", std::string FilterExtension = "txt",

我目前正在创建一个类,以便更方便地使用GetOpenFileName。现在,我遇到了一个问题:

我包含了一个字符串作为构造函数参数设置为窗口标题。但当使用它时,窗口只输出许多特殊字符

这是我的代码:

OpenFileDialog(HWND hwndOwner = NULL, std::string WindowTitle = "", std::string FilterDescription = "Text Files", std::string FilterExtension = "txt", bool ShowAnyFilesOption = true) {
    ZeroMemory(&filename, sizeof(filename));
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = hwndOwner;
    setFilter(FilterDescription, FilterExtension, ShowAnyFilesOption);
    ofn.lpstrFile = filename;
    ofn.nMaxFile = MAX_PATH;
    ofn.lpstrTitle = WindowTitle.c_str();
    ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
}


bool setFilter(std::string FormatDescription, std::string FileExtension, bool ShowAnyFilesOption = true) {
    if (FileExtension != "") {
#define NULLstr sFilter.push_back('\0')

        sFilter = FormatDescription + " (*." + FileExtension + ")";
        NULLstr;
        sFilter.append("*." + FileExtension);
        NULLstr;

        if (ShowAnyFilesOption) {
            sFilter.append("Any Files (*.*)");
            NULLstr;
            sFilter.append("*.*");
            NULLstr;
        }
        ofn.lpstrFilter = sFilter.c_str();
        return true;
#undef NULLstr
    }

    else // File extension empty
                return false;
}

如果你能帮我解决这个问题,我将非常高兴。老实说,我不太擅长使用WinAPI,因为在OpenFileDialog构造函数中没有发生GetOpenFileName调用,所以您希望将字符串复制到一个更持久的位置(例如OpenFileDialog中的类字段)-作为参数传递的std::string稍后可能会被释放并用于其他用途,从而导致您的c_str指向不相关的/垃圾数据。

我认为这行:
ofn.lpstrTitle=WindowTitle.c_str()
(以及
setFilter
函数中的类似函数)将“悬挂指针”指定为标题。(当传递到的函数返回时,基础
字符串
将超出范围。)
Ì
为0xCC,其中为的值。当您看到一系列特殊值的字节时,请将它们转换为十六进制并查看