In my MFC application i have a derived CDHtmlDialog class that opens a login screen and i need to get a url and a cookie from the server after a redirect.
I navigate to the url inside the OnInitDialog and catch the redirect inside OnNavigateComplete:
void CDMYHtmlDlg::OnNavigateComplete(LPDISPATCH pDisp, LPCTSTR szUrl)
{
BSTR *bstr = nullptr;
this->GetDHtmlDocument(&this->m_spHtmlDoc);
if (this->m_spHtmlDoc != nullptr)
this->m_spHtmlDoc->get_cookie(bstr);
}
but the GetDHtmlDocument is returning E_NOINTERFACE, should i implement him? if yes how do i get the IHTMLDocument2?.
So my question is why i can't get the document and is this the right way to get a cookie?
Thank's in advance and sorry for my bad English.
Finally figured it out.
Inside the CDHtmlDialog class the OnNavigateComplete method is responsible for assigning value to m_spHtmlDoc but since i overwritten the method no ones assign value to the document, not even the OnDocumentComplete so the simple solution is
void CDHtmlDlgPersonalizado::OnNavigateComplete(LPDISPATCH pDisp, LPCTSTR szUrl)
{
/*CALL THE PARENT METHOD*/
CDHtmlDialog::OnNavigateComplete(pDisp, szUrl);
/*Now GetDHtmlDocument will get the value from m_spHtmlDoc and assign to spHtmlDoc*/
IHTMLDocument2Ptr spHtmlDoc = nullptr;
this->GetDHtmlDocument(&spHtmlDoc);
if (spHtmlDoc != nullptr)
{
BSTR bstr = ::SysAllocString(L" ");
spHtmlDoc->get_cookie(&bstr);
}
}
Related
I've seen a number of BHO samples and I try to implement this common scenario:
STDMETHODIMP CBhoImpl::SetSite(IUnknown* pSite)
{
if (NULL != pSite)
{
CComQIPtr<IWebBrowser2> webBrowser(pSite);
// webBrowser should hold a non-null pointer here
// but it holds a null pointer instead
//whatever
}
// whatever
}
SetSite() is invoked and the if branch is entered but QueryInterface() fails to retrieve IWebBrowser2.
I've seen a number of examples doing exactly the same.
What am I doing wrong?
Try querying the IUnknown parameter for IServiceProvider, and if successful then you can call its QueryService() method to get the IWebBrowser2, eg:
STDMETHODIMP CBhoImpl::SetSite(IUnknown* pSite)
{
if (pSite)
{
CComPtr<IWebBrowser2> webBrowser;
CComQIPtr<IServiceProvider> pServiceProvider(pSite);
if (pServiceProvider)
{
pServiceProvider->QueryService(SID_SWebBrowserApp, IID_PPV_ARGS(&webBrowser));
}
else
{
webBrowser = CComQIPtr<IWebBrowser2>(pSite);
}
if (webBrowser)
{
// use webBrowser as needed...
}
}
}
I have generated a Visual MFC project with a CMultiDocTemplate.
New and File open is already implemented. But I want now to open a database and query the data.
Under CDocument::OnOpenDocument I could read that it would be possible to write an application for that. But I have no idea how to open a document with an attached view without open an file.
What I need is a function OnOpenDB(LPCTSTR tableName) which creates the document and the view but not trying to open a file and extent the table name.
Google did not help me. I could not find any useful documentation.
#Adrian,
is there maybe another solution?
Finally I want to use more than one template for different functions.
In your solution I am missing a possibility to send the table name to the doc for doing the database query and provide the data to the view.
Can I do it in override the template class?
In your code, is dynamic_cast<CMyDoc *> ... ceating the view?
So finally I would need a public function which allows me to send a request for a new document with the table name.
Is this possible?
Update:
I tried to override CMultiDocTemplate::OpenDocumentFile(...), but it does not work. It seem that this function is not virtual.
Therefore I created a OpenDocumentFile(...) inside the override of the CMultiDocTemplate
CDocument* CStreamAuswertungMDT::OpenDocumentFile(LPCTSTR lpszPathName, BOOL bAddToMRU, BOOL bMakeVisible)
{
CDocument* pDoc = CreateNewDocument();
if (pDoc == NULL)
{
TRACE(traceAppMsg, 0, "CDocTemplate::CreateNewDocument returned NULL.\n");
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
return NULL;
}
ASSERT_VALID(pDoc);
BOOL bAutoDelete = pDoc->m_bAutoDelete;
pDoc->m_bAutoDelete = FALSE; // don't destroy if something goes wrong
CFrameWnd* pFrame = CreateNewFrame(pDoc, NULL);
pDoc->m_bAutoDelete = bAutoDelete;
if (pFrame == NULL)
{
AfxMessageBox(AFX_IDP_FAILED_TO_CREATE_DOC);
delete pDoc; // explicit delete on error
return NULL;
}
ASSERT_VALID(pFrame);
// avoid creating temporary compound file when starting up invisible
if (!bMakeVisible)
pDoc->m_bEmbedded = TRUE;
if (!pDoc->OnNewDocument())
{
// user has be alerted to what failed in OnNewDocument
TRACE(traceAppMsg, 0, "CDocument::OnNewDocument returned FALSE.\n");
pFrame->DestroyWindow();
return NULL;
}
// it worked, now bump untitled count
pDoc->SetTitle(lpszPathName);
pDoc->OnDocumentEvent(CDocument::onAfterOpenDocument);
InitialUpdateFrame(pFrame, pDoc);
return pDoc;
}
This is now working for me!
You have to be a bit careful but you can do this by creating a local (static) 'copy' of your CMultiDocTemplate object and then manually calling OpenDocumentFile() on that with a nullptr argument:
static CMultiDocTemplate MY_tmplate(IDR_MYDOC, RUNTIME_CLASS(CMyDoc), RUNTIME_CLASS(CMDIChild), RUNTIME_CLASS(CMyView));
void OnOpenDB(LPCTSTR tableName)
{
CMyDoc* pDoc = dynamic_cast<CMyDoc *>(MY_tmplate.OpenDocumentFile(nullptr));
//... do stuff with pDoc (using `tableName` to probe the database, maybe)
//
}
You will need to be careful when initializing any required data members and any limits, et cetera, in the pDoc object and its associated view(s).
You can set the document's filename/path at any stage, using the SetPathName() base class function; or you can just change the displayed title (that shown in the view's frame windows) with pDoc->SetTitle().
Alternatively, rather than using a local (copy) document template, you could add a member to your application class to run through its list of 'installed' templates, looking for a match to a given string identifier:
CMultiDocTemplate *MyApp::GetDocTemplate(CString name)
{
POSITION dtPos = GetFirstDocTemplatePosition(); CString dtName;
while (dtPos != nullptr) {
CMultiDocTemplate *mdTmp = dynamic_cast<CMultiDocTemplate *>(GetNextDocTemplate(dtPos));
if ((mdTmp == nullptr) || !mdTmp->GetDocString(dtName, CDocTemplate::docName)) break;
if (dtName == name) return mdTmp;
}
return nullptr;
}
Then, if that function returns a non-NULL pointer, use that to call OpenDocumentFile. For a list of the various string components you can use to match the relevant template, see the CDocTemplate documentation.
From various questions, I know it is impossible to create DataTemplate from the code behind without using the XamlReader. So I want to ask if there is a way to programatically generates the UI for each Item in a ListView. I don't seem to find any relevant event handler or member of ListView for this purpose. Ideally, I want the ListView to invoke my handler code to generate UI for each data item it needs to display.
Imitating the official XamlTreeView sample, I have tried overriding some ListView method such as PrepareContainerForItemOverride but this won't work. The solution I found is as #JustinXL suggests: producing ListViewItem and insert them to the ListView->Items directly
//assume that items is a list of items we want to bind
myListView->Items->Clear();
for(auto i : items)
{
ListViewItem^ v = ref new ListViewItem();
v->Content = GenerateUIFor(i);
myListView->Items->Append(v); // NOTE: a wrapping ListViewItem is required!
}
To support usual data binding, it would be best to make the data structure to cache the generated UI. For example,
ref class MyDataStructure
{
public:
property ListViewItem^ Item
{
ListViewItem^ get()
{
if (_item == nullptr)
GenerateUI();
return _item;
}
}
void GenerateUI()
{
_item = ref new ListViewItem();
_text_block = ref new TextBlock(); // sample
_item->Content = _text_block;
UpdateUI();
}
// Invoke this when changing the state of this object
void UpdateUI()
{
if (_text_block != nullptr) // sample
{
_text_block->Text = this->ToString(); // sample
}
}
private:
ListViewItem^ _item;
TextBlock^ _text_block;
};
The downside of this is of course we can't make use of data virtualization. But it works properly for small set of data. For large set, one can use website's approach with Next and Prev button to load next page or go back to previous page.
ItemContainerGenerator should let you construct the entire UI for an item inside a list view. Unfortunately there doesn't appear to be much in the way of non MSDN documentation/samples for this.
Alternatively if you can maintain a list of all the DataTemplates you might need to show, you could use a DataTemplateSelector to choose which DataTemplate you want to show for each individual item.
I'm doing my controller. When I click on an item in my ListWidget, the following method is called :
void ContactBookController::setCurrentlySelectedItem(QListWidgetItem *item)
{
setCurrentItem(*item);
m_window.setEditButtonsState(true);
}
And the setCurrentItem() method is as follows : (m_current_item is a private variable):
void ContactBookController::setCurrentItem(const QListWidgetItem ¤t_item)
{
m_current_item = current_item;
}
Now, when I create a contact, I add an item to the QListWidget, but I also create a Contact object, but I also bind them together in a QHash so I know what QListWidgetItem corresponds to what Contact. In short, here is what I did :
void ContactBookController::createContact()
{
auto new_contact = m_contact_book.createContact();
if(new_contact != nullptr)
{
new_contact->setName(tr("New contact"));
}
auto list_item = m_window.createContact(new_contact->getName());
m_window.clearSelection();
m_contact_map.insert(list_item, new_contact);
}
Now, when clicking on a QListWidgetItem, I activate the edit button and I would like to retrieve the corresponding Contact object. However, when doing this, the Contact object doesn't seem to be correct. Indeed, if I use a method on him (like getName()), my application instantly crashes.
Here is what I did :
void ContactBookController::editContact()
{
auto list_item = m_current_item;
auto contact = m_contact_map.value(&list_item); /* doesn't work */
}
My hash table is declared as such :
QHash<QListWidgetItem*, Contact*> m_contact_map;
Any idea what I did wrong ?
Your hash has the type QHash<QListWidgetItem*, Contact>. So, mapping a item pointer to a contact value.
When you save the mapping for a specific item with m_contact_map.insert(list_item, new_contact), you add a mapping from the item pointer to the contact. But when you try to retrieve the contact with m_contact_map.value(&list_item), you look up the value for the pointer to the local list_item variable, which points to somewhere on the stack, and has nothing to do with the pointer to the item that is shown in the view.
Either you need to save to pointer to the selected value, i.e. make m_current_item a QListWidgetItem *m_current_item, or you simply use QListWidget::currentItem() to retrieve the current item without the need for an additional member variable.
(Side note: You should check for currentItem() != nullptr, since a list widget does not necessarily has an item selected.)
I'm trying to create a custom lookup filter in a dialog in AX.
I've followed the instructions in this post x++ filter lookup in dialog and am getting a Stack Trace error -- FormRun object not initialized -- when I'm run my code.
What I am trying to do is filter the lookup() for the ConfigId EDT based on the selection from the ItemId EDT. I have the custom lookup() ready to go and working properly, I just can't get it called from my dialog box.
public Object dialog(DialogRunbase _dialog, boolean _forceOnClient)
{
DialogRunBase dialog;
;
dialog = super(_dialog, true);
dialog.caption('#RID2885');
dfItem = dialog.addField(typeid(ItemId));
dfInventLoc = dialog.addField(typeid(InventLocationId));
dfReplaceCost = dialog.addField(typeid(PdsCost));
dfItemConfig = dialog.addField(typeid(ConfigId));
dfColorId = dialog.addField(typeid(InventColorId), '#RID101');
return dialog;
}
Here's the call to the lookup():
void Fld_7_lookup()
{
Formrun fr = this.dialogModify().parmDialog();
Object control = fr.controlCallingMethod();
;
ConfigTable::lookupConfigIdSimple(control, dfItem.value());
}
And this is where it keeps getting the Stack Trace error:
public void dialogPostRun(DialogRunbase _dialog)
{
;
super(_dialog);
**_dialog.formRun().controlMethodOverload(true);** // Causes Stack Trace error
_dialog.formRun().controlMethodOverloadObject(this);
}
I have tried multiple configurations with the dialog box. When the code reaches that point, it still has information passed in from the dialog() method, but when it goes to get the FormRun, that object is blank.
Could someone please help me understand why there is no FormRun object associated with the DiaglogRunBase that is passed-in?
Thanks.
Mayby you should call super(_dialog) last in the dialogPostRun method.
Have a look on a similar solution and one more.
Did you check to see if your class is set to run at "Called From"?
Here is an example code for overriding the modified method. Maybe lookup has the same requirements:
public void dialogPostRun(DialogRunbase _dialog)
{
// Must be overriden to enable overriding modified method
;
_dialog.dialogForm().formRun().controlMethodOverload(true);
_dialog.dialogForm().formRun().controlMethodOverloadObject(this);
_dialog.formRun().controlMethodOverload(true);
_dialog.formRun().controlMethodOverloadObject(this);
super(_dialog);
}
And for the custom method:
boolean Fld2_1_modified()
{
FormStringControl c = dialog.formrun().controlCallingMethod();
boolean ret;
;
ret = c.modified(); // Super() Call the FormControl ->modified
dlgCustomField.value(MyClass::someMethod(dlgCustomField.value())); // example
return ret;
}