Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting Selection from CHTMLEditView

Getting Selection from CHTMLEditView

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 3 Posters 5 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Matt Gates
    wrote on last edited by
    #1

    I would like to get at the selection in a CHTMLEditView so I can modify it and reinsert it back in to the view. Specifically I am looking to add the option to format the selection as a superscript or subscript. If I can get what is selected on screen into a CString I will be 90% there. Any suggestions? TIA Matt (Padawan Learner)

    T N 2 Replies Last reply
    0
    • M Matt Gates

      I would like to get at the selection in a CHTMLEditView so I can modify it and reinsert it back in to the view. Specifically I am looking to add the option to format the selection as a superscript or subscript. If I can get what is selected on screen into a CString I will be 90% there. Any suggestions? TIA Matt (Padawan Learner)

      T Offline
      T Offline
      Tom Archer
      wrote on last edited by
      #2

      The easiest way would be to copy the selected text to the clipboard and then retrieve the text from the clipboard

      // Copy selected text to clipboard using CHtmlEditView base
      // class (CHtmlEditCtrlBase) ExecHelperNN function
      ExecHelperNN(IDM_COPY)

      I have an article on CP that illustrates how to retrieve text from the clipboard. Cheers, Tom Archer Inside C#,
      Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

      M 1 Reply Last reply
      0
      • T Tom Archer

        The easiest way would be to copy the selected text to the clipboard and then retrieve the text from the clipboard

        // Copy selected text to clipboard using CHtmlEditView base
        // class (CHtmlEditCtrlBase) ExecHelperNN function
        ExecHelperNN(IDM_COPY)

        I have an article on CP that illustrates how to retrieve text from the clipboard. Cheers, Tom Archer Inside C#,
        Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

        M Offline
        M Offline
        Matt Gates
        wrote on last edited by
        #3

        Thanks, that should get me going in the right direction. I don't suppose you have a link to that article? Matt (Padawan Learner)

        T M 3 Replies Last reply
        0
        • M Matt Gates

          Thanks, that should get me going in the right direction. I don't suppose you have a link to that article? Matt (Padawan Learner)

          T Offline
          T Offline
          Tom Archer
          wrote on last edited by
          #4

          http://www.codeproject.com/clipboard/archerclipboard1.asp[^] Cheers, Tom Archer Inside C#,
          Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

          1 Reply Last reply
          0
          • M Matt Gates

            Thanks, that should get me going in the right direction. I don't suppose you have a link to that article? Matt (Padawan Learner)

            T Offline
            T Offline
            Tom Archer
            wrote on last edited by
            #5

            Pasting text basically amounts to the following

            if (OpenClipboard())
            {
            // Retrieve the Clipboard data (specifying that
            // we want ANSI text (via the CF_TEXT value).
            HANDLE hClipboardData = GetClipboardData(CF_TEXT);

            // Call GlobalLock so that to retrieve a pointer
            // to the data associated with the handle returned
            // from GetClipboardData.
            char *pchData = (char*)GlobalLock(hClipboardData);

            // Set a local CString variable to the data
            // and then update the dialog with the Clipboard data
            CString strFromClipboard = pchData;

            // Unlock the global memory.
            GlobalUnlock(hClipboardData);

            // Finally, when finished I simply close the Clipboard
            // which has the effect of unlocking it so that other
            // applications can examine or modify its contents.
            CloseClipboard();

            Cheers, Tom Archer Inside C#,
            Extending MFC Applications with the .NET Framework It's better to listen to others than to speak, because I already know what I'm going to say anyway. - friend of Jörgen Sigvardsson

            1 Reply Last reply
            0
            • M Matt Gates

              I would like to get at the selection in a CHTMLEditView so I can modify it and reinsert it back in to the view. Specifically I am looking to add the option to format the selection as a superscript or subscript. If I can get what is selected on screen into a CString I will be 90% there. Any suggestions? TIA Matt (Padawan Learner)

              N Offline
              N Offline
              Niall Barr
              wrote on last edited by
              #6

              This seems to work...

              //Derived from a table insert function that I can't remember the original source of.
              CString CHTMLEditView::GetSelection(void)
              {
              IDispatch * pDocDisp = NULL;
              IHTMLDocument2* pDoc;
              IHTMLSelectionObject* pSelObj;
              IHTMLTxtRange* pTxtRange;
              CString cstext;
              pDocDisp = GetHtmlDocument();
              HRESULT hr = pDocDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDoc );
              if (SUCCEEDED(hr)) {
              hr = pDoc->get_selection(&pSelObj);
              CComBSTR p;
              pSelObj->get_type(&p); // Should test this is suitable "Text" or "None"
              if ((SUCCEEDED(hr))&&((p==L"Text")||(p==L"None"))) {
              hr = pSelObj->createRange((IDispatch**)&pTxtRange);
              if (SUCCEEDED(hr)) {
              BSTR text;
              pTxtRange->get_htmlText(&text);
              cstext = text;
              SysFreeString(text);
              pTxtRange->Release();
              }
              pSelObj->Release();
              }
              pDocDisp->Release();
              pDoc->Release();
              }
              return cstext;
              }

              To put a string back into the selection use pTxtRange->pasteHTML(text) instead of pTxtRange->get_htmlText(&text) Niall.

              1 Reply Last reply
              0
              • M Matt Gates

                Thanks, that should get me going in the right direction. I don't suppose you have a link to that article? Matt (Padawan Learner)

                M Offline
                M Offline
                Matt Gates
                wrote on last edited by
                #7

                Thanks a lot, that article did the trick. Matt (Padawan Learner)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups