BrowserWindow OnClose event #479

Closed
opened 2026-01-29 16:40:48 +00:00 by claunia · 5 comments
Owner

Originally created by @AykutToprak on GitHub (May 6, 2020).

Originally assigned to: @GregorBiswanger on GitHub.

🚨 The issue tracker is not for questions 🚨

How to hide browser window and put application in Tray mode on close button clicked?

I tried as it shown below;

browserWindow.OnClose += () => OnClose(browserWindow);

and OnClose action;


private static void OnClose(BrowserWindow browserWindow)
{
    browserWindow.Hide();
    var menu = new MenuItem[]
    {
        new MenuItem
        {
            Label = "Show",
            Click = () =>
            {
                Electron.Tray.Destroy();
                browserWindow.Show();
            }
        },
        new MenuItem
        {
            Label = "Quit",
            Click = () => Electron.App.Quit()
        }
    };

    Electron.Tray.Show("favicon.ico", menu);
    Electron.Tray.SetToolTip("Electron Demo in the tray.");
}
Originally created by @AykutToprak on GitHub (May 6, 2020). Originally assigned to: @GregorBiswanger on GitHub. 🚨 The issue tracker is not for questions 🚨 How to hide browser window and put application in Tray mode on close button clicked? I tried as it shown below; `browserWindow.OnClose += () => OnClose(browserWindow);` and OnClose action; <pre><code> private static void OnClose(BrowserWindow browserWindow) { browserWindow.Hide(); var menu = new MenuItem[] { new MenuItem { Label = "Show", Click = () => { Electron.Tray.Destroy(); browserWindow.Show(); } }, new MenuItem { Label = "Quit", Click = () => Electron.App.Quit() } }; Electron.Tray.Show("favicon.ico", menu); Electron.Tray.SetToolTip("Electron Demo in the tray."); } </code></pre>
claunia added the question label 2026-01-29 16:40:48 +00:00
Author
Owner

@GregorBiswanger commented on GitHub (May 6, 2020):

please see how to do this in native electron and try our API:
https://www.likeanswer.com/question/19825

Please let me know if everything worked out for you.

@GregorBiswanger commented on GitHub (May 6, 2020): please see how to do this in native electron and try our API: https://www.likeanswer.com/question/19825 Please let me know if everything worked out for you.
Author
Owner

@AykutToprak commented on GitHub (May 6, 2020):

Ekran Alıntısı

Using browserWindow in OnClose event give error as it can be seen in above image.
In my opinion the difference between native code is event.preventDefault(); of onClose event.

browserWindow has been already closed in OnClose event so that it give error.

@AykutToprak commented on GitHub (May 6, 2020): ![Ekran Alıntısı](https://user-images.githubusercontent.com/28687593/81178119-603f5200-8fb0-11ea-94c4-7b7a613408a3.JPG) Using browserWindow in OnClose event give error as it can be seen in above image. In my opinion the difference between native code is `event.preventDefault();` of onClose event. browserWindow has been already closed in OnClose event so that it give error.
Author
Owner

@GregorBiswanger commented on GitHub (May 7, 2020):

I built you a sample. You have to put a script in your window (HTML) when the window is closed. You prevent the app from closing and inform your code via IPC. There you create a system tray and hide the window.

Could that help you?

The script in your MainWindow (html):

<script>
const { ipcRenderer } = require("electron");

window.onbeforeunload = (e) => {
    e.returnValue = true;
    ipcRenderer.send("hideToSystemTray");
};
</script>

The code in startup.cs:

Task.Run(async () =>
{
    var mainWindow = await Electron.WindowManager.CreateWindowAsync();

    Electron.IpcMain.On("hideToSystemTray", (e) =>
    {
        mainWindow.Hide();

        if (Electron.Tray.MenuItems.Count == 0)
        {
            var menu = new MenuItem[]
            {
                new MenuItem
                {
                    Label = "Show Window",
                    Click = () => mainWindow.Show()
                },
                new MenuItem
                {
                    Label = "Exit",
                    Click = () => Electron.App.Exit()
                }
            };

            Electron.Tray.Show(@"..\bin\wwwroot\images\electron.png", menu);
            Electron.Tray.SetToolTip("Electron.NET Demo in the tray.");
        }
    });
});

electron net-sys-tray

@GregorBiswanger commented on GitHub (May 7, 2020): I built you a sample. You have to put a script in your window (HTML) when the window is closed. You prevent the app from closing and inform your code via IPC. There you create a system tray and hide the window. Could that help you? **The script in your MainWindow (html):** ``` <script> const { ipcRenderer } = require("electron"); window.onbeforeunload = (e) => { e.returnValue = true; ipcRenderer.send("hideToSystemTray"); }; </script> ``` **The code in startup.cs:** ``` Task.Run(async () => { var mainWindow = await Electron.WindowManager.CreateWindowAsync(); Electron.IpcMain.On("hideToSystemTray", (e) => { mainWindow.Hide(); if (Electron.Tray.MenuItems.Count == 0) { var menu = new MenuItem[] { new MenuItem { Label = "Show Window", Click = () => mainWindow.Show() }, new MenuItem { Label = "Exit", Click = () => Electron.App.Exit() } }; Electron.Tray.Show(@"..\bin\wwwroot\images\electron.png", menu); Electron.Tray.SetToolTip("Electron.NET Demo in the tray."); } }); }); ``` ![electron net-sys-tray](https://user-images.githubusercontent.com/7336300/81241404-6e21c100-900a-11ea-91b7-73dd63703e8c.gif)
Author
Owner

@AykutToprak commented on GitHub (May 7, 2020):

Thank you @GregorBiswanger .

It solved the problem.

@AykutToprak commented on GitHub (May 7, 2020): Thank you @GregorBiswanger . It solved the problem.
Author
Owner

@ruohki commented on GitHub (Oct 31, 2020):

To piggyback on this problem.

In js you can do something like this in your app.js

    mainWindow.on('close', (event) => {
      if(!shouldQuit){
        event.returnValue = true;
        event.preventDefault()
        mainWindow.hide();
      } else {
        // close app
      }
    });

I noticed the OnClose do not hand over any arguments to do the same in c#. Is this a missing feature?

@ruohki commented on GitHub (Oct 31, 2020): To piggyback on this problem. In js you can do something like this in your app.js ```js mainWindow.on('close', (event) => { if(!shouldQuit){ event.returnValue = true; event.preventDefault() mainWindow.hide(); } else { // close app } }); ``` I noticed the OnClose do not hand over any arguments to do the same in c#. Is this a missing feature?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#479