龙龙Blog

龙龙Blog

  DonewsBlog  |  Donews首页  |  Donews社区  |  Donews邮箱  |  我的首页  |  联系作者  |  聚合   |  登录
  58篇文章 :: 0篇收藏:: 12篇评论:: 0个Trackbacks

公告

文章

收藏

相册

存档


正在读取评论……


A list of the COMDLG32 classes is shown in the following table.
ClassPurpose
CColorDialogAllows the user to select or create a color
CFileDialogAllows the user to open or save a file
CFindReplaceDialogAllows the user to substitute one string for another
CPageSetupDialogAllows the user to input page measurement parameters
CFontDialogAllows the user to select a font from a list of available fonts
CPrintDialogAllows the user to set up the printer and print a document

Using the CFileDialog Class Directly

CFileDialog dlg(TRUE, "bmp", "*.bmp");
if (dlg.DoModal() == IDOK) {
CFile file;
VERIFY(file.Open(dlg.GetPathName(), CFile::modeRead));
}

The first constructor parameter (TRUE) specifies that this object is a "File Open" dialog instead of a "File Save" dialog. The default file extension is bmp, and *.bmp appears first in the filename edit box. The CFileDialog::GetPathName function returns a CString object that contains the full pathname of the selected file.

Nested Dialogs

我们可以自己定制一个资源模板,让Common Dialog寄宿在我们的模板里:
1.Run AppWizard to produce \vcpp32\ex07b\ex07b. Accept all the defaults but two: select Single Document and deselect Printing And Print Preview. The options and the default class names are shown in the next graphic.
用向导生成一个工程ex07b,去掉打印功能。

2.Use the dialog editor to create a dialog resource.
Make the dialog box about 3-by-5 inches, and use the ID IDD_FILESPECIAL. Set the dialog's Style property to Child, its Border property to None, and select its Clip Siblings and Visible properties. Create the template with a button with ID IDC_DELETE and a group box with ID stc32=0x045f, as shown here.
在资源中插入一个Dialog,设置其ID为IDD_FILESPECIAL并将属性改为Child窗体,边框设为None,选上Siblings。在上面画一个GroupBox,ID设为stc32=0x045f,去掉Visible。再画一个Button with ID IDC_DELETE

Check your work by choosing Resource Symbols from the Visual C++ View menu. You should see a symbol list like the one shown in the graphic below.
在View菜单下面检查一下资源的ID是否正确。

3.Use ClassWizard to create the CSpecialFileDialog class. Fill in the New Class dialog, as shown here, and then click the Change button.
用向导生成类CSpecialFileDialog,Change the names to SpecFileDlg.h and SpecFileDlg.cpp.

Unfortunately, we cannot use the Base Class drop-down list to change the base class to CFileDialog, as that would decouple our class from the IDD_FILESPECIAL template. We have to change the base class by hand.

4.Map the WM_INITDIALOG message in the CSpecialDialog class. The OnInitDialog member function needs to change the common dialog's Open button caption to Delete. The child window ID is IDOK.
BOOL bRet = CFileDialog::OnInitDialog();
if (bRet == TRUE) {
GetParent()->GetDlgItem(IDOK)->SetWindowText("Delete");
}
return bRet;

5.Map the new IDC_DELETE button (Delete All Matching Files) in the CSpecialDialog class. 
 void CSpecialFileDialog::OnDelete() 
{
m_bDeleteAll = TRUE;
// 0x480 is the child window ID of the File Name edit control
// (as determined by SPYXX)
GetParent()->GetDlgItem(0x480)->GetWindowText(m_strFilename);
GetParent()->SendMessage(WM_COMMAND, IDCANCEL);

}
The OnDelete member function sets the m_bDeleteAll flag and then forces the main dialog to exit as if the Cancel button had been clicked. The client program (in this case, the view) gets the IDCANCEL return from DoModal and reads the flag to see whether it should delete all files. Here is the function:


6.Edit the file SpecFileDlg.h.
1)change
class CSpecialFileDialog : public CDialog
to
class CSpecialFileDialog : public CFileDialog


2)Add the following two public data members:

CString m_strFilename;
BOOL m_bDeleteAll;
3)Finally, edit the constructor declaration:
CSpecialFileDialog(BOOL bOpenFileDialog, 
LPCTSTR lpszDefExt = NULL,
LPCTSTR lpszFileName = NULL,
DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
LPCTSTR lpszFilter = NULL,
CWnd* pParentWnd = NULL

);

7.Edit the CSpecialFileDialog constructor in SpecFileDlg.cpp.

CSpecialFileDialog::CSpecialFileDialog(BOOL bOpenFileDialog,
LPCTSTR lpszDefExt, LPCTSTR lpszFileName, DWORD dwFlags,
LPCTSTR lpszFilter, CWnd* pParentWnd)
: CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName,
dwFlags, lpszFilter, pParentWnd)

{
//{{AFX_DATA_INIT(CSpecialFileDialog)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_ofn.Flags |= OFN_ENABLETEMPLATE;
m_ofn.lpTemplateName = MAKEINTRESOURCE(IDD_FILESPECIAL);
m_ofn.lpstrTitle = "Delete File";
m_bDeleteAll = FALSE;

}


In addition, it must set some members of the CFileDialog base class data
member m_ofn, which is an instance of the Win32 OPENFILENAME
structure. The Flags and lpTemplateName members control the
coupling to your IDD_FILESPECIAL template, and the lpstrTitle
member changes the main dialog box title.

8.Replace CDialog with CFileDialog in SpecFileDlg.h And SpecFileDlg.cpp

9.Add code to the virtual OnDraw function in file ex07bView.cpp. The
CEx07bView OnDraw function (whose skeleton was generated by AppWizard)
should be coded as follows to prompt the user to press the mouse button:
void CEx07bView::OnDraw(CDC* pDC)
 {
 pDC->TextOut(0, 0, "Press the left mouse button here.");
}

10.Add the OnLButtonDown message handler to the CEx07bView class. Use ClassWizard to create the message handler for WM_LBUTTON-DOWN, and then edit the code as follows: 
void CEx07bView::OnLButtonDown(UINT nFlags, CPoint point) 
{
CSpecialFileDialog dlgFile(TRUE, NULL, "*.obj");
CString strMessage;
int nModal = dlgFile.DoModal();
if ((nModal == IDCANCEL) && (dlgFile.m_bDeleteAll)) {
strMessage.Format(
"Are you sure you want to delete all %s files?",
dlgFile.m_strFilename);
if (AfxMessageBox(strMessage, MB_YESNO) == IDYES) {
HANDLE h;
WIN32_FIND_DATA fData;
while((h = ::FindFirstFile(dlgFile.m_strFilename, &fData))
!= (HANDLE) 0xFFFFFFFF) { // no MFC equivalent
if (::DeleteFile(fData.cFileName) == FALSE) {
strMessage.Format("Unable to delete file %s\n",
fData.cFileName);
AfxMessageBox(strMessage);
break;
}
}
}
}
else if (nModal == IDOK) {
CString strSingleFilename = dlgFile.GetPathName();
strMessage.Format(
"Are you sure you want to delete %s?", strSingleFilename);
if (AfxMessageBox(strMessage, MB_YESNO) == IDYES) {
CFile::Remove(strSingleFilename);
}
}

}
11.Of course, you'll need to include the statement 
#include "SpecFileDlg.h"

after the line

#include "ex07bView.h"



Trackback: http://tb.donews.net/TrackBack.aspx?PostId=601147


[点击此处收藏本文]  发表于2005年10月25日 10:14 AM




正在读取评论……

发表评论

大名:
网址:
验证码
评论