|
Rank: Member Groups: Member
Joined: 12/3/2013 Posts: 16
|
Hi,
i have a little problem with your webBrowser component for WinForms. I want to manage the NewWindow event to display a new browser control in a new tab, but i can't use WPF component but only the WinForms one. In your example (TabbedBrowser that is developed with WPF), essentially you put the new WebView object inside a new WebControl and host this control inside a DockView of a new Tab. This is possible because WebControl object of WPF library expose WebView with GET and SET. In WinForms edition, WebView is exposed ONLY with GET and NOT with SET... so, how can i do the same behaviour of TabbedBrowser in a pure WinForms project. Instead, if it is not possible, how can i redirect the "NewWindow" to the existing one (for example change the behaviour of "window.open" to navigate on the same WebView). I have not found any way to do that because, in the NewWindow event, the WebView URL is not populated and the Activate Event is not raised if i cannot host the new WebView inside any existing control...
Help me please!!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Very good point. We will look into it and get back to you as soon as possible!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi,
We have updated our download page with a new build that allows you to set the WinForm's WebControl.WebView property. Please take a look and let us know how it goes!
Thanks!
|
|
Rank: Member Groups: Member
Joined: 12/3/2013 Posts: 16
|
Hi, now the WebView works.. but i have a problem. When, in WPF, i manage the NewWindow Event, and inside it, i create a new TAB with the new WebView, the first WebControl does not freeze and do not navigate to the new window. Instead, in winforms, when i try to do the same thing, the first WebView freeze! why that? what i'm doing wrong?
I post my code here so you can help me.. i hope!
PS. In this implementation i do not use a tab but a WebControl hidden to user that i use only to manage newWindow because i do not want to display it, but if is a pdf document, i want to download.. so the behaviour is:
1. If the page require a new window and the window is a normal page, do nothing (display in an hidden webControl) 2. If the page require a new window and the window is a PDF file, display the download dialog 3. If the page require directly a pdf file, display the download dialog
Please help me!!
/// FORM CODE using System; using System.Windows.Forms; using EO.WebBrowser;
namespace EOBrowser { /// <summary> /// The Web Browser Interface /// </summary> public partial class EOBrowser : Form { private WebView _webView;
public EOBrowser() { InitializeComponent(); SetupBrowser(); }
private void SetupBrowser() {
wBrowser.WebView.Url = "http://www.google.it";
// Setup Handlers wBrowser.WebView.StatusMessageChanged += WebView_StatusMessageChanged; wBrowser.WebView.IsLoadingChanged += WebView_IsLoadingChanged; wBrowser.WebView.UrlChanged += WebView_UrlChanged; wBrowser.WebView.TitleChanged += WebView_TitleChanged; wBrowser.WebView.CanGoBackChanged += WebView_CanGoBackChanged; wBrowser.WebView.CanGoForwardChanged += WebView_CanGoForwardChanged; wBrowser.WebView.ShouldForceDownload += WebView_ShouldForceDownload; wBrowser.WebView.BeforeDownload += WebView_BeforeDownload; wBrowser.WebView.NewWindow += WebView_NewWindow;
// Update UI status WebView_UrlChanged(this, EventArgs.Empty); WebView_IsLoadingChanged(this, EventArgs.Empty); WebView_CanGoForwardChanged(this, EventArgs.Empty); WebView_CanGoBackChanged(this, EventArgs.Empty); }
private void InitializeNewWindowWW(WebView newWebView) { if(newWebView == null) newWebView = new WebView();
_webView = newWebView; newPageWC.WebView = _webView;
_webView.ShouldForceDownload += WebView_ShouldForceDownload; _webView.BeforeDownload += WebView_BeforeDownload; }
#region --- WebView Specific Event Handlers
/// <summary> /// Handle new window and navigate inside main one /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WebView_NewWindow(object sender, NewWindowEventArgs e) { InitializeNewWindowWW(e.WebView); e.Accepted = true; }
/// <summary> /// Manage download /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WebView_BeforeDownload(object sender, BeforeDownloadEventArgs e) { MessageBox.Show(@"Download Handled for file: " + e.Item.MimeType); }
/// <summary> /// Manage download of pdf files /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WebView_ShouldForceDownload(object sender, ShouldForceDownloadEventArgs e) { //Force download PDF files. You can also check e.Url //to force download certain Urls if (e.MimeType == "application/pdf") e.ForceDownload = true; }
/// <summary> /// Manage title change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WebView_TitleChanged(object sender, EventArgs e) { Text = String.Format("EOBrowser - {0}", wBrowser.WebView.Title); }
/// <summary> /// Manage url change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void WebView_UrlChanged(object sender, EventArgs e) { lblUrl.Text = String.Format("URL: {0}", wBrowser.WebView.Url); }
/// <summary> /// Handle Status Message Change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void WebView_StatusMessageChanged(object sender, EventArgs e) { string msg = wBrowser.WebView.StatusMessage; if (string.IsNullOrEmpty(msg)) { if (wBrowser.WebView.IsLoading) { lblLoading.Visible = true; msg = "Caricamento..."; pbLoading.Visible = true; pbLoading.Style = ProgressBarStyle.Marquee; } else { lblLoading.Visible = false; msg = "Pronto"; pbLoading.Visible = false; pbLoading.Style = ProgressBarStyle.Continuous; pbLoading.Value = 0; } } lblStatus.Text = String.Format("Stato: {0}", msg); }
/// <summary> /// Handle the loading change /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void WebView_IsLoadingChanged(object sender, EventArgs e) { // Update status bar to display "Loading..." or "Ready" WebView_StatusMessageChanged(this, EventArgs.Empty); }
/// <summary> /// Can go Back /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void WebView_CanGoBackChanged(object sender, EventArgs e) { tbBack.Enabled = wBrowser.WebView.CanGoBack; }
/// <summary> /// Can go Forward /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void WebView_CanGoForwardChanged(object sender, EventArgs e) { tbNext.Enabled = wBrowser.WebView.CanGoForward; }
#endregion --- WebView Specific Event Handlers
#region --- Toolbar Events
/// <summary> /// Reload Behavior /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tbReload_Click(object sender, EventArgs e) { wBrowser.WebView.Reload((ModifierKeys == Keys.Shift)); }
/// <summary> /// Go Back /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tbBack_Click(object sender, EventArgs e) { if (wBrowser.WebView.CanGoBack) wBrowser.WebView.GoBack(); }
/// <summary> /// Go Forward /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tbNext_Click(object sender, EventArgs e) { if (wBrowser.WebView.CanGoForward) wBrowser.WebView.GoForward(); }
#endregion --- Toolbar Events
private void toolStripButton1_Click(object sender, EventArgs e) { wBrowser.WebView.LoadUrl("www.google.it"); } } }
/////////////// DESIGNER CODE using EOBrowser.Component;
namespace EOBrowser { partial class EOBrowser { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null;
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Windows Form Designer generated code
/// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(EOBrowser)); this.wBrowser = new EO.WebBrowser.WinForm.WebControl(); this.sbStatus = new System.Windows.Forms.StatusStrip(); this.lblStatus = new System.Windows.Forms.ToolStripStatusLabel(); this.lblUrl = new System.Windows.Forms.ToolStripStatusLabel(); this.tbToolbar = new System.Windows.Forms.ToolStrip(); this.lblLoading = new System.Windows.Forms.ToolStripLabel(); this.toolStripButton1 = new System.Windows.Forms.ToolStripButton(); this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator(); this.tbReload = new System.Windows.Forms.ToolStripButton(); this.tbBack = new System.Windows.Forms.ToolStripButton(); this.tbNext = new System.Windows.Forms.ToolStripButton(); this.pbLoading = new System.Windows.Forms.ProgressBar(); this.newPageWC = new EO.WebBrowser.WinForm.WebControl(); this.sbStatus.SuspendLayout(); this.tbToolbar.SuspendLayout(); this.SuspendLayout(); // // wBrowser // this.wBrowser.BackColor = System.Drawing.Color.White; this.wBrowser.Dock = System.Windows.Forms.DockStyle.Fill; this.wBrowser.Location = new System.Drawing.Point(0, 42); this.wBrowser.Name = "wBrowser"; this.wBrowser.Size = new System.Drawing.Size(840, 434); this.wBrowser.TabIndex = 0; this.wBrowser.Text = "wBrowser"; this.wBrowser.WebView.Shortcuts = new EO.WebBrowser.Shortcut[0]; this.wBrowser.WebView.StatusMessage = null; this.wBrowser.WebView.Url = null; // // sbStatus // this.sbStatus.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.lblStatus, this.lblUrl}); this.sbStatus.Location = new System.Drawing.Point(0, 476); this.sbStatus.Name = "sbStatus"; this.sbStatus.Size = new System.Drawing.Size(840, 24); this.sbStatus.TabIndex = 1; this.sbStatus.Text = "statusStrip1"; // // lblStatus // this.lblStatus.AutoSize = false; this.lblStatus.BorderSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.Right; this.lblStatus.Name = "lblStatus"; this.lblStatus.Size = new System.Drawing.Size(180, 19); this.lblStatus.Text = "Stato: "; this.lblStatus.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // lblUrl // this.lblUrl.BorderSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.Right; this.lblUrl.Name = "lblUrl"; this.lblUrl.Size = new System.Drawing.Size(38, 19); this.lblUrl.Text = "URL: "; // // tbToolbar // this.tbToolbar.AutoSize = false; this.tbToolbar.BackColor = System.Drawing.Color.White; this.tbToolbar.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden; this.tbToolbar.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.lblLoading, this.toolStripButton1, this.toolStripSeparator1, this.tbReload, this.tbBack, this.tbNext}); this.tbToolbar.Location = new System.Drawing.Point(0, 0); this.tbToolbar.Name = "tbToolbar"; this.tbToolbar.RenderMode = System.Windows.Forms.ToolStripRenderMode.Professional; this.tbToolbar.Size = new System.Drawing.Size(840, 42); this.tbToolbar.TabIndex = 2; // // lblLoading // this.lblLoading.Alignment = System.Windows.Forms.ToolStripItemAlignment.Right; this.lblLoading.AutoSize = false; this.lblLoading.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.lblLoading.Image = global::EOBrowser.Properties.Resources.loading; this.lblLoading.ImageTransparentColor = System.Drawing.Color.Magenta; this.lblLoading.Name = "lblLoading"; this.lblLoading.Size = new System.Drawing.Size(39, 39); // // toolStripButton1 // this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.toolStripButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image"))); this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta; this.toolStripButton1.Name = "toolStripButton1"; this.toolStripButton1.Size = new System.Drawing.Size(23, 39); this.toolStripButton1.Text = "toolStripButton1"; this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click); // // toolStripSeparator1 // this.toolStripSeparator1.Name = "toolStripSeparator1"; this.toolStripSeparator1.Size = new System.Drawing.Size(6, 42); // // tbReload // this.tbReload.Image = global::EOBrowser.Properties.Resources.reload; this.tbReload.ImageTransparentColor = System.Drawing.Color.Magenta; this.tbReload.Name = "tbReload"; this.tbReload.Size = new System.Drawing.Size(60, 39); this.tbReload.Text = "Aggiorna"; this.tbReload.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText; this.tbReload.Click += new System.EventHandler(this.tbReload_Click); // // tbBack // this.tbBack.Image = global::EOBrowser.Properties.Resources.back; this.tbBack.ImageTransparentColor = System.Drawing.Color.Magenta; this.tbBack.Name = "tbBack"; this.tbBack.Size = new System.Drawing.Size(52, 39); this.tbBack.Text = "Indietro"; this.tbBack.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText; this.tbBack.Click += new System.EventHandler(this.tbBack_Click); // // tbNext // this.tbNext.Image = global::EOBrowser.Properties.Resources.next; this.tbNext.ImageTransparentColor = System.Drawing.Color.Magenta; this.tbNext.Name = "tbNext"; this.tbNext.Size = new System.Drawing.Size(45, 39); this.tbNext.Text = "Avanti"; this.tbNext.TextImageRelation = System.Windows.Forms.TextImageRelation.ImageAboveText; this.tbNext.Click += new System.EventHandler(this.tbNext_Click); // // pbLoading // this.pbLoading.Dock = System.Windows.Forms.DockStyle.Top; this.pbLoading.ForeColor = System.Drawing.Color.SteelBlue; this.pbLoading.Location = new System.Drawing.Point(0, 42); this.pbLoading.Name = "pbLoading"; this.pbLoading.Size = new System.Drawing.Size(840, 8); this.pbLoading.Style = System.Windows.Forms.ProgressBarStyle.Continuous; this.pbLoading.TabIndex = 3; this.pbLoading.Value = 50; // // newPageWC // this.newPageWC.BackColor = System.Drawing.Color.White; this.newPageWC.Location = new System.Drawing.Point(726, 7); this.newPageWC.Name = "newPageWC"; this.newPageWC.Size = new System.Drawing.Size(51, 29); this.newPageWC.TabIndex = 4; this.newPageWC.Visible = false; this.newPageWC.WebView.Shortcuts = new EO.WebBrowser.Shortcut[0]; this.newPageWC.WebView.StatusMessage = null; this.newPageWC.WebView.Url = null; // // EOBrowser // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(840, 500); this.Controls.Add(this.newPageWC); this.Controls.Add(this.pbLoading); this.Controls.Add(this.wBrowser); this.Controls.Add(this.tbToolbar); this.Controls.Add(this.sbStatus); this.Name = "EOBrowser"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "EOBrowser"; this.sbStatus.ResumeLayout(false); this.sbStatus.PerformLayout(); this.tbToolbar.ResumeLayout(false); this.tbToolbar.PerformLayout(); this.ResumeLayout(false); this.PerformLayout();
}
#endregion
private EO.WebBrowser.WinForm.WebControl wBrowser; private System.Windows.Forms.StatusStrip sbStatus; private System.Windows.Forms.ToolStripStatusLabel lblStatus; private System.Windows.Forms.ToolStrip tbToolbar; private System.Windows.Forms.ToolStripButton tbReload; private System.Windows.Forms.ToolStripLabel lblLoading; private System.Windows.Forms.ProgressBar pbLoading; private System.Windows.Forms.ToolStripStatusLabel lblUrl; private System.Windows.Forms.ToolStripButton toolStripButton1; private System.Windows.Forms.ToolStripSeparator toolStripSeparator1; private System.Windows.Forms.ToolStripButton tbBack; private System.Windows.Forms.ToolStripButton tbNext; private EO.WebBrowser.WinForm.WebControl newPageWC; } }
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Thanks for posting the test code. We have posted a new build that should fix this problem. You can download the new build from our download page.
|
|
Rank: Member Groups: Member
Joined: 12/3/2013 Posts: 16
|
Thank you very much! you're awesome! but the problem is only partially resolved.. Now the new page is handled correctly but the first one, continue to load a blank page! The behavior is this:
1. Click on the link that call an external window 2. The main WebView manage the NewWindow and associate the new WebView with an external (new) WebControl (like a new tab) 3. The main WebView in the NewWindow method, accept the event (e.accept) and the new tab goes correctly to the destination url 4. Now, the main WebView, display a blank page and lose all the handlers (the back and forward does not work anymore)..
Why this happen? i think that, if a WebView catch a NewWindow event, the WebView need to ignore any navigation command! Can you help me please??
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi,
It did not demonstrate this problem during our test. Can you isolate the problem into a small test app and send the test app to us? We will PM you as to where to send it.
Thanks!
|
|
Rank: Member Groups: Member
Joined: 12/3/2013 Posts: 16
|
Hi,
a first example can be this: with the code i have posted in this thread, if i open this site: "http://www.htmlcodetutorial.com/linking/_A_TARGET_95y98y108y97y110y107y.html" and click on the link "a new window", the interface freeze. If the link was a pdf file, the WebView navigate to a blank window and stay there.
|
|
Rank: Member Groups: Member
Joined: 12/3/2013 Posts: 16
|
i have also found that, the event management of the new WebView does not work.. in the NewWindow event i do this:
Code: C#
private void InitializeNewWindowWW(WebView newWebView)
{
if(newWebView == null) newWebView = new WebView();
// newPageWC is a component in the winform (WebControl)
newPageWC.WebView = newWebView;
newPageWC.WebView.ShouldForceDownload += WebView_ShouldForceDownload;
newPageWC.WebView.BeforeDownload += WebView_BeforeDownload;
newPageWC.WebView.FileDialog += WebView_FileDialog;
}
/// <summary>
/// Handle new window and navigate inside main one
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void WebView_NewWindow(object sender, NewWindowEventArgs e)
{
InitializeNewWindowWW(e.WebView);
e.Accepted = true;
}
but i notice that the ShouldDownload Event Handler was raised from the main WebView and that the "child" WebView (the new window) does not call any of the managed events... what i'm doing wrong??
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi,
You can not use the new WebView that way. When a new WebView is opened by an existing WebView, the existing WebView would wait on the new WebView to properly initialize first (that's when the NewWindow event handler is called), then load the target Url into the newly created WebView. In your case, the original WebView is destroyed when you assign the newWebView to newPageWC.WebView. So it is not able to finish the second stage of the initialization for the new WebView at all.
If you still want to maintain a single document UI, you can try to use two different WebControl but only one is visible. When you load a new page, you can use the second WebControl to load the new page and set the first one as invisible at the same time. When the new WebView try to load a new window, you would then reuse the first WebControl and hide the second WebControl. This way you will only have one visible at any given time but you will always have both the WebView that opens the window and the newly opened WebView alive at the same time.
Hope this helps. Please feel free to let us know if you have any more questions.
Thanks!
|
|
Rank: Member Groups: Member
Joined: 12/3/2013 Posts: 16
|
Hi, sorry but i have to disagree with your implementation. The standard behavior of a browser flow, in case of new window, must be the following (as implemented in various libraries like CEF): 1. The user click on a link that open a new window 2. The browser raise two events: OnBeforePopop and OnAfterCreate and with that events i can do anything i want: in the following example, i block every popup and going to navigate the popup url on the main WebView.. but i can also open a new window because the "WebView" that i can handle in this events, is a fully created WebView that share session with the main one!! your advise does not work if i am in an authenticated session because the two webView does not share session informations! example
Code: C#
bool ClientHandler::OnBeforePopup(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
const CefString& target_url,
const CefString& target_frame_name,
const CefPopupFeatures& popupFeatures,
CefWindowInfo& windowInfo,
CefRefPtr<CefClient>& client,
CefBrowserSettings& settings,
bool* no_javascript_access)
{
// browser->GetMainFrame()->LoadURL(target_url);
// return true;
windowInfo.style |= ~WS_VISIBLE;
return false;
}
void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
REQUIRE_UI_THREAD();
if(browser->IsPopup())
{
// If already exist a popup, close the previous one
if(m_PopupBrowser.get())
{
m_PopupBrowser->GetHost()->CloseBrowser(true);
}
m_PopupBrowser = browser;
}
if ( !m_Browser.get() ) {
// We need to keep the main child window, but not popup windows
m_Browser = browser;
m_BrowserHwnd = browser->GetHost()->GetWindowHandle();
m_BrowserId = browser->GetIdentifier();
}
}
So, why did you have exposed so few events? in this way your object is not so flexible.. It's a very good piece of code but lacks some events that can be very useful for a programmer that want to include a browser inside it's app... i hope you can do something for that! Thank you very much!!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Hi, If you only want to open the new Url in the same WebView, you can handle NewWindow event this way:
Code: C#
private void WebView_NewWindow(object sender, NewWindowEventArgs e)
{
EO.WebBrowser.WebView webView = (EO.WebBrowser.WebView)sender;
BeginInvoke(new Action<object>(
delegate(object arg)
{
webView.Url = e.TargetUrl;
}), new object[]{null});
}
Two key points in this code are: 1. You do not set e.Accepted to true. This causes the new WebView to be abandoned so it won't open; 2. Use BeginInvoke to load the new Url into the current WebView. It is necessary to use BeginInvoke so that WebView_NewWindow will return first, and then load the Url; Please let us know if this works for you. Thanks!
|
|
Rank: Member Groups: Member
Joined: 12/3/2013 Posts: 16
|
Thank you very much! in this way it works well for me!
I hope that this product will go ahead because it really is fantastic! Thank you!
|
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,196
|
Glad to hear that! Please feel free to let us know if there is anything else. And please help us to spread the word about our product! :)
Thanks!
|
|