|

楼主 |
发表于 2006-1-13 11:17:36
|
显示全部楼层
5.6.6 公用对话框的使用实例 <BR><BR> 现在,让我们来测试一下公用对话框的使用。请读者用AppWizard创建一个单文档的MFC应用程序,名为CommonDlg。注意别忘了在AppWizard的第一步中选Single document。 <BR><BR> CommonDlg程序要对所有的公用对话框进行了测试。为此,首先要提供用户命令接口。请读者在CommonDlg的菜单资源中插入一个名为&Common的新菜单,这个菜单插在Help菜单之前。然后,在Common菜单中,请按表5.11创建菜单项。 <BR><BR> 表5.11 Common菜单的菜单项 <BR><BR> Caption <BR> ID <BR><BR> &Color... <BR> ID_COMMON_COLOR <BR><BR> &Open file... <BR> ID_COMMON_OPENFILE <BR><BR> &Save file... <BR> ID_COMMON_SAVEFILE <BR><BR> &Font... <BR> ID_COMMON_FONT <BR><BR> & rint... <BR> ID_COMMON_PRINT <BR><BR> P&rint setup... <BR> ID_COMMON_PRINTSETUP <BR><BR> F&ind... <BR> ID_COMMON_FIND <BR><BR> &Replace... <BR> ID_COMMON_REPLACE <BR><BR><BR> 接下来的工作是编写测试程序的源代码。首先,利用ClassWizard为表5.11的菜单项创建消息处理函数,注意这些处理函数都是CCommonDlgView的成员。接着,请按清单5.10和5.11修改程序。限于篇幅,这里仅列出与测试相关的部分源代码。 <BR><BR> 清单5.10 头文件CommonDlgView.h <BR>
< > class CCommonDlgView : public CView <BR><BR> { <BR><BR> . . . . . . <BR><BR> #ifdef _DEBUG <BR><BR> virtual void AssertValid() const; <BR><BR> virtual void Dump(CDumpContext& dc) const; <BR><BR> #endif <BR><BR> protected: <BR><BR> void DispPrintInfo(CPrintDialog& dlg); <BR><BR> protected: <BR><BR> CFont m_Font; //正文的字体 <BR><BR> COLORREF m_ForeColor; //正文的前景色 <BR><BR> COLORREF m_BackColor; //正文的背景色 <BR><BR> CFindReplaceDialog *m_pFindReplaceDlg; <BR><BR> BOOL m_bFindOnly; <BR><BR> // Generated message map functions <BR><BR> protected: <BR><BR> //Find和Replace对话框通知消息处理函数 <BR><BR> afx_msg LRESULT OnFindReplaceCmd(WPARAM, LPARAM lParam); <BR><BR> //{{AFX_MSG(CCommonDlgView) <BR><BR> afx_msg void OnCommonColor(); <BR><BR> afx_msg void OnCommonFont(); <BR><BR> afx_msg void OnCommonOpenfile(); <BR><BR> afx_msg void OnCommonSavefile(); <BR><BR> afx_msg void OnCommonPrint(); <BR><BR> afx_msg void OnCommonPrintsetup(); <BR><BR> afx_msg void OnCommonFind(); <BR><BR> afx_msg void OnCommonReplace(); <BR><BR> //}}AFX_MSG <BR><BR> DECLARE_MESSAGE_MAP() <BR><BR> }; </P>
< ><BR> . . . . . . <BR><BR> 清单5.11 文件CCommonDlgView.cpp <BR><BR></P>
< > #include "stdafx.h" <BR><BR> #include "CommonDlg.h" <BR><BR> #include "CommonDlgDoc.h" <BR><BR> #include "CommonDlgView.h" <BR><BR> #ifdef _DEBUG <BR><BR> #define new DEBUG_NEW <BR><BR> #undef THIS_FILE <BR><BR> static char THIS_FILE[] = __FILE__; <BR><BR> #endif <BR><BR><BR> IMPLEMENT_DYNCREATE(CCommonDlgView, CView) <BR> <BR><BR> //获取对本进程唯一的消息编号 <BR><BR> static const UINT nMsgFindReplace = ::RegisterWindowMessage(FINDMSGSTRING); <BR><BR> BEGIN_MESSAGE_MAP(CCommonDlgView, CView) <BR><BR> //{{AFX_MSG_MAP(CCommonDlgView) <BR><BR> ON_COMMAND(ID_COMMON_COLOR, OnCommonColor) <BR><BR> ON_COMMAND(ID_COMMON_FONT, OnCommonFont) <BR><BR> ON_COMMAND(ID_COMMON_OPENFILE, OnCommonOpenfile) <BR><BR> ON_COMMAND(ID_COMMON_SAVEFILE, OnCommonSavefile) <BR><BR> ON_COMMAND(ID_COMMON_PRINT, OnCommonPrint) <BR><BR> ON_COMMAND(ID_COMMON_PRINTSETUP, OnCommonPrintsetup) <BR><BR> ON_COMMAND(ID_COMMON_FIND, OnCommonFind) <BR><BR> ON_COMMAND(ID_COMMON_REPLACE, OnCommonReplace) <BR><BR> //}}AFX_MSG_MAP <BR><BR> // Standard printing commands <BR><BR> ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) <BR><BR> ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) <BR><BR> ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) <BR><BR> <BR><BR> ON_REGISTERED_MESSAGE(nMsgFindReplace, OnFindReplaceCmd) <BR><BR> END_MESSAGE_MAP() <BR><BR> <BR> CCommonDlgView::CCommonDlgView() <BR><BR> { <BR><BR> // TODO: add construction code here <BR><BR><BR> //缺省前景色为黑色,背景色为白色,字体为系统字体 <BR><BR> m_ForeColor=0; <BR><BR> m_BackColor=0xFFFFFF; <BR><BR> m_Font.CreateStockObject(SYSTEM_FONT); <BR><BR> m_pFindReplaceDlg=NULL; <BR><BR> } <BR><BR> void CCommonDlgView::OnDraw(CDC* pDC) <BR><BR> { <BR><BR> CCommonDlgDoc* pDoc = GetDocument(); <BR><BR> ASSERT_VALID(pDoc); <BR><BR> // TODO: add draw code for native data here <BR><BR> int x,y; <BR><BR> CFont *pOldFont; <BR><BR> TEXTMETRIC TM; <BR><BR> int textHeight; <BR><BR> //设置正文的字体 <BR><BR> pOldFont=pDC->SelectObject(&m_Font); <BR><BR> //设置正文的前景色和背景色 <BR><BR> pDC->SetTextColor(m_ForeColor); <BR><BR> pDC->SetBkColor(m_BackColor); <BR><BR> //计算每行正文的高度 <BR><BR> pDC->GetTextMetrics(&TM); <BR><BR> textHeight=TM.tmHeight+TM.tmExternalLeading; <BR><BR> //输出正文 <BR><BR> x=5;y=5; <BR><BR> pDC->TextOut(x,y,"ABCDEFG"); <BR><BR> y+=textHeight; <BR><BR> pDC->TextOut(x,y,"abcdefg"); <BR><BR> //恢复原来的字体 <BR><BR> pDC->SelectObject(pOldFont); <BR><BR> } <BR><BR> void CCommonDlgView::OnCommonColor() <BR><BR> { <BR><BR> // TODO: Add your command handler code here <BR><BR> CColorDialog dlg; <BR><BR> if(dlg.DoModal()==IDOK) <BR><BR> { <BR><BR> m_BackColor=dlg.GetColor(); <BR><BR> //重绘视图 <BR><BR> Invalidate(); <BR><BR> UpdateWindow(); <BR><BR> } <BR><BR> } <BR><BR> void CCommonDlgView::OnCommonFont() <BR><BR> { <BR><BR> // TODO: Add your command handler code here <BR><BR> CFontDialog dlg; <BR><BR> if(dlg.DoModal()==IDOK) <BR><BR> { <BR><BR> LOGFONT LF; <BR><BR> //获取所选字体的信息 <BR><BR> dlg.GetCurrentFont(&LF); <BR><BR> m_ForeColor=dlg.GetColor(); <BR><BR> //建立新的字体 <BR><BR> m_Font.DeleteObject(); <BR><BR> m_Font.CreateFontIndirect(&LF); <BR><BR> Invalidate(); <BR><BR> UpdateWindow(); <BR><BR> } <BR><BR> } <BR><BR> void CCommonDlgView::OnCommonOpenfile() <BR><BR> { <BR><BR> // TODO: Add your command handler code here <BR><BR> //过滤字符串 <BR><BR> char szFileFilter[]= <BR><BR> "Cpp files(*.cpp)|*.cpp|" <BR><BR> "Header files(*.h)|*.h|" <BR><BR> "All files(*.*)|*.*||"; <BR><BR> CFileDialog dlg(TRUE, //Open对话框 <BR><BR> "cpp", //缺省扩展名 <BR><BR> "*.cpp", <BR><BR> OFN_HIDEREADONLY|OFN_FILEMUSTEXIST, //文件必须存在 <BR><BR> szFileFilter, <BR><BR> this); <BR><BR> if(dlg.DoModal()==IDOK) <BR><BR> { <BR><BR> CString str="The full path name is:"; <BR><BR> str+=dlg.GetPathName(); <BR><BR> AfxMessageBox(str); <BR><BR> } <BR><BR> } </P>
< ><BR><BR></P> |
|