May 25, 2021 Electron
The BrowserWindow class gives you the power to create a browser window. For example:
// In the main process. const BrowserWindow = require('electron').BrowserWindow; // Or in the renderer process. const BrowserWindow = require('electron').remote.BrowserWindow; var win = new BrowserWindow({ width: 800, height: 600, show: false }); win.on('closed', function() { win = null; }); win.loadURL('https://github.com'); win.show();
You can also use the Frameless Window API without creating windows through chrome.
BrowserWindow is an EventEmitter .
Options allows you to create a BrowserWindow with essential properties.
The values and effects of type vary from platform to platform, specifically:
TitleBarStyle is only supported on OS X 10.10 Yosemite or later, with available values:
The webPreferences parameter is an object whose properties:
The BrowserWindow object can trigger the following events:
Note: Some events can only be triggered in a specific os environment and have been marked as much as possible.
Return:
When the document changes the title, use event.preventDefault() to prevent the title of the original window from changing.
Return:
Triggered when the window is about to close. I t is triggered before the DOM's beforeunload and unload events. Use event.preventDefault() to cancel this operation
Usually you want to decide whether to close the window through the beforeunload processor, but it is also triggered when the window is overloaded. I n Electron, returning an empty string or false can cancel the shutdown. For example:
window.onbeforeunload = function(e) { console.log('I do not want to be closed'); // Unlike usual browsers, in which a string should be returned and the user is // prompted to confirm the page unload, Electron gives developers more options. // Returning empty string or false would prevent the unloading now. // You can also use the dialog API to let the user confirm closing the application. e.returnValue = false; };
Triggered when the window is closed. When you receive this event, you should remove the reference object to the closed window and avoid using it again.
Trigger an event when the interface is stuck.
Triggered when interface recovery is stuck.
Triggered when the window loses focus.
Triggers when the window gets focus.
Triggered when the window is maximized.
Triggered when window exit is maximized.
Triggered when the window is minimized.
Triggers when the window recovers from minimization.
Triggered when the window size changes.
Triggered as the window moves.
Note: Alias moved in OS X .
Triggered as the window moves.
Triggered when the window enters full screen.
Triggered when the window exits full screen.
Triggered when the window enters full screen through html api.
Triggered when the window exits full screen through html api.
Triggered when an App Command .aspx) is requested. Typically keyboard media or browser commands, the "Back" button on Windows will also be triggered as a mouse.
someWindow.on('app-command', function(e, cmd) { // Navigate the window back when the user hits their mouse back button if (cmd === 'browser-backward' && someWindow.webContents.canGoBack()) { someWindow.webContents.goBack(); } });
Triggered at the beginning of the scroll bar event.
Triggered at the end of the scroll bar event.
The BrowserWindow object has the following methods:
Returns an array of all objects that have windows open.
Returns to the app's current focus window, or null if not .
Find windows according to webContents.
Find windows based on id.
Add the developer toolbar extension at path and return the name of the extension.
This extension will be added to history so only need to use this API once and this api cannot be used for programming purposes.
Remove the extension of the developer toolbar name name name.
Instance objects created with new BrowserWindow have the following properties:
// In this example `win` is our instance var win = new BrowserWindow({ width: 800, height: 600 });
This window's WebContents object, through which all interface-related events and methods are done.
View webContents documentation methods and events.
The unique id of the window.
Instance objects created with new BrowserWindow are available in the following ways:
Note: Some methods can only be called in a specific os environment and have been landmarkd as much as possible.
Force close window, unload and beforeunload will not fire, and close will not fire, but it guarantees closed trigger.
Try closing the window as if the user had clicked the close button. Although the page may be de-closed, view close event .
The window gets focus.
Back to boolean, does the window get the focus?
Show and focus the window.
Show window but don't get focus.
Hide the window.
Back to boolean, whether the window is visible.
The window maximizes.
Cancel window maximization.
Back to boolean, whether the window is maximized.
Window minimization. In some os, it will be displayed in the dock.
Restore the minimized window to its previous state.
Back to boolean, whether the window is minimized.
Set whether the screen is full.
Back to boolean, whether the window is fully screened.
A window maintains the aspect ratio. ExtraSize allows developers to use it in pixels and is not included in aspectRatio. This API can be used to distinguish between the size of a window and the size of the content.
Imagine a normal controlled HD video player window. I f the left edge has 15 control pixels, the right edge has 25 control pixels, and under the player there are 50 control pixels. I n order to maintain a 16:9 aspect ratio within the player, we can call this api incoming parameters 16/9 and . Just add up all the extra height and width of the page.
Reset the width value of the window and move to the specified x, y position.
Returns an object that contains the width, height, x coordinates, and y coordinates of the window.
Reset the width value of the window.
Returns an array that contains the width and height of the window.
Reset the width value of the window client (such as the web interface).
Returns an array that contains the width and height of the window client.
Set the width and height value of the window minimization.
Returns an array that contains the width and height of the window minimization.
Set the width and height value of the window maximization.
Returns an array that contains the maximum width and height of the window.
Set whether the window can be changed by the user size.
Back to boolean, whether the window can be changed by the user size.
Set whether the window can be dragged by the user. Linux is not valid.
Back to boolean, whether the window can be dragged by the user. Linux always returns true .
Set whether the window can be minimized. Linux is not valid.
Back to boolean, whether the window can be minimized. Linux always returns true .
Set whether the window can be maximized. Linux is not valid.
Back to boolean, whether the window can be maximized. Linux always returns true .
Set whether the click maximize button can be full screen or maximize the window.
Return to boolean and click on the maximize button whether the full screen or maximize the window.
Set whether the window can be closed artificially. Linux is not valid.
Back to boolean, whether the window can be artificially closed. Linux always returns true .
Whether to set this window is always on top of other windows. After setting up, this window is still an ordinary window, not a toolbox window that does not get focus.
Return to boolean, whether the current window is always before other windows.
The window is centered.
Move the window to the corresponding x and y coordinates.
Returns an array containing the current window position.
Change the title of the original window.
Return to the title of the original window.
Note: Interface title may not be the same as window title.
Start or stop the display window to get the user's attention.
Let the window not appear in the taskbar.
Enter or leave kiosk mode.
Return to boolean whether to enter or leave kiosk mode.
Returns the handle to the window for this particular platform in buffer form.
The handle type on windows is HWND, OS X NSView, Linux Window .
Block windows messages and trigger callback functions when WndProc receives messages.
Return true or false to indicate whether the message has been intercepted.
Do not block window messages.
Window messages are not blocked at all.
Set the current file path of the window and place the icon of the file on the window title bar.
Gets the window's current file path.
Make it clear whether the window document can be edited and, if so, turn the icon of the title bar gray.
Back to boolean, whether the current window document is editable.
Capture a snapshot of the page in the rect. W hen complete, the callback function callback is called and the image . Image is an instance of NativeImage that stores snapshot information. If you do not set rect, all visible page will be captured.
Similar to webContents.print
Similar to webContents.printToPDF (options, callback).
Similar to webContents.loadURL (url, options). .
Similar to webContents.reload .
Setting the menu of the menu bar and setting it to null means that the menu bar is not set.
Set the progress value in the progress bar, with a valid range of 0, 1.0.
The progress is not displayed when the progress is less than 0; The result is uncertain when the progress is greater than 0.
On libix, only unity desktop environments are supported, and you need to indicate the .desktop file and add the file name to package.json. By default, it is app.getName().desktop .
Add a 16 x 16 pixel icon to the current taskbar, usually to override the status of some apps or to prompt the user directly.
Set whether the window should have shadows. Invalid in Windows and Linux systems.
Go back to boolean and set whether the window has shadows. True is always returned on Windows and Linux systems .
The taskbar button layout in the window is out to add a thumbnail toolbar with a special button. Return a Boolean object to indicate whether this thumbnail toolbar was successfully added.
Because of space, the number of buttons on the thumbnail toolbar should not exceed 7. O nce set, you can't move it because of platform restrictions. But you can use an empty array to call the api to clear the buttons .
All buttons are an array of Button objects:
Flags is an array that contains the following Strings:
A pop-up dictionary is displayed Chinese the interface to find the selected words.
Set whether the window's menu bar can be hidden automatically. Once set, it is displayed only when the user presses the Alt key.
If the menu bar is already visible, calling setAutoHideMenuBar (true) is not immediately hidden.
Back to boolean, whether the window's menu bar can be automatically hidden.
Set whether the menu bar is visible. If the menu bar is automatically hidden, the user can still press the Alt key to display.
Back to boolean, whether the menu bar is visible.
Set whether the window is visible everywhere.
Note: This api is not valid in windows.
Return to boolean and see if the window is visible everywhere.
Note: False is always returned on windows.
Ignore all mouse events in the window.