想在程序运行时单击"打开"按钮,将弹出"打开"对话框,怎么办?
"打开"对话框就是例如vc界面上我们点击工具栏上的"打开"图标跳出来的那个对话框,从中我们可以选择我们所需的文件.

解决方案 »

  1.   

    CFileDialog FileDlg(TRUE,"*.ico",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    "Icon files(*.ico)|*.ico|All files (*.*)|*.*||");

    FileDlg.m_ofn.lpstrInitialDir="C:\\winnt";
    if(FileDlg.DoModal()==IDOK)
    {
       CString aa=FileDlg.GetPathName();
    }
      

  2.   

    楼上的 CFileDialog FileDlg(TRUE,"*.ico",NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    "Icon files(*.ico)|*.ico|All files (*.*)|*.*||");
    第一个参数就是控制显示“另存”(false)或"打开(true)对话框的
      

  3.   

    同意两位楼上
        FileDlg.m_ofn.lpstrInitialDir="C:\\winnt";//初始化默认路径
        "Icon files(*.ico)|*.ico|All files (*.*)|*.*||"//可以设置你所想要显示的文件类型.
      

  4.   

    谢谢各位了!
    OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT是什么意思?
      

  5.   

    CString       filename;
    static  char  BASED_CODE  fliter[]="all files(*.*)|*.*||";
    CFileDialog   dc(TRUE, "txt", NULL, NULL, fliter);dc.m_ofn.lpstrTitle="files open";  // 打开框的名字
    if(dc.DoModal()==IDOK)
    {
         filename=dc.GetPathName();
    }
      

  6.   

    看msdn
    2是用來設置默認的擴展名稱.3是在你打開後,出現在框框里的指定文件名;
    OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT
    看OPENFILENAME結構
      

  7.   

    OFN_ALLOWMULTISELECT Specifies that the File Name list box allows multiple selections. If you also set the OFN_EXPLORER flag, the dialog box uses the Explorer-style user interface; otherwise, it uses the old-style user interface. 
    If the user selects more than one file, the lpstrFile buffer returns the path to the current directory followed by the file names of the selected files. The nFileOffset member is the offset, in bytes or characters, to the first file name, and the nFileExtension member is not used. For Explorer-style dialog boxes, the directory and file name strings are NULL separated, with an extra NULL character after the last file name. This format enables the Explorer-style dialog boxes to return long file names that include spaces. For old-style dialog boxes, the directory and file name strings are separated by spaces and the function uses short file names for file names with spaces. You can use the FindFirstFile function to convert between long and short file names. If you specify a custom template for an old-style dialog box, the definition of the File Name list box must contain the LBS_EXTENDEDSEL value. 
     
      

  8.   

    OFN_CREATEPROMPT If the user specifies a file that does not exist, this flag causes the dialog box to prompt the user for permission to create the file. If the user chooses to create the file, the dialog box closes and the function returns the specified name; otherwise, the dialog box remains open. If you use this flag with the OFN_ALLOWMULTISELECT flag, the dialog box allows the user to specify only one nonexistent file.  
    OFN_DONTADDTORECENT Windows 2000/XP: Prevents the system from adding a link to the selected file in the file system directory that contains the user's most recently used documents. To retrieve the location of this directory, call the SHGetSpecialFolderLocation function with the CSIDL_RECENT flag.  
    OFN_ENABLEHOOK Enables the hook function specified in the lpfnHook member. OFN_ENABLEINCLUDENOTIFY Windows 2000/XP: Causes the dialog box to send CDN_INCLUDEITEM notification messages to your OFNHookProc hook procedure when the user opens a folder. The dialog box sends a notification for each item in the newly opened folder. These messages enable you to control which items the dialog box displays in the folder's item list.  OFN_ENABLESIZING Windows 2000/XP, Windows 98/Me: Enables the Explorer-style dialog box to be resized using either the mouse or the keyboard. By default, the Explorer-style Open and Save As dialog boxes allow the dialog box to be resized regardless of whether this flag is set. This flag is necessary only if you provide a hook procedure or custom template. The old-style dialog box does not permit resizing. 
      

  9.   


    OFN_ENABLETEMPLATE Indicates that the lpTemplateName member is a pointer to the name of a dialog template resource in the module identified by the hInstance member. 
    If the OFN_EXPLORER flag is set, the system uses the specified template to create a dialog box that is a child of the default Explorer-style dialog box. If the OFN_EXPLORER flag is not set, the system uses the template to create an old-style dialog box that replaces the default dialog box.
     
    OFN_ENABLETEMPLATEHANDLE Indicates that the hInstance member identifies a data block that contains a preloaded dialog box template. The system ignores the lpTemplateName if this flag is specified. 
    If the OFN_EXPLORER flag is set, the system uses the specified template to create a dialog box that is a child of the default Explorer-style dialog box. If the OFN_EXPLORER flag is not set, the system uses the template to create an old-style dialog box that replaces the default dialog box.
     
    OFN_EXPLORER Indicates that any customizations made to the Open or Save As dialog box use the new Explorer-style customization methods. For more information, see Explorer-Style Hook Procedures and Explorer-Style Custom Templates. 
    By default, the Open and Save As dialog boxes use the Explorer-style user interface regardless of whether this flag is set. This flag is necessary only if you provide a hook procedure or custom template, or set the OFN_ALLOWMULTISELECT flag. If you want the old-style user interface, omit the OFN_EXPLORER flag and provide a replacement old-style template or hook procedure. If you want the old style but do not need a custom template or hook procedure, simply provide a hook procedure that always returns FALSE.
     
    OFN_EXTENSIONDIFFERENT Specifies that the user typed a file name extension that differs from the extension specified by lpstrDefExt. The function does not use this flag if lpstrDefExt is NULL. 
    OFN_FILEMUSTEXIST Specifies that the user can type only names of existing files in the File Name entry field. If this flag is specified and the user enters an invalid name, the dialog box procedure displays a warning in a message box. If this flag is specified, the OFN_PATHMUSTEXIST flag is also used. 
    OFN_FORCESHOWHIDDEN Windows 2000/XP: Forces the showing of system and hidden files, thus overriding the user setting to show or not show hidden files. However, a file that is ed both system and hidden is not shown. 
    OFN_HIDEREADONLY Hides the Read Only check box. 
    OFN_LONGNAMES For old-style dialog boxes, this flag causes the dialog box to use long file names. If this flag is not specified, or if the OFN_ALLOWMULTISELECT flag is also set, old-style dialog boxes use short file names (8.3 format) for file names with spaces. 
    Explorer-style dialog boxes ignore this flag and always display long file names.