szhandrew
2018-07-11 22:52
采纳率: 25%
浏览 2.3k

请教如何用C++,弹出选择文件窗口,并在控制台打印已选文件路径

如题,现在使用VC2017,听说可以使用OpenFileDialog,刚入门小白,请问具体需要怎么用,感谢各位大神

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • blownewbee 2018-07-11 23:29
    已采纳

    这是原生C++的做法:

    // Q694481_Win32.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <windows.h>
    #include <Commdlg.h>
    #include <stdio.h>
    
    OPENFILENAME ofn;
    char szFile[300];
    
    int main()
    {
        ZeroMemory(&ofn, sizeof(ofn));
        ofn.lStructSize = sizeof(ofn);
        ofn.hwndOwner = NULL;
        ofn.lpstrFile = (LPWSTR)szFile;
        ofn.lpstrFile[0] = '\0';
        ofn.nMaxFile = sizeof(szFile);
        ofn.lpstrFilter = L"All\0*.*\0Text\0*.TXT\0";
        ofn.nFilterIndex = 1;
        ofn.lpstrFileTitle = NULL;
        ofn.nMaxFileTitle = 0;
        ofn.lpstrInitialDir = NULL;
        ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
        if (GetOpenFileName(&ofn))
        {
            wprintf(L"%s\n", ofn.lpstrFile);
        }
        else
        {
            printf("user cancelled\n");
        }
        return 0;
    }
    
    
    点赞 4 评论
  • blownewbee 2018-07-11 23:15

    OpenFileDialog是.net的库,你必须创建一个C++ CLR的程序

     // Q694481.cpp : main project file.
    
    #include "stdafx.h"
    
    using namespace System;
    using namespace System::Windows::Forms;
    
    [STAThreadAttribute]
    int main(array<System::String ^> ^args)
    {
        OpenFileDialog^ openFileDialog = gcnew OpenFileDialog;
        if (openFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK)
        {
            Console::WriteLine(openFileDialog->FileName);
        }
        Console::ReadLine();
    }
    
    
    点赞 评论
  • blownewbee 2018-07-11 23:16

    图片说明

    点赞 评论
  • blownewbee 2018-07-11 23:30

    图片说明

    点赞 评论

相关推荐 更多相似问题