Getting Selection from CHTMLEditView
-
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)
-
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)
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 -
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 SigvardssonThanks, that should get me going in the right direction. I don't suppose you have a link to that article? Matt (Padawan Learner)
-
Thanks, that should get me going in the right direction. I don't suppose you have a link to that article? Matt (Padawan Learner)
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 -
Thanks, that should get me going in the right direction. I don't suppose you have a link to that article? Matt (Padawan Learner)
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 -
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)
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.
-
Thanks, that should get me going in the right direction. I don't suppose you have a link to that article? Matt (Padawan Learner)
Thanks a lot, that article did the trick. Matt (Padawan Learner)