Link to home
Start Free TrialLog in
Avatar of hung2k
hung2k

asked on

How to Edit text in CHtmlView ?

Dear Experts,

I have used CHtmlView to load and display some local HTML pages, but after that, I do want to allow editing some texts in these pages, say,
- using keyboard to modify some text
- changing font, color and size
- save back to a new page

I know that there is some sample code using IHtmlEditHost or something similar, but the code used ATL and I don;t know how to apply to an existing CHtmlView derived class

Any help would be very appreciated

Thank you very much

Hung
Avatar of migel
migel

Hi!
you can switch CHTMLView to the design mode
here is example how to :

//class CMfcieView is derived from CHTMLView

void CMfcieView::OnDesign()
{
     static bool bDesign = false;
     bDesign = !bDesign;
     if (m_pBrowserApp != NULL)
          {
          IHTMLDocument2* lpTarget = NULL;
          LPDISPATCH lpDisp = GetHtmlDocument();

          if (lpDisp != NULL)
               {
               // the control will handle all printing UI
               if (SUCCEEDED(lpDisp->QueryInterface(IID_IHTMLDocument2,
                         (LPVOID*) &lpTarget)))
                    {
                    lpTarget->put_designMode(bDesign ? L"On" : L"Off");
                    lpTarget->Release();
                    }
               lpDisp->Release();
               }
          }
}
Avatar of hung2k

ASKER

Thanks migel,

I can allow edit the page with this but I dont know how to let it accept event from the application to change fonts and color or format them

Hung
Hi!
you have to use IOleCommandTarget interface of hte WebBrowser control to change selection style
here is example:
void CMfcieView::OnBoldSelection()
{
    static bool bDesign = false;
    bDesign = !bDesign;
    if (m_pBrowserApp != NULL)
         {
         IOleCommandTarget* pCmdTarg;
         m_pBrowserApp->QueryInterface(IID_IOleCommandTarget, (void**)&pCmdTarg);
         if (pCmdTarg!= NULL)
              {
              // Bold the current selection
              hr = pCmdTarg->Exec(&CGID_MSHTML, IDM_BOLD, MSOCMDEXECOPT_DODEFAULT, NULL, NULL);
              pCmdTarg->Release();
              }
         }
}

// for complete list of the command look into the MSHTMCID.H
Avatar of hung2k

ASKER

Migel,

I don't understand why my previous content are wiped every time I enter Design mode by using OnDesign

I tried not to keep the IHTMLDocument2 around and not release them as you did in OnDesign, but still can not prevent this previous content wiped out

What can I do to load a previous content, turn on Design mode then edit on that content

Thanks for your helpful help
Hi!
I do not understant what previous content you mean?
Your changes are gone out when you switch from Design to View mode?
Avatar of hung2k

ASKER

I loaded an existing HTML into the browser
Then because I wanted to edit this page, so I switched to edit mode.
But whenever OnDesign was called, everything was gone out, I only got an empty page and can do edit on a blank new page only.

hmm
it is so strange
I tested this code and everething was ok;
Do you edit local page?
Avatar of hung2k

ASKER

Oh, yes, I forgot to tell you that it's a local page that I loaded during OnBeginNavigate2 instead of loading as normal URL. There may be a problem there ? Whenever it switches to design mode, seemed that it reloads the page ?

Anyway, you deserved the point for this questions. I want to reward the points to you, but still want to discuss more about this a little bit before I close. Can I do that ?

Thanks


ASKER CERTIFIED SOLUTION
Avatar of migel
migel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial