• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            CFileDialog文件選擇對話框的使用

            CFileDialog文件選擇對話框的使用:首先構造一個對象并提供相應的參數,構造函數原型如下: CFileDialog::CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL );參數意義如下:

            • bOpenFileDialog 為TRUE則顯示打開對話框,為FALSE則顯示保存對話文件對話框。
            • lpszDefExt 指定默認的文件擴展名。
            • lpszFileName 指定默認的文件名。
            • dwFlags 指明一些特定風格。
            • lpszFilter 是最重要的一個參數,它指明可供選擇的文件類型和相應的擴展名。參數格式如: "Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";文件類型說明和擴展名間用 | 分隔,同種類型文件的擴展名間可以用 ; 分割,每種文件類型間用 | 分隔,末尾用 || 指明。
            • pParentWnd 為父窗口指針。
            創建文件對話框可以使用DoModal(),在返回后可以利用下面的函數得到用戶選擇:
            • CString CFileDialog::GetPathName( ) 得到完整的文件名,包括目錄名和擴展名如:c:"test"test1.txt
            • CString CFileDialog::GetFileName( ) 得到完整的文件名,包括擴展名如:test1.txt
            • CString CFileDialog::GetExtName( ) 得到完整的文件擴展名,如:txt
            • CString CFileDialog::GetFileTitle ( ) 得到完整的文件名,不包括目錄名和擴展名如:test1
            • POSITION CFileDialog::GetStartPosition( ) 對于選擇了多個文件的情況得到第一個文件位置。
            • CString CFileDialog::GetNextPathName( POSITION& pos ) 對于選擇了多個文件的情況得到下一個文件位置,并同時返回當前文件名。但必須已經調用過POSITION CFileDialog::GetStartPosition( )來得到最初的POSITION變量。

             實例:

            char szFliter[] = "log Fliex(*.log)|*.log||";
             CFileDialog dlg(TRUE,NULL,
            "", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT |
                 OFN_ALLOWMULTISELECT, szFliter);
             m_itrVecStrPathSave 
            = m_vecStrPathSave.begin();
             
            int iNum = 0;
             
             
            for(int iCount = 0; m_vecStrPathSave.size() > iCount; iCount++)
             
            {
              m_vecStrPathSave.erase(m_vecStrPathSave.begin(), m_vecStrPathSave.end());
              
            break;
             }

               
             
            if(IDOK == dlg.DoModal())
             
            {
              POSITION mPos 
            = dlg.GetStartPosition();
              CFileStatus status;
              itrVecStrSort 
            = vecStrSort.begin();
              itrVecStrSortName 
            = vecStrSortName.begin();
              CMainFrame 
            * pMainFrame;
              CLogSwitchToolView 
            *pView;
              pMainFrame 
            = (CMainFrame *)AfxGetApp()->m_pMainWnd;
              pView 
            = (CLogSwitchToolView*)pMainFrame->GetActiveView();
              pView
            ->m_bScrollSet = TRUE;

              
            while(NULL != mPos)
              
            {
               m_vecStrPathSave.push_back(dlg.GetNextPathName(mPos));  
               iNum
            ++;  
              }

              iNum
            --;  
              CStdioFile sdfFile;
              CFileException en;
              CString strLine;

              
            if(!sdfFile.Open(m_vecStrPathSave[0], CFile::modeRead, &en))
              
            {
               en.ReportError();
               
            return;
              }

              sdfFile.Close();

              
            while(0 < iNum)
              
            {
               itrVecStrSort 
            = vecStrSort.begin();
               itrVecStrSortName 
            = vecStrSortName.begin();
               OnPateDel(m_vecStrPathSave[iNum]);
               iNum
            --;   
              }

              m_strUse 
            = m_vecStrPathSave[0];
              m_iMain 
            = 1;   
              pView
            ->m_iCol = 1;  
              m_bSearchDiff 
            = FALSE;
              m_iSearchDiffCount
            = 0;
              m_bSearchSame 
            = FALSE;
              m_bExtract 
            = FALSE;
              m_bFileOpen 
            = TRUE;
              
              
            for(int iCount = 0; pView->m_vecStrText.size() > iCount; iCount++)
              
            {
               pView
            ->m_vecStrText.erase(pView->m_vecStrText.begin(), pView->m_vecStrText.end());
               
            break;
              }


              
            for(int iCountTwo = 0; pView->m_vecNum.size() > iCountTwo; iCountTwo++)
              
            {
               pView
            ->m_vecNum.erase(pView->m_vecNum.begin(), pView->m_vecNum.end());
               
            break;
              }
              
              m_compDlgBar.m_comboBoxFile.ShowWindow(SW_SHOW);  
              m_compDlgBar.m_comboBoxFile.ResetContent();
              m_compDlgBar.m_comboBoxFileTwo.ResetContent();
              
            int iCountThree;
              iCountThree 
            = 0;
              
              OnPateDel(m_vecStrPathSave[
            0]);

              
            for(int iCountFour = vecStrSortName.size() - 10 <= iCountFour; iCountFour--)
              
            {
               m_compDlgBar.m_comboBoxFile.InsertString(iCountThree,vecStrSortName[iCountFour]);
               iCountThree
            ++;
              }

              m_compDlgBar.m_comboBoxFile.SetCurSel(
            0);
              iCountThree 
            = 0;
            /////////////////////////////////////   
              for(int iCountFive = vecStrSortName.size() - 10 <= iCountFive; iCountFive--)
              
            {
               m_compDlgBar.m_comboBoxFileTwo.InsertString(iCountThree,vecStrSortName[iCountFive]);
               iCountThree
            ++;
              }

              m_compDlgBar.m_comboBoxFileTwo.SetCurSel(
            1);
              OnTextDel();
              Invalidate();
              g_intPoint 
            = 1;
             }

             [一些小心得]

            例: CFileDialog GetFile(TRUE,NULL,NULL,OFN_FILEMUSTEXIST,"Microsoft Excel(*.xls)|*.xls|All Files(*.*)|*.*||");

            CFileDialog GetFile(打開文件對話框(TRUE),擴展名(NULL),文件名(NULL),風格-文件必須存在(OFN_FILEMUSTEXIST),查 看文件類型-EXCEL文件,所有文件(Microsoft Excel(*.xls)|*.xls|All Files(*.*)|*.*||);

            風格的宏定義

            #define OFN_READONLY                 0x00000001 #define OFN_OVERWRITEPROMPT          0x00000002 #define OFN_HIDEREADONLY             0x00000004 #define OFN_NOCHANGEDIR              0x00000008 #define OFN_SHOWHELP                 0x00000010 #define OFN_ENABLEHOOK               0x00000020 #define OFN_ENABLETEMPLATE           0x00000040 #define OFN_ENABLETEMPLATEHANDLE     0x00000080 #define OFN_NOVALIDATE               0x00000100 #define OFN_ALLOWMULTISELECT         0x00000200 #define OFN_EXTENSIONDIFFERENT       0x00000400 #define OFN_PATHMUSTEXIST            0x00000800 #define OFN_FILEMUSTEXIST            0x00001000 #define OFN_CREATEPROMPT             0x00002000 #define OFN_SHAREAWARE               0x00004000 #define OFN_NOREADONLYRETURN         0x00008000 #define OFN_NOTESTFILECREATE         0x00010000 #define OFN_NONETWORKBUTTON          0x00020000 #define OFN_NOLONGNAMES              0x00040000     // force no long names for 4.x modules #if(WINVER >= 0x0400) #define OFN_EXPLORER                 0x00080000     // new look commdlg #define OFN_NODEREFERENCELINKS       0x00100000 #define OFN_LONGNAMES                0x00200000     // force long names for 3.x modules #define OFN_ENABLEINCLUDENOTIFY      0x00400000     // send include message to callback #define OFN_ENABLESIZING             0x00800000 #endif /* WINVER >= 0x0400 */ #if (_WIN32_WINNT >= 0x0500) #define OFN_DONTADDTORECENT          0x02000000 #define OFN_FORCESHOWHIDDEN          0x10000000    // Show All files including System and hidden files #endif // (_WIN32_WINNT >= 0x0500)

            這個就不解釋了,應該都能看懂吧

            需要注意的是,用了CFileDialog之后,會把程序的當前路徑設置成選中文件的路徑 所以,如果程序里有用到IO訪問或者數據庫訪問之類的本地操作時,需要注意你的當前路徑 用相對路徑的話就不是原來你的程序路徑了,切記!

             

            CFileDialog文件選擇對話框的使用

            CFileDialog文件選擇對話框的使用: 首先構造一個對象并提供相應的參數,構造函數原型如下: CFileDialog::CFileDialog( BOOL bOpenFileDialog, LPCTSTR lpszDefExt = NULL, LPCTSTR lpszFileName = NULL, DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, LPCTSTR lpszFilter = NULL, CWnd* pParentWnd = NULL );參數意義如下:

            • bOpenFileDialog 為TRUE則顯示打開對話框,為FALSE則顯示保存對話文件對話框。
            • lpszDefExt 指定默認的文件擴展名。
            • lpszFileName 指定默認的文件名。
            • dwFlags 指明一些特定風格。
            • lpszFilter 是最重要的一個參數,它指明可供選擇的文件類型和相應的擴展名。參數格式如: "Chart Files (*.xlc)|*.xlc|Worksheet Files (*.xls)|*.xls|Data Files (*.xlc;*.xls)|*.xlc; *.xls|All Files (*.*)|*.*||";文件類型說明和擴展名間用 | 分隔,同種類型文件的擴展名間可以用 ; 分割,每種文件類型間用 | 分隔,末尾用 || 指明。
            • pParentWnd 為父窗口指針。
            創建文件對話框可以使用DoModal(),在返回后可以利用下面的函數得到用戶選擇:
            • CString CFileDialog::GetPathName( ) 得到完整的文件名,包括目錄名和擴展名如:c:"test"test1.txt
            • CString CFileDialog::GetFileName( ) 得到完整的文件名,包括擴展名如:test1.txt
            • CString CFileDialog::GetExtName( ) 得到完整的文件擴展名,如:txt
            • CString CFileDialog::GetFileTitle ( ) 得到完整的文件名,不包括目錄名和擴展名如:test1
            • POSITION CFileDialog::GetStartPosition( ) 對于選擇了多個文件的情況得到第一個文件位置。
            • CString CFileDialog::GetNextPathName( POSITION& pos ) 對于選擇了多個文件的情況得到下一個文件位置,并同時返回當前文件名。但必須已經調用過POSITION CFileDialog::GetStartPosition( )來得到最初的POSITION變量。

            CColorDialog顏色選擇對話框的使用: 首先通過CColorDialog::CColorDialog( COLORREF clrInit = 0, DWORD dwFlags = 0, CWnd* pParentWnd = NULL )構造一個對象,其中clrInit為初始顏色。通過調用DoModal()創建對話框,在返回后調用COLORREF CColorDialog::GetColor( )得到用戶選擇的顏色值。

            CFontDialog字體選擇對話框的使用: 首先構造一個對象并提供相應的參數,構造函數原型如下: CFontDialog::CFontDialog( LPLOGFONT lplfInitial = NULL, DWORD dwFlags = CF_EFFECTS | CF_SCREENFONTS, CDC* pdcPrinter = NULL, CWnd* pParentWnd = NULL );構造一個對象,其中參數lplfInitial指向一個LOGFONG結構.如果該參數設置為NULL表示不設置初始字體。pdcPrinter指向 一個代表打印機設備環境的DC對象,若設置該參數則選擇的字體就為打印機所用。pParentWnd用于指定父窗口。通過調用DoModal()創建對話 框,在返回后通過調用以下函數來得到用戶選擇:

            • void CFontDialog::GetCurrentFont( LPLOGFONT lplf );用來獲得所選字體的屬性。該函數有一個參數,該參數是指向LOGFONT結構的指針,函數將所選字體的各種屬性寫入這個LOGFONT結構中。
            • CString CFontDialog::GetFaceName( ) 得到所選字體名字。
            • int CFontDialog::GetSize( ) 得到所選字體的尺寸(以10個象素為單位)。
            • COLORREF CFontDialog::GetColor( ) 得到所選字體的顏色。
            • BOOL CFontDialog::IsStrikeOut( ) BOOL CFontDialog::IsUnderline( ) BOOL CFontDialog::IsBold( ) BOOL CFontDialog::IsItalic( ) 得到所選字體的其他屬性,是否有刪除線,是否有下劃線,是否為粗體,是否為斜體。

             

            附   dwFlags

            Flags
            A set of bit flags you can use to initialize the dialog box. When the dialog box returns, it sets these flags to indicate the user's input. This member can be a combination of the following flags.
            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.
            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.
            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 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. This flag can be used in an Open dialog box. It cannot be used with a Save As dialog box.
            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 marked 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.
            OFN_NOCHANGEDIR
            Restores the current directory to its original value if the user changed the directory while searching for files.
            Windows NT 4.0/2000/XP: This flag is ineffective for GetOpenFileName.
            OFN_NODEREFERENCELINKS
            Directs the dialog box to return the path and file name of the selected shortcut (.LNK) file. If this value is not specified, the dialog box returns the path and file name of the file referenced by the shortcut.
            OFN_NOLONGNAMES
            For old-style dialog boxes, this flag causes the dialog box to use short file names (8.3 format). Explorer-style dialog boxes ignore this flag and always display long file names.
            OFN_NONETWORKBUTTON
            Hides and disables the Network button.
            OFN_NOREADONLYRETURN
            Specifies that the returned file does not have the Read Only check box selected and is not in a write-protected directory.
            OFN_NOTESTFILECREATE
            Specifies that the file is not created before the dialog box is closed. This flag should be specified if the application saves the file on a create-nonmodify network share. When an application specifies this flag, the library does not check for write protection, a full disk, an open drive door, or network protection. Applications using this flag must perform file operations carefully, because a file cannot be reopened once it is closed.
            OFN_NOVALIDATE
            Specifies that the common dialog boxes allow invalid characters in the returned file name. Typically, the calling application uses a hook procedure that checks the file name by using the FILEOKSTRING message. If the text box in the edit control is empty or contains nothing but spaces, the lists of files and directories are updated. If the text box in the edit control contains anything else, nFileOffset and nFileExtension are set to values generated by parsing the text. No default extension is added to the text, nor is text copied to the buffer specified by lpstrFileTitle. If the value specified by nFileOffset is less than zero, the file name is invalid. Otherwise, the file name is valid, and nFileExtension and nFileOffset can be used as if the OFN_NOVALIDATE flag had not been specified.
            OFN_OVERWRITEPROMPT
            Causes the Save As dialog box to generate a message box if the selected file already exists. The user must confirm whether to overwrite the file.
            OFN_PATHMUSTEXIST
            Specifies that the user can type only valid paths and file names. If this flag is used and the user types an invalid path and file name in the File Name entry field, the dialog box function displays a warning in a message box.
            OFN_READONLY
            Causes the Read Only check box to be selected initially when the dialog box is created. This flag indicates the state of the Read Only check box when the dialog box is closed.
            OFN_SHAREAWARE
            Specifies that if a call to the OpenFile function fails because of a network sharing violation, the error is ignored and the dialog box returns the selected file name. If this flag is not set, the dialog box notifies your hook procedure when a network sharing violation occurs for the file name specified by the user. If you set the OFN_EXPLORER flag, the dialog box sends the CDN_SHAREVIOLATION message to the hook procedure. If you do not set OFN_EXPLORER, the dialog box sends the SHAREVISTRING registered message to the hook procedure.
            OFN_SHOWHELP
            Causes the dialog box to display the Help button. The hwndOwner member must specify the window to receive the HELPMSGSTRING registered messages that the dialog box sends when the user clicks the Help button. An Explorer-style dialog box sends a CDN_HELP notification message to your hook procedure when the user clicks the Help button.
            OFN_USESHELLITEM
            Do not use.

            posted on 2010-02-22 11:58 wrh 閱讀(2700) 評論(0)  編輯 收藏 引用

            導航

            <2008年4月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            統計

            常用鏈接

            留言簿(19)

            隨筆檔案

            文章檔案

            收藏夾

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            婷婷综合久久狠狠色99h| 国产激情久久久久久熟女老人| 亚洲AV无一区二区三区久久| AV无码久久久久不卡蜜桃 | 99久久99久久精品国产| 国产精自产拍久久久久久蜜| 欧美精品九九99久久在观看| 精品乱码久久久久久久| 久久性生大片免费观看性| 久久无码人妻一区二区三区午夜| 91秦先生久久久久久久| 亚洲中文精品久久久久久不卡| 一级做a爰片久久毛片16| 狠狠色丁香久久婷婷综合_中| 久久亚洲精品视频| 色欲久久久天天天综合网| 久久精品18| 亚洲一本综合久久| 久久婷婷国产综合精品| 日韩十八禁一区二区久久 | 久久综合久久美利坚合众国| 免费精品99久久国产综合精品| 久久99精品国产麻豆宅宅| 看全色黄大色大片免费久久久| 7国产欧美日韩综合天堂中文久久久久| 久久久久亚洲av成人网人人软件 | 久久精品国产只有精品2020| 久久中文字幕人妻丝袜| 性欧美大战久久久久久久| 久久久久亚洲AV成人网人人网站| 久久精品国产亚洲av麻豆色欲 | 亚洲中文久久精品无码| 久久久午夜精品| 久久精品一本到99热免费| 久久精品国产清自在天天线| 精品综合久久久久久97| 久久久久久久精品妇女99| 久久99热这里只频精品6| 久久精品国产亚洲AV不卡| 久久天天躁夜夜躁狠狠躁2022 | 日韩精品久久久久久久电影|