Get a text from CHtmlEditView - mfc

I am using CHtmlEditView class for my edit control. I want to get text written in the edit control. GetWindowText() returns an empty string. Not sure its supported but not giving me any warning either. I tried IHTMLDocument2 interface to get a text. I am getting a text but in html format (text written in <body>). So I am not sure how to get text only from CHtmlEditView control. I have gone through list of APIs of CHtmlEditView and its base classes but I did not get anything there. Can any one know how to get a text from CHtmlEditView ?

IHTMLElement::innerText
Sets or retrieves the text between the start and end tags of the
object.
Example:
CHtmlEditView html_edit;
// or CHtmlEditCtrl html_edit;
...
CString getPlainText()
{
CComPtr<IHTMLDocument2> doc2;
html_edit.GetDHtmlDocument(&doc2);
CComPtr<IHTMLElement> element;
doc2->get_body(&element);
CComBSTR bstr;
element->get_innerText(&bstr);
CString text = bstr;
return text;
}

Related

How to implement Find-on-Page command in MFC web browser control with pre-filled search word?

I'm using the MFC web browser control in my dialog-based MFC project to display some HTML content and I am trying to make it show the "Find on page" dialog window with the search word already pre-selected in it. (What you'd get if you hit Ctrl+F in the IE web browser.)
If I do the following the find dialog is shown OK, but it doesn't seem to support a way to pre-fill the search word:
IWebBrowser2* pWebBrowser = NULL;
LPUNKNOWN unknown = m_browser.GetControlUnknown();
unknown->QueryInterface(IID_IWebBrowser2,(void **)&pWebBrowser);
if(pWebBrowser)
{
HRESULT hr;
CComVariant varNull;
if(SUCCEEDED(hr = pWebBrowser->ExecWB(OLECMDID_FIND, OLECMDEXECOPT_PROMPTUSER, &varNull, &varNull)))
{
//Success!
}
pWebBrowser->Release();
}
if(unknown)
{
unknown->Release();
}
I found this MSDN page, that says:
OLECMDID_SHOWFIND Tells the receiver to show the Find dialog box. It
takes a VT_DISPATCH input param.
So evidently there's another Find command with OLECMDID_SHOWFIND ID, but I can't seem to make it work. I don't understand what is this VT_DISPATCH input param either? When I try this -- a total shot in the dark due to the lack of documentation:
CComVariant var1 = L"Pre-filled search word 1", var2 = L"Pre-filled search word 2";
hr = pWebBrowser->ExecWB(OLECMDID_SHOWFIND, OLECMDEXECOPT_DODEFAULT, &var1, &var2);
but I get hr=0x80040100 or "Trying to revoke a drop target that has not been registered"
Any idea how to make it work?

CFileDialog truncates offered file name

I have strange behaviour of CFileDialog once it is opened. The offered file name looks like it has been truncated, so that only last n characters are visible:
image http://ves.fijmovi.com/cpp/CFileDialog_1.jpg
As soon as I click on space for name, I get to see the whole file name:
image http://ves.fijmovi.com/cpp/CFileDialog_2.jpg
So it is not truly truncated, just the beginning position of shown string is not from the start.
The complete file name is in this case 36 characters long and part of the string which I get upon opening of dialog is from the 21st character to the last. I tested with some other files which are longer and they also appear truncated, but not from the 21st character, but some other one. I don't see any sense in this.
This piece of code has been several years old:
// Ask user for name and location of the ZIP
CString OfferName = DEFAULT_ZIPFILE_NAME;
CString File_Mask = GetString (IDS_ZIP_SELECT);
CFileDialog fileDlg (TRUE, _T("zip"), OfferName,
OFN_EXPLORER | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_ENABLESIZING,
File_Mask, pStateThread->GetThreadWindow());
CString strTitleName = GetString (IDS_ARCHIVE_CAPTION);
fileDlg.m_ofn.lpstrTitle = strTitleName;
CString Default_Dir = pConfig->GetTricUserKey (_T("TricSupportDir"));
fileDlg.m_ofn.lpstrInitialDir = Default_Dir;
if (fileDlg.DoModal () != IDOK) {
//..
}
Should I use some special flag for m_ofn? I've read article on MSN about OPENFILENAME structure but I see nothing worth adding to existing flags.
Any thoughts on what might be going on?
It happens in all software for me that uses the open file dialog. I think it's just a peculiarity of recent-ish incarnations of that dialog.

What is the method to set the text for a QTreeWidget's header?

I've checked the documentation here and I can't seem to find a method for setting the text of a QTreeWidget's title or header.
Without setting the title QTreeWidget automatically uses the number '1' in my code. An example of what it looks like outputted is below. I'm presuming QTreeWidget has a method for this and I just can't find it.
You're looking for setHeaderLabel.
Note that the documentation says it adds a new column, so if your view already has column 0 with text "1", you may instead have to do the following:
if(QTreeWidgetItem* header = treeWidget->headerItem()) {
header->setText(0, "My Text");
} else {
treeWidget->setHeaderLabel("My Text");
}
Here is an another method to set header texts
QStringList headerLabels;
headerLabels.push_back(tr("text1"));
headerLabels.push_back(tr("text2"));
headerLabels.push_back(tr("text3"));
..
headerLabels.push_back(tr("textN"));
treeWidget->setColumnCount(headerLabels.count());
treeWidget->setHeaderLabels(headerLabels);

Using MSHTML how do you set the focus on an input field?

Using MSHTML I'd like to set the user focus on a text element, what I've been given and tried so far doesn't work.
CComPtr<IHTMLInputTextElement> field;
field->put_value( .. ); // I have the right element as I can set the text...
VARIANT vStatus;
vStatus.vt = VT_BOOL;
vStatus.boolVal = VARIANT_TRUE;
field->put_status(vStatus);
CComQIPtr<IHTMLInputElement> inputElement(field);
if( inputElement )
inputElement->put_status(VARIANT_TRUE);
and by doesn't work I mean it runs and reports no errors but the focus on the web page doesn't change.
CComQIPtr<IHTMLElement2 > element2(field);
if(element2!=NULL) element2->focus();

CHtmlView class and focus

I have an SDI application written in MFC. The frame is divided into 1 row and 2 columns using a splitter window. Below are details of Row and Column (R0C0 means Row#0 and Col#0)
R0C0 view is a CFormView with multiple input controls like text box, combo box etc.
R0C1 view is a CHtmlView that contains HTML content relavant to the control that has input focus in the R0C0
I am able to update the HTML content and also invoke Javascript functions through my MFC code.
Problem:
When user clicks on the R0C1, continaing CHtmlView, the focus is now on the html page. I wish to allow the user to tab out of R0C1 using the key board and return back to R0C0. Can you help with this please? The user can obviously click on the R0C0 view using mouse but we have a user who needs to use Keyboard for using this functionality.
Let me know if the question is not descriptive enough and I'll simplify it further.
Appreciate your time.
Thanks,
Byte
Try to overload CHtmlView::OnTranslateAccelerator. I have successfully used this trick to disable refresh with F5 key. Derive your own class from CHtmlView and overload
virtual HRESULT OnTranslateAccelerator(LPMSG lpMsg, const GUID* pguidCmdGroup, DWORD nCmdID);
like this:
HRESULT CMyHtmlView::OnTranslateAccelerator(LPMSG lpMsg, const GUID* pguidCmdGroup, DWORD nCmdID)
{
if(lpMsg->message == WM_KEYDOWN && GetAsyncKeyState(VK_TAB) != 0 )
{
// change focus
return S_OK;
}
return CHtmlView::OnTranslateAccelerator( lpMsg, pguidCmdGroup, nCmdID);
}