我現在的程式中有打開檔案對話框,但是每次打開都是最近打開的檔案目錄,程式如下。請問有什么辦法可以打開時,直接打開根目錄。謝謝!!

TCHAR szFilter[] = _T("ABC檔案 (*.BIN)|*.BIN;*.BIN||");
CFileDialog fileDlg(TRUE, _T("(*.BIN)|*.BIN"), NULL, OFN_HIDEREADONLY, szFilter, NULL); // 構造打開檔案對話框
uj5u.com熱心網友回復:
CFileDialog Class MembersData Members
m_ofn The Windows OPENFILENAME structure. Provides access to basic file dialog box parameters.
OPENFILENAME
The OPENFILENAME structure contains information that the GetOpenFileName and GetSaveFileName functions use to initialize an Open or Save As common dialog box. After the user closes the dialog box, the system returns information about the user's selection in this structure.
typedef struct tagOFN { // ofn
DWORD lStructSize;
HWND hwndOwner;
HINSTANCE hInstance;
LPCTSTR lpstrFilter;
LPTSTR lpstrCustomFilter;
DWORD nMaxCustFilter;
DWORD nFilterIndex;
LPTSTR lpstrFile;
DWORD nMaxFile;
LPTSTR lpstrFileTitle;
DWORD nMaxFileTitle;
LPCTSTR lpstrInitialDir;
LPCTSTR lpstrTitle;
DWORD Flags;
WORD nFileOffset;
WORD nFileExtension;
LPCTSTR lpstrDefExt;
DWORD lCustData;
LPOFNHOOKPROC lpfnHook;
LPCTSTR lpTemplateName;
} OPENFILENAME;
uj5u.com熱心網友回復:
請問怎么寫程式呢?新手不懂。uj5u.com熱心網友回復:
比如fileDlg.m_ofn.lpstrInitialDir=_T("D:\\");
uj5u.com熱心網友回復:
這樣寫為什么不行呢?
TCHAR szFilter[] = _T("ABC檔案 (*.BIN)|*.BIN;*.BIN||");
CFileDialog fileDlg(TRUE, _T("(*.BIN)|*.BIN"), NULL, OFN_HIDEREADONLY, szFilter, NULL); // 構造打開檔案對話框
fileDlg.m_ofn.lpstrInitialDir=_T("D:\\");
uj5u.com熱心網友回復:
Collapse AllExpand All Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScriptVisual Basic
C#
Visual C++
J#
JScript
MFC Library Reference
CFileDialog Class
See Also Send Feedback
Encapsulates the common file dialog box for Windows.
class CFileDialog : public CCommonDialog
Remarks
Common file dialog boxes provide an easy way to implement File Open and File Save As dialog boxes (and other file-selection dialog boxes) in a manner consistent with Windows standards.
You can use CFileDialog as is with the constructor provided, or you can derive your own dialog class from CFileDialog and write a constructor to suit your needs. In either case, these dialog boxes will behave like standard MFC dialog boxes because they are derived from the CCommonDialog Class.
Both the appearance and the functionality of the CFileDialog with Windows Vista differ from the earlier versions of Windows. The default CFileDialog automatically uses the new Windows Vista style without code changes if a program is compiled and run under Windows Vista. Use the bVistaStyle parameter in the constructor to manually override this automatic update. The exception to the automatic update is customized dialog boxes. They will not be converted to the new style. For more information about the constructor, see CFileDialog::CFileDialog.
Note:
The control ID system differs in Windows Vista from earlier versions of Windows when you use a CFileDialog. You must update all references to CFileDialog controls in code before you can port your project from an earlier version of Windows.
Some CFileDialog methods are not supported under Windows Vista. Check the individual method topic for information about whether it is supported. In addition, the following inherited functions are not supported under Windows Vista:
CDialog::OnInitDialog
CDialog::OnSetFont
The windows messages for the CFileDialog class vary based on what operating system you are using. For example, Windows XP does not support CDialog::OnCancel and CDialog::OnOK for the CFileDialog class. However, Windows Vista does support them. For more information about the different messages that are generated and the order in which they are received, see CFileDialog Sample: Logging Event Order.
To use a CFileDialog object, first create the object by using the CFileDialog constructor. After the dialog box has been constructed, you can set or modify any values in the CFileDialog::m_ofn structure to initialize the values or states of the dialog box's controls. The m_ofn structure is of type OPENFILENAME. For more information, see the OPENFILENAME structure in the Windows SDK.
After initializing the dialog box's controls, call the CFileDialog::DoModal method to display the dialog box for the user to enter the path and file. DoModal returns whether the user selected the OK (IDOK) or the Cancel (IDCANCEL) button.
If DoModal returns IDOK, you can use one of CFileDialog's public member functions to retrieve the information input by the user.
CFileDialog includes several protected members that enable you to do custom handling of share violations, file name validation, and list-box change notification. These protected members are callback functions that most applications do not need to use, because default handling is done automatically. Message-map entries for these functions are not necessary because they are standard virtual functions.
You can use the Windows CommDlgExtendedError function to determine whether an error occurred during initialization of the dialog box and to learn more about the error.
The destruction of CFileDialog objects is handled automatically. It is not necessary to call CDialog::EndDialog.
To allow the user to select multiple files, set the OFN_ALLOWMULTISELECT flag before calling DoModal. You must supply your own file name buffer to accommodate the returned list of multiple file names. Do this by replacing m_ofn.lpstrFile with a pointer to a buffer you have allocated, after constructing the CFileDialog, but before calling DoModal.
When the user allocates their own buffer to accommodate OFN_ALLOWMULTISELECT, the buffer cannot be larger than 2048 or else everything gets corrupted (2048 is the maximum size).
Additionally, you must set m_ofn.nMaxFile with the number of characters in the buffer pointed to by m_ofn.lpstrFile. If you set the maximum number of files to be selected to n, the necessary buffer size is n*(_MAX_PATH + 1) + 1. For example:
Visual C++ Copy Code
CFileDialog dlgFile(TRUE);
CString fileName;
const int c_cMaxFiles = 100;
const int c_cbBuffSize = (c_cMaxFiles * (MAX_PATH + 1)) + 1;
dlgFile.GetOFN().lpstrFile = fileName.GetBuffer(c_cbBuffSize);
dlgFile.GetOFN().nMaxFile = c_cMaxFiles;
dlgFile.DoModal();
fileName.ReleaseBuffer();
CFileDialog relies on the COMMDLG.DLL file that ships with Windows.
If you derive a new class from CFileDialog, you can use a message map to handle any messages. To extend the default message handling, derive a class from CWnd, add a message map to the new class, and provide member functions for the new messages. You do not need to provide a hook function to customize the dialog box.
To customize the dialog box, derive a class from CFileDialog, provide a custom dialog template, and add a message map to process the notification messages from the extended controls. Any unprocessed messages should be passed to the base class.
Customizing the hook function is not required.
When you are using the Windows Vista style of the CFileDialog, you cannot use message maps and dialog templates. Instead, you will need to use the COM interfaces for similar functionality.
For more information about using CFileDialog, see Common Dialog Classes.
Requirements
Header:afxdlgs.h
See Also
Concepts
CFileDialog Members
CCommonDialog Class
Hierarchy Chart
Send feedback on this topic to Microsoft.
uj5u.com熱心網友回復:
Collapse AllExpand All Code: All Code: Multiple Code: Visual Basic Code: C# Code: Visual C++ Code: J# Code: JScriptVisual Basic
C#
Visual C++
J#
JScript
MFC Library Reference
CFileDialog::m_ofn
See Also Send Feedback
m_ofn is a structure of type OPENFILENAME. The data in this structure represents the current state of the CFileDialog.
Remarks
Use this structure to initialize the appearance of a File Open or File Save As dialog box after you construct it but before you display it with the DoModal method. For example, you can set the lpstrTitle member of m_ofn to the caption you want the dialog box to have.
With the Windows Vista style of CFileDialog, m_ofn is not guaranteed to always match the state of the dialog box. It is synchronized with the dialog box in earlier versions of Windows. See CFileDialog::ApplyOFNToShellDialog and CFileDialog::UpdateOFNFromShellDialog for more information about synchronizing the m_ofn structure and the CFileDialog state under Windows Vista.
Windows Vista style file dialogs do not support certain members and flags of the CFileDialog. As a result, these will have no effect.
The following is a list of the members that are not supported by Windows Vista:
lpstrCustomFilter
lpstrInitialDir
lCustData
lpfnHook
lpTemplateName
The following flags are not supported and therefore have no effect when you use the Windows Vista style of CFileDialog:
OFN_ENABLEHOOK
OFN_ENABLEINCLUDENOTIFY
OFN_ENABLETEMPLATE
OFN_ENABLETEMPLATEHANDLE
OFN_EXPLORER
OFN_EXTENSIONDIFFERENT
OFN_HIDEREADONLY
OFN_LONGNAMES - effectively always on in Windows Vista
OFN_NOLONGNAMES - effectively always off in Windows Vista
OFN_NONETWORKBUTTON - effectively always on in Windows Vista
OFN_READONLY
OFN_SHOWHELP
For more information about this structure, see the OPENFILENAME structure in the Windows SDK. For more information about the different behavior of the CFileDialog under Windows Vista, see CFileDialog Class.
Requirements
Header: afxdlgs.h
See Also
Concepts
CFileDialog Class
CFileDialog Members
Hierarchy Chart
Send feedback on this topic to Microsoft.
uj5u.com熱心網友回復:
void CViewQuickinfo::GetFilePath(){
CString StrTempFilePath;
TCHAR szExePathEx[MAX_PATH] = { 0 };
if ( ::GetModuleFileName( NULL, szExePathEx, MAX_PATH ) > 0 ) /* 獲取檔案路徑 */
{
StrTempFilePath.Format( "%s", szExePathEx );
int pos = StrTempFilePath.ReverseFind( '\\' );
m_strQuickInfoPath = StrTempFilePath.Mid( 0, pos );
m_strQuickInfoPath += _T( "\\Setting\\快訊.htm" );
}
}
你參考下吧GetModuleFileName獲取.exe當前路徑,你指的本目錄是這個嗎
uj5u.com熱心網友回復:
奇怪,這樣好像又行了uj5u.com熱心網友回復:
重建所有?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/122955.html
標籤:界面
上一篇:IWebBrowser2打開本地htm檔案時,有時可以打開,有時打不開?
下一篇:MFC制作發光矩形按鈕
