Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The example Jeroen gave is right, except it's not C++, and I thought you might find an example in the language you're using helpful. The TCppWebBrowser component is similar to the TWebBrowser component and has the same events. (It get more complicated when you try to access some of the internals, though.)</p> <p>Here's an edited version of an OnBeforeNavigate2 method I use:</p> <pre> <code>void __fastcall TMyForm::CppWebBrowserBeforeNavigate2(TObject* /*Sender*/, LPDISPATCH /*pDisp*/, Variant* URL, Variant* /*Flags*/, Variant* /*TargetFrameName*/, Variant* /*PostData*/, Variant* /*Headers*/, VARIANT_BOOL *Cancel) { UnicodeString strURL(URL->operator UnicodeString()); if (strURL != m_strWebPage && strURL != L"about:blank") { // if navigating somewhere else, ie the user clicked a link *Cancel = true; if ((int)::ShellExecute(NULL, L"open", strURL.c_str(), NULL, NULL, SW_SHOWNORMAL) &lt;= 32) { MyMessageBox(L"There was an error opening the link '" + strURL + L"'."); } } } </code> </pre> <p>It cancels navigation within the web browser, except to <code>about:blank</code> (you could remove that bit if it's not a legal page for your control) and the URL <code>m_strWebPage</code> that is the one I want it locked to. You could make this check more flexible, allowing it to, say, navigate anywhere on a certain domain but opening links to another domain in another window, for example.</p> <p>The code is also written for C++Builder 2009 / 2010, because of its use of <code>UnicodeString</code> and the <code>L</code> string prefix. You don't say what version you're using, but if you are using 2007 or before cast to <code>WideString</code> instead.</p> <p>Cheers,</p> <p>David</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload