wxWidgets实战:打开保存文件wxFileDialog使用详解

2023-12-14 17:44:43

打开文件和保存文件都是用wxFileDialog可以实现!!!

1.头文件

#include <wx/filedlg.h>

2.打开文件

wxString wildcards = AmmImportFilesWildcard()
                            + wxS( "|" ) + AmmFilesWildcard()
                            + wxS( "|" ) + AmmxFilesWildcard()
                            + wxS( "|" ) + DecapLibraryXMLFileWildcard();
    wxString pro_dir=wxGetCwd();
    wxFileDialog fileDlg( this, _( "Open Files" ), pro_dir, wxEmptyString,
                      wildcards, wxFD_OPEN | wxFD_FILE_MUST_EXIST );
    if( fileDlg.ShowModal() != wxID_OK )
    {
        return;
    }
    wxString filepath = fileDlg.GetPath();
  • wxFD_OPEN表示打开文件对话框
  • wxFD_FILE_MUST_EXIST表示要求用户选择的文件必须存在,否则无法完成操作。
  • wxGetCwd是一个函数,用于获取当前工作目录的路径

2.1FileDialog的文件过滤wildcards

AmmImportFilesWildcard()

wxString AmmImportFilesWildcard()
{
    return _( "Import Files" )
            + AddFileExtListToFilter( { "amm","ammx","xml","ammp" } );
}

AddFileExtListToFilter函数,

wxString AddFileExtListToFilter( const std::vector<std::string>& aExts )
{
    if( aExts.size() == 0 )
    {
        // The "all files" wildcard is different on different systems
        wxString filter;
        filter << wxS( " (" ) << wxFileSelectorDefaultWildcardStr << wxS( ")|" )
               << wxFileSelectorDefaultWildcardStr;
        return filter;
    }

    wxString files_filter = wxS( " (" );

    // Add extensions to the info message:
    for( const std::string& ext : aExts )
    {
        if( files_filter.length() > 2 )
            files_filter << wxS( "; " );

        files_filter << "*." << ext;
    }

    files_filter << wxS( ")|*." );

    // Add extensions to the filter list, using a formatted string (GTK specific):
    bool first = true;

    for( const std::string& ext : aExts )
    {
        if( !first )
            files_filter << wxS( ";*." );

        first = false;

        files_filter << formatWildcardExt( ext );
    }

    return files_filter;
}
wxString AmmFilesWildcard()
{
    return _( "AMM File" )
            + AddFileExtListToFilter( { "amm" } );
}
wxString AmmxFilesWildcard()
{
    return _( "AMMX File" )
            + AddFileExtListToFilter( { "ammx" } );
}
wxString DecapLibraryXMLFileWildcard()
{
    return _( "Decap Library XML File" )
            + AddFileExtListToFilter( { "xml" } );
}
wxString AmmPrefFileWildcard()
{
    return _( "AMM Pref File" )
            + AddFileExtListToFilter( { "ammp" } );
}

2.2效果:

在这里插入图片描述

3.保存文件

   wxFileName savePath( wxGetCwd() );
    
    if( savePath.HasExt() )
    {
        savePath.SetExt( "csv");
    }          
    else
    {
       savePath.SetName( wxEmptyString );
    }
            
    wxString wildcards = ZInputFilesWildcard();
    
    wxFileDialog dlg( this, _( "Save Map File" ), savePath.GetPath(),
                              savePath.GetFullName(), wildcards, wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
    if( dlg.ShowModal() == wxID_CANCEL )
        return ;
  • wxFD_SAVE表示保存文件对话框
  • wxFD_OVERWRITE_PROMPT表示如果用户尝试覆盖现有文件,会提示用户确认。

3.1ZInputFilesWildcard

wxString ZInputFilesWildcard()
{
    return _( "CSV files" )
            + AddFileExtListToFilter( { "csv",
                                        "CSV" } );
}

AddFileExtListToFilter参见2.1

3.2效果图

在这里插入图片描述

文章来源:https://blog.csdn.net/haimianjie2012/article/details/134999553
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。