Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WinForms - Toggle Form.ShowInTaskbar causes browser reload #2042

Closed
sai10rman opened this issue May 7, 2017 · 8 comments · Fixed by #3090
Closed

WinForms - Toggle Form.ShowInTaskbar causes browser reload #2042

sai10rman opened this issue May 7, 2017 · 8 comments · Fixed by #3090

Comments

@sai10rman
Copy link

changing the minimal example with this line
add a menu item that does:
this.ShowInTaskbar = !this.ShowInTaskbar;
causes the url to reload

Bug Report
using versions 55, 57
x86 + x64
Win10
using WinForms

https://github.com/cefsharp/CefSharp.MinimalExample

@amaitland amaitland changed the title changing form ShowInTaskbar causes cef to reload the url!! Toggle Form.ShowInTaskbar causes browser reload May 8, 2017
@amaitland amaitland added wontfix and removed invalid labels May 8, 2017
@amaitland
Copy link
Member

It may be possible to handle this scenario by reparenting the browser window handle.

http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10290

Pull Requests welcome.

@mitchcapper

This comment has been minimized.

@amaitland
Copy link
Member

The WinForms version will require changes to allow for it to be re-parented when a new handle is created.

@amaitland amaitland added this to the 63.0.0 milestone Dec 19, 2017
@amaitland
Copy link
Member

Resolved with 85e3297

@MihaMarkic
Copy link

@amaitland It doesn't see to work for me.
Below is the code from Form1.cs (new Winforms project template) and using 65 pre2 CefSharp.
(I trigger it when user wants to close the form, but the trigger doesn't really matter)
Should I set anything? From looking at source it sounds like it should be done automatically.

Expected: to see browser as it was
Actual: no browser - same as it was in 63

protected override void OnLoad(EventArgs e)
{
    if (!DesignMode)
    {
        browserControl = new ChromiumWebBrowser("https://blog.rthand.com/")
        {
            Dock = DockStyle.Fill
        };
        Controls.Add(browserControl);
    }
    base.OnLoad(e);
}

protected override async void OnClosing(CancelEventArgs e)
{
    e.Cancel = true;
    ShowInTaskbar = false;
    WindowState = FormWindowState.Minimized;
    base.OnClosing(e);

    await Task.Delay(1000);
    WindowState = FormWindowState.Normal;
    ShowInTaskbar = true;
}

@amaitland amaitland reopened this Jul 3, 2018
@amaitland amaitland removed this from the 63.0.0 milestone Jul 3, 2018
@amaitland
Copy link
Member

It works with the CefSharp.WinForms.Example when using the Tabbed control, which is what I would have tested with originally.

When the ChromiumWebBrowser is attached directly to the form it doesn't work as expected. At this point I'm not sure what the difference is.

@amaitland
Copy link
Member

The browser needs to be temporarily re-parented to a new control whilst the handle is destroyed and a new one created.

A workaround would look something like.

var parent = toolStripContainer.ContentPanel;

var tempControl = new Control();
tempControl.CreateControl();

parent.Controls.Remove(browser);
tempControl.Controls.Add(browser);

ShowInTaskbar = !ShowInTaskbar;

tempControl.Controls.Remove(browser);
parent.Controls.Add(browser);

tempControl.Dispose();
tempControl = null;

WinForms moves it's controls to a parking window when a handle is being recreated, ideally we'd do the same, unfortunately all the methods required are declared internal.

@amaitland amaitland changed the title Toggle Form.ShowInTaskbar causes browser reload WinForms - Toggle Form.ShowInTaskbar causes browser reload Mar 26, 2020
amaitland added a commit to amaitland/CefSharp that referenced this issue Mar 31, 2020
Use a parkingControl as a temporary parent for the CefBrowser while the handle is recreating.

https://docs.microsoft.com/en-us/archive/blogs/sburke/flashback-windows-forms-parking-window

Issue cefsharp#2840 should now create a new browser correctly when the MdiParent is changed,
the browser state won't be persisted, I believe this is expected as detailed in the .Net Source
https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Form.cs,1925

When toggling ShowInTaskbar when the browser is hosted in a TabControl then the handle isn't
actually recreated yet the browser is effectively parked, perhaps the .Net source has some tricks
we can use to further improve the case when using  MdiParent

Resolves cefsharp#2042
@helpr helpr bot added the pr-available label Mar 31, 2020
amaitland added a commit to amaitland/CefSharp that referenced this issue Mar 31, 2020
Use a parkingControl as a temporary parent for the CefBrowser while the handle is recreating.

https://docs.microsoft.com/en-us/archive/blogs/sburke/flashback-windows-forms-parking-window

Issue cefsharp#2840 should now create a new browser correctly when the MdiParent is changed,
the browser state won't be persisted, I believe this is expected as detailed in the .Net Source
https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Form.cs,1925

When toggling ShowInTaskbar when the browser is hosted in a TabControl then the handle isn't
actually recreated yet the browser is effectively parked, perhaps the .Net source has some tricks
we can use to further improve the case when using  MdiParent

Resolves cefsharp#2042
amaitland added a commit that referenced this issue Mar 31, 2020
Use a parkingControl as a temporary parent for the CefBrowser while the handle is recreating.

https://docs.microsoft.com/en-us/archive/blogs/sburke/flashback-windows-forms-parking-window

Issue #2840 should now create a new browser correctly when the MdiParent is changed,
the browser state won't be persisted, I believe this is expected as detailed in the .Net Source
https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Form.cs,1925

When toggling ShowInTaskbar when the browser is hosted in a TabControl then the handle isn't
actually recreated yet the browser is effectively parked, perhaps the .Net source has some tricks
we can use to further improve the case when using  MdiParent

Resolves #2042
@helpr helpr bot added pr-merged and removed pr-available labels Mar 31, 2020
@igorkrets
Copy link

this.Visible = !this.Visible;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants