typedef struct {
int nHtmlEdCmdID;
CString sMenuText;
} EditCmds;
EditCmds m_EdCmds[]= {
{ IDM_BOLD ,L"Bold\tCtrl+B" },
{ IDM_ITALIC ,L"Italic\tCtrl+I" },
{ IDM_UNDERLINE ,L"Underline\tCtrl+U" },
{ IDM_REMOVEFORMAT ,L"RemoveFormat\tCtrl+spc" },
{-1 ,0 },
{ IDM_FONT ,L"Font and Color" },
{ IDM_FORECOLOR ,L"Foreground color" },
{ IDM_BACKCOLOR ,L"Background color" },
{-1 ,0 },
{ IDM_ORDERLIST ,L"OrderList" },
{ IDM_UNORDERLIST ,L"UnorderList" },
{-1 ,0 },
{ IDM_INDENT ,L"Indent" },
{ IDM_OUTDENT ,L"Outdent" },
{ IDM_JUSTIFYCENTER ,L"JustifyCenter" },
{ IDM_JUSTIFYLEFT ,L"JustifyLeft" },
{ IDM_JUSTIFYRIGHT ,L"JustifyRight" },
{-1 ,0 },
{ IDM_HYPERLINK ,L"Hyperlink\tCtrl+K" },
{ IDM_UNLINK ,L"Unlink" },
{ IDM_BOOKMARK ,L"Bookmark (anchor)" },
{ IDM_UNBOOKMARK ,L"UnBookmark" },
{-1 ,0 },
{ IDM_IMAGE ,L"Insert Image..." },
{ IDM_HORIZONTALLINE ,L"Horizontal Line" },
{-1 ,0 },
{ IDM_UNDO ,L"Undo\tCtrl+bksp" },
{ IDM_CUT ,L"Cut\tCtrl+X" },
{ IDM_COPY ,L"Copy\tCtrl+C" },
{ IDM_DELETE ,L"Delete\tDel" },
{ IDM_PASTE ,L"Paste\tCtrl+V" },
{ IDM_SELECTALL ,L"SelectAll\tCtrl+A" },
{-1 ,0 },
{ IDM_FIND ,L"Find...\tCtrl+F" },
{ IDM_PRINT ,L"Print...\tCtrl+P" },
{ IDM_PRINTPREVIEW ,L"Print Preview" },
{0 ,0 }, // end of list
};
void CEditHtmlDlg::OnContextMenu(CWnd* pWnd, CPoint pt )
{
CMenu mnu;
mnu.CreatePopupMenu();
for (int j=0; m_EdCmds[j].nHtmlEdCmdID != 0; j++ ) {
int nCmdID = m_EdCmds[j].nHtmlEdCmdID;
if ( nCmdID == -1 ) {
mnu.AppendMenuW( MF_SEPARATOR, nCmdID, L"" );
} else {
mnu.AppendMenuW( MF_STRING, nCmdID+M_CmdsStart, m_EdCmds[j].sMenuText );
long nStatus= m_ctlEditHtml.QueryStatus( nCmdID );
if (!(nStatus & OLECMDF_ENABLED)) {
mnu.EnableMenuItem( nCmdID+M_CmdsStart, MF_DISABLED|MF_GRAYED);
}
}
}
m_ctlEditHtml.ClientToScreen( &pt );
mnu.TrackPopupMenu(TPM_LEFTALIGN, pt.x, pt.y, this );
}
void CEditHtmlDlg::OnRangeCmds(UINT nID)
{
int nRealID= nID-M_CmdsStart;
// Demo: Some commands need user input
if ( nRealID == IDM_BACKCOLOR ) {
m_ctlEditHtml.SetBackColor(L"green"); // or numeric: RGB(0,255,0)
return;
}
m_ctlEditHtml.ExecCommand( nRealID, 0,0,0 );
}
ON_COMMAND_RANGE(M_EdCmdsStart, M_EdCmdsStart+IDM_REMOVEFORMAT, OnRangeCmds)
//
in the MESSAGE MAP section near the top of the dialog box code. I use the existing IDM_XXXX values, but added an offset value (M_EdCmdsStart) to each one to avoid conflict with other command you might be supporting. Every WM_COMMAND message that is higher than M_EdCmdsStart will be routed to my OnRangeCmds handler (line 66 above). These mostly pass through directly to the ExecCommand function, but it is also possible to intercept the command and take specific action here. For instance, setting the background color requires a color value -- either a string such as "Red", or an RGB value such as 0xFF000000. To demonstrate, I just hard coded "green," but you could toss up a dialog box to obtain the desired color value.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)