wxWidget中这块是写死的,这让我感到非常郁闷,而且没有提供一些简单的配置工具,关于右键弹出菜单,我们可以通过下面这段代码来看它是怎么实现的:
void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event)
{
#if wxUSE_RICHEDIT
if (IsRich())
{
if (!m_privateContextMenu)
{
m_privateContextMenu = new wxMenu;
m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
m_privateContextMenu->AppendSeparator();
m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
m_privateContextMenu->Append(wxID_COPY, _("&Copy"));
m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
m_privateContextMenu->AppendSeparator();
m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
}
PopupMenu(m_privateContextMenu);
return;
}
else
#endif
event.Skip();
}
我们可以通过重载这个函数来自定义我们的弹出菜单,假设我们要实现一个用于预览的文本框,那么我们可以这样写:
#include <wx/wx.h>
#include <wx/textctrl.h>
class CPreviewTextCtrl : public wxTextCtrl
{
public:
CPreviewTextCtrl(
wxWindow *parent, wxWindowID id, const wxString &value=wxEmptyString, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=0, const wxValidator &validator=wxDefaultValidator, const wxString &name=wxTextCtrlNameStr
);
public:
// 重写右键菜单
void OnContextMenu(wxContextMenuEvent& event);
private: // 声明事件表
DECLARE_EVENT_TABLE()
};
由于OnContextMenu不是虚函数所以我们不能光重载它,还需要在消息事件表中去重新注册它,否则我们还是会继续调用父类的OnContextMenu,然后弹出下面这些东西:
通过下面的代码我们去重新注册这个事件:
BEGIN_EVENT_TABLE(CPreviewTextCtrl, wxTextCtrl)
EVT_CONTEXT_MENU(CPreviewTextCtrl::OnContextMenu)
END_EVENT_TABLE()
接下来我们再定制我们自己的弹出菜单:
CPreviewTextCtrl::CPreviewTextCtrl( wxWindow *parent, wxWindowID id, const wxString &value/* =wxEmptyString */, const wxPoint &pos/* =wxDefaultPosition */, const wxSize &size/* =wxDefaultSize */, long style/* =0 */, const wxValidator &validator/* =wxDefaultValidator */, const wxString &name/* =wxTextCtrlNameStr */ ):
wxTextCtrl( parent, id, value, pos, size, style, validator )
{
}
void CPreviewTextCtrl::OnContextMenu(wxContextMenuEvent& event)
{
if (IsRich())
{
if (!m_privateContextMenu)
{
m_privateContextMenu = new wxMenu;
// m_privateContextMenu->Append(wxID_UNDO, _("&Undo"));
// m_privateContextMenu->Append(wxID_REDO, _("&Redo"));
// m_privateContextMenu->AppendSeparator();
// m_privateContextMenu->Append(wxID_CUT, _("Cu&t"));
m_privateContextMenu->Append(wxID_COPY, /*_("&Copy")*/ GetSearchLocalize(wxT("PreviewCopy")));
// m_privateContextMenu->Append(wxID_PASTE, _("&Paste"));
// m_privateContextMenu->Append(wxID_CLEAR, _("&Delete"));
m_privateContextMenu->AppendSeparator();
m_privateContextMenu->Append(wxID_SELECTALL, GetSearchLocalize(wxT("PreviewSelectAll"))/*_("Select &All")*/);
}
PopupMenu(m_privateContextMenu);
return;
}
else
{
event.Skip();
}
}
好了,我们把不需要的删除掉,然后加入我们自己的本地化系统,效果如图:
考虑到我们要实现的预览是只读功能,所以我们创建的时候还需要注意下Style的参数:
m_pPreviewText = new CPreviewTextCtrl( m_pRightSplitter, TS_ID_SEARCH_RESULT_PREVIEW, ::GetSearchLocalize(wxT("PreviewWindow")),
wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_READONLY|wxHSCROLL|wxTE_RICH );
from:http://hi.baidu.com/dbfr2011818/item/d670f4c040cea02e47d5c004