讨论:到底能否将CFindReplaceDialog作为基类?

zhdleo 2002-12-03 06:07:39
刚才看了个贴子:
想要将CFindReplaceDialog类作为基类。

类似CFileDialog CColorDialog CFontDialog 。
因为他们都是通用对话框。

但是,我查到的结果是------好像除了CFindReplaceDialog类,其他
几个CFileDialog CColorDialog CFontDialog 是可以做基类的。

我刚才研究了一下,并没有太大进展,所以发个贴子,
大家讨论一下。

我刚才想用的方法是:
现以CDialog为基类,建立这个CMyFindDlg的新类。

然后将里面的CDialog的地方换成CFindReplaceDialog。

并作了很多其它修改:
如----
DECLARE_DYNAMIC(CMyFindDlg) //新加的,
// Construction
public: //CMyFindDlg的构造,参照CFindReplaceDialog的Create参数。
CMyFindDlg( BOOL bFindDialogOnly,
LPCTSTR lpszFindWhat,
LPCTSTR lpszReplaceWith = NULL,
DWORD dwFlags = FR_DOWN,
CWnd* pParentWnd = NULL );
并将void CMyFindDlg::DoDataExchange(CDataExchange* pDX)删除。

最终做到和声明一个以CFileDialog通用对话框为基类的CMyFileDlg一样的格式。

运行后看看这个CMyFindDlg定义的是否有问题。
编译通过。
--------------------
下面就是要看能不能用CMyFindDlg定义对象了。

接下来,在void CMainFrame::OnFind()
{
// TODO: Add your command handler code here
CMyFindDlg dlg;
}想参生一个CMyFindDlg 的对象,可是失败了

提示:: error C2512: 'CMyFindDlg' : no appropriate default constructor available

看来还是我的CMyFindDlg 类不成功。

大家有什么好办法???
...全文
114 26 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
GoogleGeek 2002-12-08
  • 打赏
  • 举报
回复
可以作为基类!只是一般没有意义,除非你要改变默认的cfindreplacedialog对话框!
------------------------------
我已经解决了这个问题!

TNND,我第一次时,竟然将消息处理代码写在CMainFrame 中去了,搞得我得不到自己注册的消息!
---------一度怀疑自己的能力,想不到竟然翻了个如此低级的错误!
实践是检验真理的唯一标准!
itanynj@msn.com
------------------------------------------------
class CUseReplaceDialogView : public CEditView
{
protected: // create from serialization only
CUseReplaceDialogView();
DECLARE_DYNCREATE(CUseReplaceDialogView)

// Attributes
public:
CUseReplaceDialogDoc* GetDocument();
BOOL FindText( LPCTSTR lpszFind, BOOL bNext = TRUE, BOOL bCase = TRUE );
CFindReplaceDialog *m_pFRDlg;
bool bFirstSearchTime;

// Operations
public:

// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CUseReplaceDialogView)
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
//}}AFX_VIRTUAL

// Implementation
public:
virtual ~CUseReplaceDialogView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
//{{AFX_MSG(CUseReplaceDialogView)
afx_msg void OnFind();
//}}AFX_MSG
afx_msg LRESULT OnFindReplace(WPARAM wParam,LPARAM lParam);
DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG // debug version in UseReplaceDialogView.cpp
inline CUseReplaceDialogDoc* CUseReplaceDialogView::GetDocument()
{ return (CUseReplaceDialogDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_USEREPLACEDIALOGVIEW_H__B3EC2E7A_1258_4579_BC73_4A27411D28DE__INCLUDED_)

---------------------------------------------------------------------------------------------
static UINT FindReplaceDialogMessage =
::RegisterWindowMessage(FINDMSGSTRING);

/////////////////////////////////////////////////////////////////////////////
// CUseReplaceDialogView

IMPLEMENT_DYNCREATE(CUseReplaceDialogView, CEditView)

BEGIN_MESSAGE_MAP(CUseReplaceDialogView, CEditView)
//{{AFX_MSG_MAP(CUseReplaceDialogView)
ON_COMMAND(ID_SEARCH_REPLACE, OnFind)
//}}AFX_MSG_MAP
ON_REGISTERED_MESSAGE(FindReplaceDialogMessage, OnFindReplace )
END_MESSAGE_MAP()

CUseReplaceDialogView::CUseReplaceDialogView()
{
// TODO: add construction code here
m_pFRDlg=NULL;
bFirstSearchTime=true;
}
BOOL CUseReplaceDialogView:: FindText( LPCTSTR lpszFind, BOOL bNext ,BOOL bCase )
{
if(bFirstSearchTime)
{
CString str;
CEdit &editCtrl=this->GetEditCtrl();
editCtrl.SetSel(0,0);//将搜寻起点设置在cedit 得开始部分
this->bFirstSearchTime=false;
}
return CEditView::FindText(lpszFind,
bNext,
bCase);
}
void CUseReplaceDialogView::OnFind()
{
// TODO: Add your command handler code here
if(m_pFRDlg==NULL)
{
m_pFRDlg = new CFindReplaceDialog(); // Must be created on the heap
m_pFRDlg->m_fr.lStructSize = sizeof(FINDREPLACE);
m_pFRDlg->m_fr.hwndOwner = this->m_hWnd;

m_pFRDlg->Create( false, "", "", FR_DOWN | FR_WHOLEWORD, this );
}

}
LRESULT CUseReplaceDialogView::OnFindReplace(WPARAM wParam,LPARAM lParam)
{
// TODO: Add your command handler code here
CFindReplaceDialog* pFindReplace =
CFindReplaceDialog::GetNotifier(lParam);

ASSERT(pFindReplace != NULL);

if (pFindReplace->IsTerminating())
{
m_pFRDlg = NULL;
this->bFirstSearchTime=true;
}
else if (pFindReplace->FindNext())
{
if(FindText(pFindReplace->GetFindString(),
true,
FALSE))
;
//AdjustDialogPosition(pFindReplace);
else
{
this->bFirstSearchTime=true;
OnTextNotFound(pFindReplace->GetFindString());
MessageBox("Have reached the end of line,Cannt find!");
}
}
return 0;
}


zhdleo 2002-12-08
  • 打赏
  • 举报
回复
此问题完成的差不多了。
这个先结了,剩下的问题都到:

http://expert.csdn.net/Expert/topic/1235/1235211.xml?temp=2.567691E-02里解决。

谢谢大家。
whwjn 2002-12-07
  • 打赏
  • 举报
回复
ding
GZCompiler 2002-12-07
  • 打赏
  • 举报
回复
我没具体实现“查找功能的代码”,我只是觉得应该写在这里。
而且这个查找功能应该有一定的查找对象啊,比如如果你使用的是CEditView或CRichEditView,他就是在编辑器的buffer中查找,但是你用的是CView吧?那你是在什么中查找呢?

我可能没看全你的内容,所述不知重复与否。
cbc 2002-12-07
  • 打赏
  • 举报
回复
up
zhdleo 2002-12-07
  • 打赏
  • 举报
回复
我也知道写在这个!:

我的View视图类的基类用的是CEditView!

如果用的是CRichEditView,那就不用那么麻烦了:CRichEditView创建的SDI里本身就有查找功能,
而且如果是CRichEditView做基类,那就可以用函数AdjustDialogPosition()了。
该写查找代码的地方用
if(FindText(pFindReplace->GetFindString(),FALSE,FALSE))
AdjustDialogPosition(pFindReplace);
就可以了!
------------因为有个例子就是这样的。
但现在视图类的基类是CEditView,我就不知道怎么写了。

光用FindText(pFindReplace->GetFindString(),FALSE,FALSE),
效果出来就是:
比如,我输入了3行s
ssssssssssssss
ssssssssssssssss *
ssssssssssssss
把鼠标放到*的地方,查找ss,
那么就是从*的前面一个ss开始,一直往前,
不断的点查找下一个,就一直往前,
到了最前面就停住了。不能循环!!!
---------------------------------------
你说呢!
zhdleo 2002-12-06
  • 打赏
  • 举报
回复
你说自己写代码----替换,
那对于查找,一样吧!也要写个什么,调用
FindNext
GetNotifier
GetFindString
GetReplaceString
IsTerminating
MatchCase
MatchWholeWord
ReplaceAll
ReplaceCurrent
SearchDown
这个类对象的函数????

看人家,在函数
LRESULT CMainFrame::OnFindDialogMessage(WPARAM wParam, LPARAM lParam)
里,调用都成功了,我的怎么不行呢?
zhdleo 2002-12-06
  • 打赏
  • 举报
回复
我也知道,但我写的查找代码好像有问题!
GZCompiler 2002-12-06
  • 打赏
  • 举报
回复
这样来实现具体的“查找/替换”功能:

在你使用CFindReplaceDialog的类中,在类定义中增加消息函数声明:
afx_msg LONG OnFindReplace(WPARAM wParam, LPARAM lParam);

在类定义外(.h文件中)加入:
static UINT WM_FINDREPLACE = ::RegisterWindowMessage(FINDMSGSTRING);

在类的实现文件中,消息映射处增加代码:
BEGIN_MESSAGE_MAP( CMyFrameWnd, CFrameWnd )
//Normal message map entries here.
ON_REGISTERED_MESSAGE( WM_FINDREPLACE, OnFindReplace ) //加入该行
END_MESSAGE_MAP

在类的实现文件中,加入消息响应函数的实现代码,如:
LONG CYourClass::OnFindReplace(WPARAM wParam, LPARAM lParam)
{
//实现查找功能的代码
MessageBox("you see, it can work now!"); //测试用
}

不知说明白没有,我试过了,可以的。
根据是上一贴英文的资料所述内容。


GZCompiler 2002-12-06
  • 打赏
  • 举报
回复
CFindReplaceDialog dlg;
dlg.Create(TRUE,"");

-----------------------------------------------------------------
Creates and displays either a Find or Find/Replace dialog box object, depending on the value of bFindDialogOnly.

In order for the parent window to be notified of find/replace requests, you must use the WindowsRegisterWindowMessage function whose return value is a message number unique to the application’s instance. Your frame window should have a message map entry that declares the callback function (OnFindReplace in the example that follows) that handles this registered message. The following code fragment is an example of how to do this for a frame window class named CMyFrameWnd:

class CMyFrameWnd : public CFrameWnd
{
protected:
afx_msg LONG OnFindReplace(WPARAM wParam, LPARAM lParam);

DECLARE_MESSAGE_MAP()
};
static UINT WM_FINDREPLACE = ::RegisterWindowMessage(FINDMSGSTRING);

BEGIN_MESSAGE_MAP( CMyFrameWnd, CFrameWnd )
//Normal message map entries here.
ON_REGISTERED_MESSAGE( WM_FINDREPLACE, OnFindReplace )
END_MESSAGE_MAP

Within your OnFindReplace function, you interpret the intentions of the user and create the code for the find/replace operations.


注意最后两行的说明,你应该写一个消息函数来处理真正的查找/替换工作。
zhdleo 2002-12-06
  • 打赏
  • 举报
回复
这么个贴子,怎么没人看到:

没人能解答我吗?

连同:
http://expert.csdn.net/Expert/topic/1235/1235207.xml?temp=.901211
http://expert.csdn.net/Expert/topic/1235/1235211.xml?temp=.8234522
zhdleo 2002-12-05
  • 打赏
  • 举报
回复
我用CFindReplaceDialog类 创建一个 通用的查找对话框!

对话框是出现了!

不过要实现真正的查找,还要做些什么?这方面好像没有那本书上讲过具体的。

所以才向大家请教。
GoogleGeek 2002-12-05
  • 打赏
  • 举报
回复
当然需要自己写真正的代码进行替换功能
zhdleo 2002-12-05
  • 打赏
  • 举报
回复
创建是成功了,但是
怎么实现其查找/替换的真正功能,还是需要别的代码,你知道??
mietian 2002-12-05
  • 打赏
  • 举报
回复
这样:
m_pMyFindDialog= new CFindReplaceDialog();
m_pMyFindDialog->Create(TRUE,NULL,NULL,FR_DOWN,this);
m_pMyFindDialog->SetActiveWindow();
m_pMyFindDialog->ShowWindow(TRUE);
可以出来呀!
zhdleo 2002-12-05
  • 打赏
  • 举报
回复
怎么没人知道吗?

不会吧??
zhdleo 2002-12-04
  • 打赏
  • 举报
回复
问题解决!
zhdleo 2002-12-04
  • 打赏
  • 举报
回复
这样可以吗?

我还以为只能:
CMyFindDlg::Create(BOOL bFindDialogOnly, LPCTSTR lpszFindWhat,
LPCTSTR lpszReplaceWith, DWORD dwFlags, CWnd* pParentWnd )
:CFindReplaceDialog::Create(bFindDialogOnly, lpszFindWhat,
lpszReplaceWith, dwFlags, pParentWnd);
{
}
呢!
riverboat 2002-12-04
  • 打赏
  • 举报
回复
CMyFindDlg::Create(BOOL bFindDialogOnly, LPCTSTR lpszFindWhat,
LPCTSTR lpszReplaceWith, DWORD dwFlags, CWnd* pParentWnd )
{
return CFindReplaceDialog::Create(bFindDialogOnly, lpszFindWhat,
lpszReplaceWith, dwFlags, pParentWnd);
}
zhdleo 2002-12-03
  • 打赏
  • 举报
回复
由于CFindReplaceDialog是一个modeless dialogs无模式通用对话框!
所以不能用DoModal()
我能不能这样:
以CFindReplaceDialog为基类。
重载CFindReplaceDialog的Create函数?

这个应该怎么做:
我试了一下,没成功。
---------------
CMyFindDlg.h中: //原来定义是错误的。
BOOL Create(BOOL bFindDialogOnly,
LPCTSTR lpszFindWhat,
LPCTSTR lpszReplaceWith = NULL,
DWORD dwFlags = FR_DOWN,
CWnd* pParentWnd = NULL);
//现在希望的是重载CFindReplaceDialog的Create函数,所以在
CMyFindDlg.h中声明BOOL Create(。。。)

在CMyFindDlg.cpp中
应该怎么样写才是调用的是基类CFindReplaceDialog的Create函数?
我写的是:
CMyFindDlg::Create(BOOL bFindDialogOnly, LPCTSTR lpszFindWhat,
LPCTSTR lpszReplaceWith, DWORD dwFlags, CWnd* pParentWnd )
{
return TRUE;
}
但测试发现错误,调用的是派生类的CMyFindDlg::Create()

如果写成:
CMyFindDlg::Create(BOOL bFindDialogOnly, LPCTSTR lpszFindWhat,
LPCTSTR lpszReplaceWith, DWORD dwFlags, CWnd* pParentWnd )
:CFindReplaceDialog::Create(bFindDialogOnly, lpszFindWhat,
lpszReplaceWith, dwFlags, pParentWnd)
{
return TRUE;
}
有无法编译通过:
显示错误:
: error C2550: 'Create' : constructor initializer lists are only allowed on constructor definitions。

该怎么办???
加载更多回复(6)

16,548

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧