BrowserWindow
å建并æ§å¶æµè§å¨çªå£ã
Process: Main
å¨ app 模å emitted ready äºä»¶ä¹åï¼æ¨ä¸è½ä½¿ç¨æ¤æ¨¡åã
// å¨ä¸»è¿ç¨ä¸.
const { BrowserWindow } = require('electron')
const win = new BrowserWindow({ width: 800, height: 600 })
// Load a remote URL
win.loadURL('https://github.com')
// Or load a local HTML file
win.loadFile('index.html')
èªå®ä¹çªå£â
BrowserWindow ç±»æ´é²äºåç§æ¹æ³æ¥ä¿®æ¹åºç¨çªå£çå¤è§åè¡ä¸ºã For more details, see the Window Customization tutorial.
ä¼é å°æ¾ç¤ºçªå£â
æ¯æ¬¡å 载页é¢é½æ¯ç´æ¥å±ç¤ºï¼ç¨æ·çªç¶å°±çå°äºï¼è¿ä¸æ¯ä¸ä¸ªå¥½çæ¬å°åºç¨ä½¿ç¨ä½éª è¦ä½¿çªå£æ¾ç¤ºæ¶æ²¡æè§è§éªçï¼å¯¹äºä¸åæ 嵿䏤ç§è§£å³æ¹æ¡ã
ä½¿ç¨ ready-to-show äºä»¶â
å¨å è½½é¡µé¢æ¶ï¼æ¸²æè¿ç¨ç¬¬ä¸æ¬¡å®æç»å¶æ¶ï¼å¦æçªå£è¿æ²¡æè¢«æ¾ç¤ºï¼æ¸²æè¿ç¨ä¼ååº ready-to-show äºä»¶ ã 卿¤äºä»¶åæ¾ç¤ºçªå£å°æ²¡æè§è§éªçï¼
const { BrowserWindow } = require('electron')
const win = new BrowserWindow({ show: false })
win.once('ready-to-show', () => {
win.show()
})
è¿ä¸ªäºä»¶éå¸¸å¨ did-finish-load äºä»¶ä¹åååºï¼ä½æ¯é¡µé¢æè®¸å¤è¿ç¨èµæºæ¶ï¼å®å¯è½ä¼å¨ did-finish-loadä¹åååºäºä»¶ã
请注æï¼ä½¿ç¨æ¤äºä»¶æå³ç渲æå¨ä¼è¢«è®¤ä¸ºæ¯"å¯è§ç"å¹¶ç»å¶ï¼å³ä½¿ show æ¯falseã 妿æ¨ä½¿ç¨ paintWhenInitiallyHidden: falseï¼æ¤äºä»¶å°æ°¸è¿ä¸ä¼è¢«è§¦åã
设置 backgroundColor 屿§â
对äºä¸ä¸ªå¤æçåºç¨ï¼ready-to-show å¯è½ååºç太æï¼ä¼è®©åºç¨æè§ç¼æ
¢ã å¨è¿ç§æ
åµä¸ï¼å»ºè®®ç«å»æ¾ç¤ºçªå£ï¼å¹¶ä½¿ç¨æ¥è¿åºç¨ç¨åºèæ¯ç backgroundColor
const { BrowserWindow } = require('electron')
const win = new BrowserWindow({ backgroundColor: '#2e2c29' })
win.loadURL('https://github.com')
请注æï¼å³ä½¿å¯¹äºä½¿ç¨ ready-to-show äºä»¶çåºç¨ï¼ä»å»ºè®® 设置 backgroundColorï¼ä»¥ä½¿åºç¨æè§æ´æ¥è¿åçã
ä¸äºå
æ¬ backgroundColor çææå¼çä¾åï¼
const win = new BrowserWindow()
win.setBackgroundColor('hsl(230, 100%, 50%)')
win.setBackgroundColor('rgb(255, 145, 145)')
win.setBackgroundColor('#ff00a3')
win.setBackgroundColor('blueviolet')
æå ³è¿äºé¢è²ç±»åçææé项ï¼è¯·åé win.setBackgroundColorã
ç¶åçªå£â
éè¿ä½¿ç¨ parent é项ï¼ä½ å¯ä»¥å建åçªå£ï¼
const { BrowserWindow } = require('electron')
const top = new BrowserWindow()
const child = new BrowserWindow({ parent: top })
child.show()
top.show()
child çªå£å°æ»æ¯æ¾ç¤ºå¨ top çªå£çé¡¶é¨.
模æçªå£â
模æçªå£æ¯ç¦ç¨ç¶çªå£çåçªå£ã è¦å建模æçªå£ï¼å¿
é¡»åæ¶è®¾ç½®parentåmodal屿§ï¼
const { BrowserWindow } = require('electron')
const top = new BrowserWindow()
const child = new BrowserWindow({ parent: top, modal: true, show: false })
child.loadURL('https://github.com')
child.once('ready-to-show', () => {
child.show()
})
页é¢å¯è§æ§â
页é¢å¯è§æ§ API ç工使¹å¼å¦ä¸:
- 卿æå¹³å°ä¸, å¯è§æ§ç¶æä¸çªå£æ¯å¦éè/æå°åç¸å ³ã
- æ¤å¤, å¨ macOS ä¸, å¯è§æ§ç¶æè¿ä¼è·è¸ªçªå£ç鮿¡ç¶æã 妿çªå£è¢«å¦ä¸ä¸ªçªå£å®å
¨é®æ¡äºï¼å¯è§æ§ç¶æä¸º
hidden. å¨å ¶ä»å¹³å°ä¸ï¼å¯è§æ§ç¶æåªæå¨ä½¿ç¨win.hide()使çªå£æå°åæè éèæ¶æä¸ºhidden - 妿å建
BrowserWindowæ¶å¸¦æshow: falseçåæ°, æåçå¯è§æ§ç¶æå°ä¸ºvisible尽管çªå£å®é 䏿¯éèçã - 妿
backgroundThrottling被ç¦ç¨ï¼å¯è§æ§ç¶æå°ä¿æä¸ºvisibleå³ä½¿çªå£è¢«æå°åã鮿¡æéèã
æ¨èæ¨å¨å¯è§æ§ç¶æä¸º hidden æ¶æåæ¶èèµæºçæä½ä»¥ä¾¿åå°çµåæ¶èã
å¹³å°ç¸å ³çæç¤ºâ
- å¨ macOS ä¸ï¼modal çªå£å°æ¾ç¤ºä¸ºéå å°ç¶çªå£çå·¥ä½è¡¨ã
- å¨ macOS ä¸ï¼åçªå£å°ä¿æä¸ç¶çªå£çç¸å¯¹ä½ç½®ãèå¨ Windows å Linux ä¸ï¼å½ç¶çªå£ç§»å¨æ¶åçªå£ä¸ä¼ç§»å¨ã
- å¨Linuxä¸ï¼æ¨¡æçªå£çç±»åå°æ´æ¹ä¸º
dialog. - å¨Linuxä¸ï¼è®¸å¤æ¡é¢ç¯å¢ä¸æ¯æéèæ¨¡æçªå£ã
- On Wayland (Linux) it is generally not possible to programmatically resize windows after creation, or to position, move, focus, or blur windows without user input. If your app needs these capabilities, run it in Xwayland by appending the flag
--ozone-platform=x11.
Class: BrowserWindow extends BaseWindowâ
å建并æ§å¶æµè§å¨çªå£ã
Process: Main
BrowserWindow æ¯ä¸ä¸ª EventEmitterã
éè¿ options å¯ä»¥å建ä¸ä¸ªå
·æåç屿§ç BrowserWindow ã
[!WARNING] Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.
new BrowserWindow([options])â
å®ä¾äºä»¶â
ä½¿ç¨ new BrowserWindow å建ç对象å
·æä»¥ä¸å±æ§:
[!NOTE] Some events are only available on specific operating systems and are labeled as such.
äºä»¶ï¼ 'page-title-updated'â
è¿å:
eventEventtitlestringexplicitSetboolean
ææ¡£æ´æ¹æ 颿¶è§¦åï¼è°ç¨event.preventDefault()å°é»æ¢æ´æ¹æ é¢ å½æ é¢åæèªæä»¶ URL 䏿¶ï¼ explicitSet çå¼ä¸ºfalseã
äºä»¶ï¼ 'close'â
è¿å:
eventEvent
å¨çªå£è¦å
³éçæ¶å触åã å®å¨DOM çbeforeunload å unload äºä»¶ä¹å触å. è°ç¨event.preventDefault()å°é»æ¢è¿ä¸ªæä½ã
éå¸¸ä½ æ³éè¿ beforeunloadå¤ç卿¥å³å®æ¯å¦å
³éçªå£ï¼ä½æ¯å®ä¹ä¼å¨çªå£éè½½çæ¶å触å. å¨ Electron éï¼è¿åé¤ undefinedä¹å¤çä»»ä½å¼é½å°åæ¶å
³é. ä¾å¦ï¼
window.onbeforeunload = (e) => {
console.log('I do not want to be closed')
// ä¸éå¸¸çæµè§å¨ä¸å,ä¼æç¤ºç»ç¨æ·ä¸ä¸ªæ¶æ¯æ¡,
//è¿åé空å¼å°é»è®¤åæ¶å
³é
//建议使ç¨å¯¹è¯æ¡ API è®©ç¨æ·ç¡®è®¤å
³éåºç¨ç¨åº.
e.returnValue = false
}
[!NOTE] There is a subtle difference between the behaviors of
window.onbeforeunload = handlerandwindow.addEventListener('beforeunload', handler). It is recommended to always set theevent.returnValueexplicitly, instead of only returning a value, as the former works more consistently within Electron.
äºä»¶ï¼ 'closed'â
å¨çªå£å ³éæ¶è§¦å å½ä½ æ¥æ¶å°è¿ä¸ªäºä»¶çæ¶å, ä½ åºå½ç§»é¤ç¸åºçªå£çå¼ç¨å¯¹è±¡ï¼é¿å 忬¡ä½¿ç¨å®.
Event: 'query-session-end' Windowsâ
è¿å:
eventWindowSessionEndEvent
Emitted when a session is about to end due to a shutdown, machine restart, or user log-off. Calling event.preventDefault() can delay the system shutdown, though itâs generally best to respect the userâs choice to end the session. However, you may choose to use it if ending the session puts the user at risk of losing data.
äºä»¶: 'session-end' Windowsâ
è¿å:
eventWindowSessionEndEvent
Emitted when a session is about to end due to a shutdown, machine restart, or user log-off. Once this event fires, there is no way to prevent the session from ending.
äºä»¶: 'unresponsive'â
ç½é¡µå徿ªååºæ¶è§¦å
äºä»¶: 'responsive'â
æªååºç页é¢åæååºæ¶è§¦å
äºä»¶: 'blur'â
å½çªå£å¤±å»ç¦ç¹æ¶è§¦å
äºä»¶: 'focus'â
å½çªå£è·å¾ç¦ç¹æ¶è§¦å
äºä»¶: 'show'â
å½çªå£æ¾ç¤ºæ¶è§¦å
äºä»¶: 'hide'â
å½çªå£éèæ¶è§¦å
äºä»¶: 'ready-to-show'â
å½é¡µé¢å·²ç»æ¸²æå®æ(使¯è¿æ²¡ææ¾ç¤º) å¹¶ä¸çªå£å¯ä»¥è¢«ç°å®æ¶è§¦å
请注æï¼ä½¿ç¨æ¤äºä»¶æå³ç渲æå¨ä¼è¢«è®¤ä¸ºæ¯"å¯è§ç"å¹¶ç»å¶ï¼å³ä½¿ show æ¯falseã 妿æ¨ä½¿ç¨ paintWhenInitiallyHidden: falseï¼æ¤äºä»¶å°æ°¸è¿ä¸ä¼è¢«è§¦åã
äºä»¶: 'maximize'â
çªå£æå¤§åæ¶è§¦å
äºä»¶: 'unmaximize'â
å½çªå£ä»æå¤§åç¶æéåºæ¶è§¦å
äºä»¶: 'minimize'â
çªå£æå°åæ¶è§¦å
äºä»¶: 'restore'â
å½çªå£ä»æå°åç¶ææ¢å¤æ¶è§¦å
äºä»¶: 'will-resize' macOS Windowsâ
è¿å:
eventEventnewBoundsRectangle - Size the window is being resized to.detailsObjectedge(string) - çªå£è¾¹ç¼è¢«æå¨ä»¥è°æ´å¤§å°ã å¼å¯ä»¥æ¯bottom,left,right,top-left,top-right,bottom-leftæbottom-right.
è°æ´çªå£å¤§å°å触åã è°ç¨ event.preventDefault() å°é»æ¢çªå£å¤§å°è°æ´ã
请注æï¼è¯¥äºä»¶ä»
卿å¨è°æ´çªå£å¤§å°æ¶è§¦åã éè¿ setBounds/setSize è°æ´çªå£å¤§å°ä¸ä¼è§¦åæ¤äºä»¶ã
edge é项çå¯ç¨å¼åè¡ä¸ºé项ä¸å¹³å°ç¸å
³ å¯è½ç弿
- å¨ Windows å¹³å°å¯ç¨å¼æ
bottom,top,left,right,top-left,top-right,bottom-left,bottom-right. - å¨ macOS, å¯ç¨å¼æ
bottomåright.bottomå¼ç¨äºè¡¨ç¤ºåç´è°æ´å¤§å°ãrightå¼ç¨äºè¡¨ç¤ºæ°´å¹³è°æ´å¤§å°ã
äºä»¶: 'resize'â
è°æ´çªå£å¤§å°å触åã
äºä»¶ï¼'resized' macOS Windowsâ
å½çªå£å®æè°æ´å¤§å°å触å䏿¬¡ã
è¿é叏卿å¨è°æ´çªå£å¤§å°å触åã å¨ macOS ç³»ç»ä¸ï¼ä½¿ç¨setBounds/setSizeè°æ´çªå£å¤§å°å¹¶å°animateåæ°è®¾ç½®ä¸ºtrueä¹ä¼å¨è°æ´å¤§å°å®æåè§¦åæ¤äºä»¶ã
äºä»¶: 'will-move' macOS Windowsâ
è¿å:
eventEventnewBoundsRectangle - Location the window is being moved to.
çªå£ç§»å¨å触åã å¨Windowsä¸ï¼è°ç¨ event.preventDefault() å°é»æ¢çªå£ç§»å¨ã
注æï¼ä»
彿å¨ç§»å¨çªå£æ¶æä¼è§¦åæ¤æ¶æ¯ã ä½¿ç¨ setPosition/setBounds/center ç§»å¨çªå£ä¸ä¼è§¦åæ¤äºä»¶ã
äºä»¶: 'move'â
çªå£ç§»å¨å°æ°ä½ç½®æ¶è§¦å
äºä»¶: 'moved' macOS Windowsâ
å½çªå£ç§»å¨å°æ°ä½ç½®æ¶è§¦å䏿¬¡
[!NOTE] On macOS, this event is an alias of
move.
äºä»¶: 'enter-full-screen'â
çªå£è¿å ¥å ¨å±ç¶ææ¶è§¦å
äºä»¶: 'leave-full-screen'â
çªå£ç¦»å¼å ¨å±ç¶ææ¶è§¦å
äºä»¶: 'enter-html-full-screen'â
çªå£è¿å ¥ç±HTML API 触åçå ¨å±ç¶ææ¶è§¦å
äºä»¶: 'leave-html-full-screen'â
çªå£ç¦»å¼ç±HTML API触åçå ¨å±ç¶ææ¶è§¦å
äºä»¶: 'always-on-top-changed'â
è¿å:
eventEventisAlwaysOnTopboolean
设置æåæ¶è®¾ç½®çªå£æ»æ¯å¨å ¶ä»çªå£ç顶鍿¾ç¤ºæ¶è§¦åã
äºä»¶ï¼ 'app-command' Windows__Linuxâ
è¿å:
eventEventcommandstring
Emitted when an App Command is invoked. å ¸åçæ¯é®çä¸çåªä½é®ææµè§å¨å½ä»¤, 以åå¨Windowsä¸çä¸äºé¼ æ ä¸å ç½®çâåéâæé®ã
å½ä»¤æ¯å°åçï¼ä¸åçº¿æ¿æ¢ä¸ºè¿å符ï¼ä»¥åAPPCOMMAND_ åç¼å°è¢«å é¤ã ä¾å¦ APPCOMMAND_BROWSER_BACKWARDå°è¢«browser-backward触å.
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()
win.on('app-command', (e, cmd) => {
// å½ç¨æ·é¼ æ åå»è¿åæé®æ¶å°èªå¨è¿åä¸ä¸ä¸ªçªå£
if (cmd === 'browser-backward' && win.webContents.canGoBack()) {
win.webContents.goBack()
}
})
以ä¸åºç¨å½ä»¤å¨ Linux 䏿æç¡®å°æ¯æï¼
browser-backwardbrowser-forward
äºä»¶: 'swipe' macOSâ
è¿å:
eventEventdirectionstring
䏿æ»å¨æ¶è§¦åã Possible directions are up, right, down, left.
æ¤äºä»¶çåºæ¬æ¹æ³æ¯ç¨æ¥å¤çæ§çmacOS飿 ¼çè§¦æ¸æ¿æ»å¨ï¼å±å¹å
容ä¸ä¼éçæ»å¨èç§»å¨ã 大夿°macOSè§¦æ¸æ¿é½ä¸åå
许é
ç½®è¿æ ·çæ»å¨ï¼å æ¤ä¸ºäºæ£ç¡®å°è§¦å该äºä»¶ï¼éå°System Preferences > Trackpad > More Gesturesä¸'Swipe between pages'é¦é项设置为'Swipe with two or three fingers'ã
äºä»¶: 'rotate-gesture' macOSâ
è¿å:
eventEventrotationFloat
å¨è§¦æ§æ¿æè½¬æå¿ä¸è§¦åã æç»è§¦åç´å°æè½¬æå¿ç»æã æ¯æ¬¡è§¦åç rotation 弿¯èªä¸æ¬¡è§¦åä»¥æ¥æè½¬çè§åº¦ã æè½¬æå¿æå䏿¬¡è§¦åçäºä»¶å¼æ°¸è¿æ¯0ã éæ¶éæè½¬å¼ä¸ºæ£å¼ï¼é¡ºæ¶éæè½¬å¼ä¸ºè´å¼ã
äºä»¶: 'sheet-begin' macOSâ
çªå£æå¼sheet(å·¥ä½è¡¨) æ¶è§¦å
äºä»¶: 'sheet-end' macOSâ
çªå£å ³ésheet(å·¥ä½è¡¨) æ¶è§¦å
äºä»¶: 'new-window-for-tab' macOSâ
å½ç¨æ·ç¹å»åççmacOSæ°æ ç¾æé®æ¶è§¦åã The new tab button is only visible if the current BrowserWindow has a tabbingIdentifier.
You must create a window in this handler in order for macOS tabbing to work as expected.
Event: 'system-context-menu' Windows Linuxâ
è¿å:
eventEventpointPoint - The screen coordinates where the context menu was triggered.
å½ç³»ç»ä¸ä¸æèåå¨çªå£ä¸è§¦åæ¶ååºï¼ é常åªå¨ç¨æ·å³é®ç¹å»ä½ çªå£çé客æ·ç«¯åºåæ¶è§¦åã é客æ·ç«¯åºåæçæ¯çªå£æ 颿 ææ è¾¹æ¡çªå£ä¸è¢«ä½ 声æä¸º -webkit-app-region: drag çä»»æåºåã
è°ç¨ event.preventDefault() å°é»æ¢èåæ¾ç¤ºã
To convert point to DIP, use screen.screenToDipPoint(point).
éææ¹æ³â
BrowserWindow ç±»æä»¥ä¸æ¹æ³:
BrowserWindow.getAllWindows()â
è¿å BrowserWindow[] - æææå¼ççªå£çæ°ç»
BrowserWindow.getFocusedWindow()â
è¿å BrowserWindow | null - æ¤åºç¨ç¨åºä¸å½åè·å¾ç¦ç¹ççªå£ï¼å¦ææ å°±è¿å null.
BrowserWindow.fromWebContents(webContents)â
webContentsWebContents
è¿å BrowserWindow | null - è¿åæ¥æç»å® webContentsççªå£ï¼å¦å妿å
容ä¸å±äºä¸ä¸ªçªå£ï¼è¿ånullã
BrowserWindow.fromBrowserView(browserView) Deprecatedâ
browserViewBrowserView
[!NOTE] The
BrowserViewclass is deprecated, and replaced by the newWebContentsViewclass.
è¿å BrowserWindow | null - æ¥æç»å® browserView ççªå£ã 妿ç»å®çè§å¾æ²¡æéå å°ä»»ä½çªå£ï¼è¿å nullã
BrowserWindow.fromId(id)â
idInteger
è¿å BrowserWindow | null - 带æç»å® id ççªå£ã
å®ä¾å±æ§â
ä½¿ç¨ new BrowserWindow å建ç对象å
·æä»¥ä¸å±æ§:
const { BrowserWindow } = require('electron')
// æ¬ä¾ä¸ `win` æ¯æä»¬çå®ä¾
const win = new BrowserWindow({ width: 800, height: 600 })
win.loadURL('https://github.com')
win.webContents åªè¯»â
æ¤çªå£æ¥æç WebContents å¯¹è±¡ã ææä¸ç½é¡µç¸å
³çäºä»¶åæä½é½å°éè¿å®å®æã
See the webContents documentation for its methods and events.
win.id åªè¯»â
ä¸ä¸ª Integer 屿§ä»£è¡¨äºçªå£çå¯ä¸IDã æ¯ä¸ªID卿´ä¸ªElectronåºç¨ç¨åºçææ BrowserWindow å®ä¾ä¸é½æ¯å¯ä¸çã
win.tabbingIdentifier macOS åªè¯»â
A string (optional) property that is equal to the tabbingIdentifier passed to the BrowserWindow constructor or undefined if none was set.
win.autoHideMenuBar Linux Windowsâ
ä¸ä¸ª boolean 屿§å³å®çªå£èåæ æ¯å¦èªå¨éèã 䏿¦è®¾ç½®ï¼èåæ å°åªå¨ç¨æ·åå» Alt 鮿¶æ¾ç¤ºã
妿èåæ å·²ç»å¯è§ï¼å°è¯¥å±æ§è®¾ç½®ä¸º true å°ä¸ä¼ä½¿å
¶ç«å»éèã
win.simpleFullScreenâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦å¤äºç®å(pre-Lion) å
¨å±æ¨¡å¼ã
win.fullScreenâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦å¤äºå
¨å±æ¨¡å¼ã
win.focusable Windows macOSâ
ç¡®å®çªå£æ¯å¦å¯ä½ä¸ºç¦ç¹è¢«éä¸ç boolean 屿§ã
win.visibleOnAllWorkspaces macOS Linuxâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦å¨ææå·¥ä½åºä¸å¯è§ã
[!NOTE] Always returns false on Windows.
win.shadowâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦æ¾ç¤ºé´å½±ã
win.menuBarVisible Windows Linuxâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®èåæ æ¯å¦å¯è§ã
[!NOTE] If the menu bar is auto-hide, users can still bring up the menu bar by pressing the single
Altkey.
win.kioskâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦å¤äºkiosk模å¼ã
win.documentEdited macOSâ
ä¸ä¸ª boolean 屿§ææçªå£ææ¡£æ¯å¦å·²è¢«ç¼è¾ã
å½è®¾ç½®ä¸º true æ¶ï¼æ 颿 ç徿 å°åç°ã
win.representedFilename macOSâ
ä¸ä¸ª string 屿§ï¼ç¨äºç¡®å®çªå£ä»£è¡¨çæä»¶çè·¯å¾åï¼æä»¶ç徿 å°æ¾ç¤ºå¨çªå£çæ 颿 ä¸ã
win.titleâ
ä¸ä¸ª string 屿§ï¼ç¨äºç¡®å®åççªå£çæ é¢ã
[!NOTE] The title of the web page can be different from the title of the native window.
win.minimizable macOS Windowsâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦å¯è¢«ç¨æ·æå¨æå°åã
å¨ Linux ä¸ï¼setter ä¸ä¼è¿è¡ä»»ä½æä½ï¼å°½ç®¡ getter è¿åçæ¯ trueã
win.maximizable macOS Windowsâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦å¯è¢«ç¨æ·æå¨æå¤§åã
å¨ Linux ä¸ï¼setter ä¸ä¼è¿è¡ä»»ä½æä½ï¼å°½ç®¡ getter è¿åçæ¯ trueã
win.fullScreenableâ
ä¸ä¸ª boolean 屿§ï¼å³å®æå¤§å/缩æ¾çªå£æé®æ¯åæ¢å
¨å±æ¨¡å¼è¿æ¯æå¤§åçªå£ã
win.resizableâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦å¯è¢«ç¨æ·æå¨è°æ´å¤§å°ã
win.closable macOS Windowsâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦å¯è¢«ç¨æ·æå¨å
³éã
å¨ Linux ä¸ï¼setter ä¸ä¼è¿è¡ä»»ä½æä½ï¼å°½ç®¡ getter è¿åçæ¯ trueã
win.movable macOS Windowsâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦å¯è¢«ç¨æ·ç§»å¨ã
å¨ Linux ä¸ï¼setter ä¸ä¼è¿è¡ä»»ä½æä½ï¼å°½ç®¡ getter è¿åçæ¯ trueã
win.excludedFromShownWindowsMenu macOSâ
ä¸ä¸ª boolean 屿§ï¼ç¨äºå³å®çªå£æ¯å¦ä»åºç¨ç¨åºç Windows èåæé¤ã é»è®¤å¼ä¸º false
const win = new BrowserWindow({ height: 600, width: 600 })
const template = [
{
role: 'windowmenu'
}
]
win.excludedFromShownWindowsMenu = true
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
win.accessibleTitleâ
ä¸ä¸ª string 屿§ï¼å®ä¹ä¸ä¸ªä»
为å¦å±å¹é
读å¨çè¾
å©å·¥å
·æä¾çæ¿ä»£æ é¢ ã æ¤å符串ä¸ç´æ¥å¯¹ç¨æ·å¯è§ã
win.snapped Windows Readonlyâ
A boolean property that indicates whether the window is arranged via Snap.
å®ä¾æ¹æ³â
ä½¿ç¨ new BrowserWindow å建ç对象å
·æä»¥ä¸å®ä¾æ¹æ³:
[!NOTE] Some methods are only available on specific operating systems and are labeled as such.
win.destroy()â
强å¶å
³éçªå£, é¤äºclosedä¹å¤ï¼closeï¼unload å beforeunload é½ä¸ä¼è¢«è§¦å
win.close()â
å°è¯å ³éçªå£ã è¯¥æ¹æ³ä¸ç¨æ·æå¨åå»çªå£çå ³éæé®ææç¸åã ä½ç½é¡µå¯è½ä¼åæ¶è¿ä¸ªå ³éæä½ã See the close event.
win.focus()â
èç¦äºçªå£
On Wayland (Linux), the desktop environment may show a notification or flash the app icon if the window or app is not already focused.
win.blur()â
åæ¶çªå£çèç¦
Not supported on Wayland (Linux).
win.isFocused()â
è¿å boolean - 夿çªå£æ¯å¦èç¦
win.isDestroyed()â
è¿å boolean -夿çªå£æ¯å¦è¢«éæ¯
win.show()â
æ¾ç¤ºå¹¶èç¦äºçªå£
win.showInactive()â
æ¾ç¤ºä½ä¸èç¦äºçªå£
Not supported on Wayland (Linux).
win.hide()â
éèçªå£
win.isVisible()â
è¿åboolean - çªå£æ¯å¦å¨åºç¨åå°å¯¹ç¨æ·å¯è§ã
win.isModal()â
è¿å boolean - 夿æ¯å¦ä¸ºæ¨¡æçªå£
win.maximize()â
æå¤§åçªå£ã 妿çªå£å°æªæ¾ç¤ºï¼è¯¥æ¹æ³ä¹ä¼å°å ¶æ¾ç¤º (ä½ä¸ä¼èç¦)ã
win.unmaximize()â
åæ¶çªå£æå¤§å
win.isMaximized()â
è¿å boolean - 夿çªå£æ¯å¦æå¤§å
win.minimize()â
æå°åçªå£ã 卿äºå¹³å°ä¸, æå°åççªå£å°æ¾ç¤ºå¨Dockä¸ã
win.restore()â
å°çªå£ä»æå°åç¶ææ¢å¤å°ä»¥åçç¶æã
win.isMinimized()â
è¿å boolean -夿çªå£æ¯å¦æå°å
win.setFullScreen(flag)â
flagboolean
设置çªå£æ¯å¦åºå¤äºå ¨å±æ¨¡å¼ã
[!NOTE] On macOS, fullscreen transitions take place asynchronously. If further actions depend on the fullscreen state, use the 'enter-full-screen' or 'leave-full-screen' events.
win.isFullScreen()â
è¿å boolean - çªå£å½åæ¯å¦å·²å
¨å±
[!NOTE] On macOS, fullscreen transitions take place asynchronously. When querying for a BrowserWindow's fullscreen status, you should ensure that either the 'enter-full-screen' or 'leave-full-screen' events have been emitted.
win.setSimpleFullScreen(flag) macOSâ
flagboolean
è¿å ¥æç¦»å¼ç®åçå ¨å±æ¨¡å¼ã
ç®åå ¨å±æ¨¡å¼æ¨¡æäº Lion (10.7) ä¹åçmacOSçæ¬ä¸çåçå ¨å±è¡ä¸ºã
win.isSimpleFullScreen() macOSâ
è¿å boolean - çªå£æ¯å¦ä¸ºç®åå
¨å±æ¨¡å¼(pre-Lion)ã
win.isNormal()â
è¿å boolean - çªå£æ¯å¦å¤äºæ£å¸¸ç¶æï¼æªæå¤§åï¼æªæå°åï¼ä¸å¨å
¨å±æ¨¡å¼ä¸ï¼ã
win.setAspectRatio(aspectRatio[, extraSize])â
aspectRatioFloat- 为å 容è§å¾ä¿æç宽髿¯.extraSizeSize (optional) macOS - The extra size not to be included while maintaining the aspect ratio.
è¿å°ä½¿çªå£ä¿æé¿å®½æ¯ã é¢å¤ç大å°å 许å¼å人åæç©ºé´ (以åç´ ä¸ºåä½), ä¸å æ¬å¨çºµæ¨ªæ¯è®¡ç®ä¸ã æ¤ API å·²ç»èèäºçªå£å¤§å°åå 容大å°ä¹é´çå·®å¼ã
æ³è±¡ä¸ä¸ªä½¿ç¨é«æ¸ è§é¢ææ¾å¨åç¸å ³æ§ä»¶çæ®éçªå£ã ååå¦å·¦è¾¹ç¼æ15px, å³è¾¹ç¼æ25px, 卿æ¾å¨ä¸é¢æ50px. 为äºå¨ææ¾å¨å é¨ä¿æ 16:9 ç宽髿¯(HD @1920x1080 çæ å宽髿¯) ï¼æä»¬å°æ¤å½æ°çåæ°è®¾ç½®ä¸º 16/9 å { width: 40, height: 50 }ã 第äºä¸ªåæ°ä¸ç®¡ç½é¡µä¸çé¢å¤ç宽度åé«åº¦å¨ä»ä¹ä½ç½®, åªè¦å®ä»¬åå¨å°±è¡. å¨å ¨é¨å é¨çªå£ä¸ï¼å ä¸ä»»ä½é¢å¤ç宽度åé«åº¦ ã
å½çªå£ä½¿ç¨ç±»ä¼¼äº win.setSize è¿æ ·ç API è°æ´çªå£æ¶ï¼å®½é«æ¯ä¸ä¼è¢«éç¨ã
To reset an aspect ratio, pass 0 as the aspectRatio value: win.setAspectRatio(0).
win.setBackgroundColor(backgroundColor)â
backgroundColorstring - é¢è²ï¼æ ¼å¼ä¸º Hexï¼RGBï¼RGBAï¼HSLï¼HSLA æ CSS å½åé¢è²ã å¯¹äº Hex ç±»åï¼éæéé为å¯éçã
ææç backgroundColor å¼çä¾åï¼
- Hex
- #fff (shorthand RGB)
- #ffff (shorthand ARGB)
- #ffffff (RGB)
- #ffffffff (ARGB)
- RGB
rgb\(([\d]+),\s*([\d]+),\s*([\d]+)\)- e.g. rgb(255, 255, 255)
- RGBA
rgba\(([\d]+),\s*([\d]+),\s*([\d]+),\s*([\d.]+)\)- e.g. rgba(255, 255, 255, 1.0)
- HSL
hsl\((-?[\d.]+),\s*([\d.]+)%,\s*([\d.]+)%\)- e.g. hsl(200, 20%, 50%)
- HSLA
hsla\((-?[\d.]+),\s*([\d.]+)%,\s*([\d.]+)%,\s*([\d.]+)\)- e.g. hsla(200, 20%, 50%, 0.5)
- Color name
- åç§°åè¡¨å¨ SkParseColor.cpp
- 类似 CSS Color Module Level 3 å
³é®åï¼ä½å¤§å°åææã
- ä¾å¦
bluevioletæredã
- ä¾å¦
设置çªå£çèæ¯é¢è²ã See Setting backgroundColor.
win.previewFile(path[, displayName]) macOSâ
pathstring -è¦ç¨ QuickLook é¢è§çæä»¶çç»å¯¹è·¯å¾ã è¿ä¸ç¹å¾éè¦ï¼å 为Quick Look 使ç¨äºè·¯å¾ä¸çæä»¶ååæä»¶æ©å±å æ¥å³å®è¦æå¼çæä»¶çå 容类åãdisplayNamestring (å¯é) - å¨Quick Look 模æè§å¾ä¸æ¾ç¤ºçæä»¶çåç§°ã è¿å®å ¨æ¯è§è§çï¼ä¸ä¼å½±åæä»¶çå 容类åã é»è®¤å¼ä¸ºpath.
Uses Quick Look to preview a file at a given path.
win.closeFilePreview() macOSâ
Closes the currently open Quick Look panel.
win.setBounds(bounds[, animate])â
boundsPartial<Rectangle>animateboolean (å¯é) macOS
éç½®çªå£ï¼å¹¶ä¸ç§»å¨çªå£å°æå®çä½ç½®. 任使ªæä¾ç屿§å°é»è®¤ä¸ºå ¶å½åå¼ã
On Wayland (Linux), has the same limitations as setSize and setPosition.
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()
// è®¾ç½®ææ bounds è¾¹ç屿§
win.setBounds({ x: 440, y: 225, width: 800, height: 600 })
// 设置åä¸ bounds è¾¹ç屿§
win.setBounds({ width: 100 })
// { x: 440, y: 225, width: 100, height: 600 }
console.log(win.getBounds())
[!NOTE] On macOS, the y-coordinate value cannot be smaller than the Tray height. The tray height has changed over time and depends on the operating system, but is between 20-40px. Passing a value lower than the tray height will result in a window that is flush to the tray.
win.getBounds()â
Returns Rectangle - The bounds of the window as Object.
[!NOTE] On macOS, the y-coordinate value returned will be at minimum the Tray height. For example, calling
win.setBounds({ x: 25, y: 20, width: 800, height: 600 })with a tray height of 38 means thatwin.getBounds()will return{ x: 25, y: 38, width: 800, height: 600 }.
[!NOTE] On Wayland, this method will return
{ x: 0, y: 0, ... }as introspecting or programmatically changing the global window coordinates is prohibited.
win.getBackgroundColor()â
è¿å string - æ ¼å¼è·åçªå£çèæ¯è²ï¼æ ¼å¼ä¸º Hex (#RRGGBB)ã
[!NOTE] The alpha value is not returned alongside the red, green, and blue values.
win.setContentBounds(bounds[, animate])â
boundsRectangleanimateboolean (å¯é) macOS
è°æ´çªå£çå·¥ä½åº (å¦ç½é¡µ) ç大å°å¹¶å°å ¶ç§»å¨å°ææä¾çè¾¹çã
On Wayland (Linux), has the same limitations as setContentSize and setPosition.
win.getContentBounds()â
Returns Rectangle - The bounds of the window's client area as Object.
win.getNormalBounds()â
Returns Rectangle - Contains the window bounds of the normal state
[!NOTE] Whatever the current state of the window (maximized, minimized or in fullscreen), this function always returns the position and size of the window in normal state. In normal state,
getBoundsandgetNormalBoundsreturn the same Rectangle.
win.setEnabled(enable)â
enableboolean
ç¦ç¨æè å¯ç¨çªå£ã
win.isEnabled()â
è¿å boolean - çªå£æ¯å¦å¯ç¨ã
win.setSize(width, height[, animate])â
widthIntegerheightIntegeranimateboolean (å¯é) macOS
è°æ´çªå£çwidthå height. 妿 width æ height ä½äºä»»ä½è®¾å®çæå°å°ºå¯¸çº¦æï¼çªå£å°å¯¹é½å°çº¦æçæå°å°ºå¯¸ã
On Wayland (Linux), may not work as some window managers restrict programmatic window resizing.
win.getSize()â
è¿å Integer []-å
å«çªå£ç宽度åé«åº¦ã
win.setContentSize(width, height[, animate])â
widthIntegerheightIntegeranimateboolean (å¯é) macOS
å°çªå£çå·¥ä½åº (å¦ç½é¡µ) ç大å°è°æ´ä¸º width å heightã
On Wayland (Linux), may not work as some window managers restrict programmatic window resizing.
win.getContentSize()â
è¿å Integer []-å
å«çªå£ç宽度åé«åº¦ã
win.setMinimumSize(width, height)â
widthIntegerheightInteger
设置çªå£æå°åç width åheight.
win.getMinimumSize()â
è¿å Integer []-å
å«çªå£æå°åç宽度åé«åº¦ã
win.setMaximumSize(width, height)â
widthIntegerheightInteger
设置çªå£æå¤§åç width å height.
win.getMaximumSize()â
è¿å Integer []-å
å«çªå£æå¤§åç宽度åé«åº¦ã
win.setResizable(resizable)â
resizableboolean
è®¾ç½®ç¨æ·æ¯å¦å¯ä»¥æå¨è°æ´çªå£å¤§å°ã
win.isResizable()â
è¿å boolean - ç¨æ·æ¯å¦å¯ä»¥æå¨è°æ´çªå£å¤§å°ã
win.setMovable(movable) macOS Windowsâ
movableboolean
è®¾ç½®ç¨æ·æ¯å¦å¯ä»¥ç§»å¨çªå£ã å¨Linuxä¸ä¸èµ·ä½ç¨ã
win.isMovable() macOS Windowsâ
è¿å boolean - çªå£æ¯å¦å¯ä»¥è¢«ç¨æ·æå¨
å¨ Linux 䏿»æ¯è¿å trueã
win.setMinimizable(minimizable) macOS Windowsâ
minimizableboolean
è®¾ç½®ç¨æ·æ¯å¦å¯ä»¥æå¨å°çªå£æå°åã å¨Linuxä¸ä¸èµ·ä½ç¨ã
win.isMinimizable() macOS Windowsâ
è¿å boolean - ç¨æ·æ¯å¦å¯ä»¥æå¨æå°åçªå£ã
å¨ Linux 䏿»æ¯è¿å trueã
win.setMaximizable(maximizable) macOS Windowsâ
maximizableboolean
è®¾ç½®ç¨æ·æ¯å¦å¯ä»¥æå¨æå¤§åçªå£ã å¨Linuxä¸ä¸èµ·ä½ç¨ã
win.isMaximizable() macOS Windowsâ
è¿å boolean - çªå£æ¯å¦å¯ä»¥æå¤§å.
å¨ Linux 䏿»æ¯è¿å trueã
win.setFullScreenable(fullscreenable)â
fullscreenableboolean
设置æå¤§å/缩æ¾çªå£æé®æ¯åæ¢å ¨å±æ¨¡å¼è¿æ¯æå¤§åçªå£ã
win.isFullScreenable()â
è¿å boolean - æå¤§å/缩æ¾çªå£æé®æ¯åæ¢å
¨å±æ¨¡å¼è¿æ¯æå¤§åçªå£ã
win.setClosable(closable) macOS Windowsâ
closableboolean
è®¾ç½®ç¨æ·æ¯å¦å¯ä»¥æå¨å ³éçªå£ã å¨Linuxä¸ä¸èµ·ä½ç¨ã
win.isClosable() macOS Windowsâ
è¿å boolean - çªå£æ¯å¦è¢«ç¨æ·å
³éäº.
å¨ Linux 䏿»æ¯è¿å trueã
win.setHiddenInMissionControl(hidden) macOSâ
hiddenboolean
Sets whether the window will be hidden when the user toggles into mission control.
win.isHiddenInMissionControl() macOSâ
Returns boolean - Whether the window will be hidden when the user toggles into mission control.
win.setAlwaysOnTop(flag[, level][, relativeLevel])â
flagbooleanlevelstring (å¯é) macOS Windows - å¼å æ¬normal,floating,torn-off-menu,modal-panel,main-menu,status,pop-up-menu,screen-saverå(å·²å¼ç¨)ã å½dockflag屿§ä¸ºtrueæ¶ï¼é»è®¤å¼ä¸ºfloatingã å½flag为falseæ¶ï¼levelä¼é置为normalã 请注æï¼å æ¬ä»floatingå°statusï¼çªå£ä¼è¢«ç½®äº macOS ä¸ç Dock 䏿¹å Windows ä¸ç任塿 䏿¹ã ä»pop-up-menuå°æ´é«çº§å«ï¼çªå£æ¾ç¤ºå¨ macOS ä¸çDock䏿¹å Windows ä¸ç任塿 䏿¹ã æ´å¤ä¿¡æ¯ï¼è¯·æ¥é macOS ææ¡£ãrelativeLevelInteger (å¯é) macOS - 设置æ¤çªå£ç¸å¯¹äºç»å®çº§å«ç屿°ã. é»è®¤å¼ä¸º0. 请注æ, Apple ä¸é¼å±å¨å±å¹ä¿æ¤ç¨åºä¹ä¸è®¾ç½®é«äº1ç级å«ã
设置çªå£æ¯å¦åºå§ç»æ¾ç¤ºå¨å ¶ä»çªå£çåé¢ã 设置åï¼çªå£ä»ç¶æ¯ä¸ä¸ªæ£å¸¸çªå£ï¼è䏿¯ä¸ä¸ªæ æ³è·åç¦ç¹çå·¥å ·æ¡çªå£ã
win.isAlwaysOnTop()â
è¿å boolean - å½åçªå£æ¯å¦å§ç»å¨å
¶å®çªå£ä¹å.
win.moveAbove(mediaSourceId)â
mediaSourceIdstring - DesktopCapturerSourceæ ¼å¼ççªå£ id ã ä¾å¦ "window:1869:0"ã
å°çªå£æz轴顺åºç§»å¨å°æºçªå£åé¢ã 妿 mediaSourceId 䏿¯windowç±»åï¼æè
妿çªå£ä¸åå¨ï¼åæ¤æ¹æ³ä¼æåºä¸ä¸ªé误ã
win.moveTop()â
Moves window to top(z-order) regardless of focus.
Not supported on Wayland (Linux).
win.center()â
å°çªå£ç§»å¨å°å±å¹ä¸å¤®ã
Not supported on Wayland (Linux).
win.setPosition(x, y[, animate])â
xIntegeryIntegeranimateboolean (å¯é) macOS
Moves window to x and y.
Not supported on Wayland (Linux).
win.getPosition()â
è¿å Integer[] - è¿åä¸ä¸ªå
å«å½åçªå£ä½ç½®çæ°ç».
[!NOTE] On Wayland, this method will return
[0, 0]as introspecting or programmatically changing the global window coordinates is prohibited.
win.setTitle(title)â
titlestring
å°åççªå£çæ 颿´æ¹ä¸º titleã
win.getTitle()â
è¿å string - æ¬å°çªå£æ é¢ã
[!NOTE] The title of the web page can be different from the title of the native window.
win.setSheetOffset(offsetY[, offsetX]) macOSâ
offsetYFloatoffsetXFloat (å¯é)
æ¹åmacOSä¸sheetç»ä»¶çéçç¹ã é»è®¤æ åµä¸ï¼sheetè´´å¨çªå£è¾¹æ¡æ£ä¸æ¹ï¼ä½ä½ å¯è½éè¦å¨ HTML 渲æå·¥å ·æ 䏿¹æ¾ç¤ºå®ä»¬ã ä¾å¦ï¼
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()
const toolbarRect = document.getElementById('toolbar').getBoundingClientRect()
win.setSheetOffset(toolbarRect.height)
win.flashFrame(flag)â
History
| Version(s) | Changes |
|---|---|
None |
|
flagboolean
å¯å¨æåæ¢éªççªå£, 以å¸å¼ç¨æ·ç注æã
win.setSkipTaskbar(skip) macOS Windowsâ
skipboolean
使çªå£ä¸æ¾ç¤ºå¨ä»»å¡æ ä¸ã
win.setKiosk(flag)â
flagboolean
è¿å ¥æç¦»å¼ kiosk 模å¼ã
win.isKiosk()â
è¿å boolean - 夿çªå£æ¯å¦å¤äºkiosk模å¼.
win.isTabletMode() Windowsâ
è¿å boolean - æ 论å½åçªå£æ¯å¦å¤å¨ Windows 10 å¹³æ¿æ¨¡å¼
Since Windows 10 users can use their PC as tablet, under this mode apps can choose to optimize their UI for tablets, such as enlarging the titlebar and hiding titlebar buttons.
This API returns whether the window is in tablet mode, and the resize event can be used to listen to changes to tablet mode.
win.getMediaSourceId()â
è¿å string - DesktopCapturerSourceçidæ ¼å¼ççªå£ id ã ä¾å¦ "window:1324:0"ã
æ´ç¡®åå°è¯´ï¼æ ¼å¼æ¯ window:id:other_idãå¨Windowsä¸ id æ¯ HWND ç±»åï¼å¨macOS䏿¯ CGWindowID (uint64_t)ï¼å¨Linux䏿¯ Window (unsigned long)ã other_id ç¨äºè¯å«åä¸é¡¶å±çªå£å
çWeb å
容 (é项å¡)ã
win.getNativeWindowHandle()â
è¿å Buffer - çªå£çå¹³å°ç¹å®å¥æ
Windowsä¸å¥æç±»å为 HWNDï¼macOS ä¸ä¸º NSView*ï¼Linux ä¸ä¸ºWindow (unsigned long)
win.hookWindowMessage(message, callback) Windowsâ
messageIntegercallbackFunctionwParamBuffer - ThewParamprovided to the WndProclParamBuffer - ThelParamprovided to the WndProc
é©ä½çªå£æ¶æ¯ã 彿¶æ¯å°è¾¾ WndProc æ¶è°ç¨callback ã
win.isWindowMessageHooked(message) Windowsâ
messageInteger
è¿å boolean - true æfalse ï¼å
·ä½åå³äºæ¯å¦é©æäºæ¶æ¯.
win.unhookWindowMessage(message) Windowsâ
messageInteger
åæ¶çªå£ä¿¡æ¯çé©åã
win.unhookAllWindowMessages() Windowsâ
åæ¶ææçªå£ä¿¡æ¯çé©åã
win.setRepresentedFilename(filename) macOSâ
filenamestring
设置çªå£æä»£è¡¨çæä»¶çè·¯å¾åï¼å¹¶ä¸å°è¿ä¸ªæä»¶ç徿 æ¾å¨çªå£æ 颿 ä¸ã
win.getRepresentedFilename() macOSâ
è¿å string - è·åçªå£å½åæä»¶è·¯å¾.
win.setDocumentEdited(edited) macOSâ
editedboolean
æç¡®æåºçªå£ææ¡£æ¯å¦å¯ä»¥ç¼è¾, å¦æè®¾ç½®ä¸ºtrueåå°æ 颿 ç徿 åæç°è².
win.isDocumentEdited() macOSâ
è¿å boolean - 夿å½åçªå£ææ¡£æ¯å¦å¯ç¼è¾.
win.focusOnWebView()â
win.blurWebView()â
win.capturePage([rect, opts])â
rectRectangle (optional) - The bounds to captureoptsObject (optional)stayHiddenboolean (å¯é) - 使页é¢ä¿æéèè䏿¯å¯è§ã é»è®¤å¼ä¸ºfalse.stayAwakeboolean (å¯é) - 使系ç»ä¿æå¤éç¶æï¼è䏿¯è®©å®å¤äºç¡ç ç¶æã é»è®¤å¼ä¸ºfalse.
è¿å Promise<NativeImage> - 宿åè¿åä¸ä¸ªNativeImage
å¨ rectå
æè·é¡µé¢çå¿«ç
§ã çç¥ rect å°æè·æ´ä¸ªå¯è§é¡µé¢ã 妿页é¢ä¸å¯è§ï¼ rect å¯è½æ¯ç©ºçã The page is considered visible when its browser window is hidden and the capturer count is non-zero. å¦æä½ æ³è¦é¡µé¢ä¿æéèç¶æï¼ååºç¡®ä¿ stayHidden 设置为 trueã
win.loadURL(url[, options])â
urlstring
Returns Promise<void> - the promise will resolve when the page has finished loading (see did-finish-load), and rejects if the page fails to load (see did-fail-load). éå ä¸ä¸ªæ æä½ç rejection å¤çãå¯ä»¥é¿å
rejection æªå¤çé误ã If the existing page has a beforeUnload handler, did-fail-load will be called unless will-prevent-unload is handled.
Same as webContents.loadURL(url[, options]).
url å¯ä»¥æ¯è¿ç¨å°å (ä¾å¦ http://),ä¹å¯ä»¥æ¯ file:// åè®®çæ¬å°HTMLæä»¶çè·¯å¾.
To ensure that file URLs are properly formatted, it is recommended to use Node's url.format method:
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()
const url = require('node:url').format({
protocol: 'file',
slashes: true,
pathname: require('node:path').join(__dirname, 'index.html')
})
win.loadURL(url)
æ¨å¯ä»¥éè¿æ§è¡ä»¥ä¸æä½, 使ç¨å¸¦æç½åç¼ç æ°æ®ç POST请æ±ââå è½½ç½å:
const { BrowserWindow } = require('electron')
const win = new BrowserWindow()
win.loadURL('http://localhost:8000/post', {
postData: [{
type: 'rawData',
bytes: Buffer.from('hello=world')
}],
extraHeaders: 'Content-Type: application/x-www-form-urlencoded'
})
win.loadFile(filePath[, options])â
filePathstring
è¿å Promise<void> - å½é¡µé¢å®æå è½½å promise å°ä¼resolve (è§ did-finish-load)ï¼å¦æé¡µé¢å 载失败ï¼å reject (è§ did-fail-load)ã
ä¸ webContents.loadFileç¸åï¼ filePath åºè¯¥æ¯ä¸ä¸ªä¸ä½ çåºç¨ç¨åºçæ ¹è·¯å¾ç¸å
³çHTMLæä»¶è·¯å¾ã æå
³æ´å¤ä¿¡æ¯ï¼è¯·åé
webContents ææ¡£ã
win.reload()â
ä¸ webContents.reload ç¸å.
win.setMenu(menu) Linux Windowsâ
menuMenu | null
å° menu 设置为çªå£çèåæ ã
win.removeMenu() Linux Windowsâ
å é¤çªå£çèåæ ã
win.setProgressBar(progress[, options])â
progressDouble
设置è¿åº¦æ¡çè¿åº¦å¼ã Valid range is [0, 1.0].
å½è¿åº¦å°äº0æ¶ä¸æ¾ç¤ºè¿åº¦; å½è¿åº¦å¤§äº0æ¶æ¾ç¤ºç»æä¸ç¡®å®.
å¨ Linux å¹³å°ä¸ï¼åªæ¯æ Unity æ¡é¢æ¨¡å¼, ä½ éè¦å¨ package.json ä¸ä¸º desktopName æå® *.desktop çæä»¶å. é»è®¤æ
åµä¸ï¼å°å {app.name}.desktopã
å¨ Windows ä¸, å¯ä»¥ä¼ 鿍¡å¼ã Accepted values are none, normal, indeterminate, error, and paused. å¦ææ²¡æè®¾ç½®æ¨¡å¼ (ä½å¼å¨ææèå´å
) çæ
åµä¸è°ç¨ setProgressBar, é»è®¤å¼ä¸ºnormalã
win.setOverlayIcon(overlay, description) Windowsâ
overlayNativeImage | null - the icon to display on the bottom right corner of the taskbar icon. 妿æ¤åæ°æ¯nullï¼è¦çå±å±ä¼è¢«æ¸ é¤ãdescriptionstring -æä¾ç»å±å¹é 读å¨çæè¿°æå
å¨å½å任塿 徿 ä¸è®¾ç½®ä¸ä¸ª 16 x 16 åç´ ç徿 , é常ç¨äºä¼ è¾¾æç§åºç¨ç¨åºç¶ææè¢«å¨å°éç¥ç¨æ·ã
win.invalidateShadow() macOSâ
Invalidates the window shadow so that it is recomputed based on the current window shape.
BrowserWindows that are transparent can sometimes leave behind visual artifacts on macOS. This method can be used to clear these artifacts when, for example, performing an animation.
win.setHasShadow(hasShadow)â
hasShadowboolean
设置çªå£æ¯å¦æé´å½±ã
win.hasShadow()â
è¿å boolean - 夿çªå£æ¯å¦æé´å½±.
win.setOpacity(opacity) Windows macOSâ
opacitynumber - ä»äº0.0 ( å®å ¨éæ ) å1.0 ( å®å ¨ä¸éæ ) ä¹é´
设置çªå£çä¸éæåº¦ã å¨Linuxä¸ä¸èµ·ä½ç¨ã Out of bound number values are clamped to the [0, 1] range.
win.getOpacity()â
è¿å number - ä»äº0.0 (å®å
¨éæ) å1.0 (å®å
¨ä¸éæ) ä¹é´ã å¨Linuxä¸ï¼å§ç»è¿å1ã
win.setShape(rects) Windows Linux å®éªæ§â
rectsRectangle[] - Sets a shape on the window. éè¿ä¼ å ¥ç©ºå表å°çªå£æ¢å¤ä¸ºç©å½¢ã
对çªå£å½¢ç¶ç设置å³å®äºçªå£å ç³»ç»å 许ç»å¶ä¸ç¨æ·äº¤äºçåºå. å¨ç»å®çåºåå¤, 没æåç´ ä¼è¢«ç»å¶, 䏿²¡æé¼ æ äºä»¶ä¼è¢«ç»è®°. å¨è¯¥åºåå¤çé¼ æ äºä»¶å°ä¸ä¼è¢«è¯¥çªå£æ¥æ¶, èæ¯è½è³è¯¥çªå£åæ¹çä»»æçªå£.
win.setThumbarButtons(buttons) Windowsâ
buttonsThumbarButton[]
è¿å boolean - æé®æ¯å¦æåæ·»å
å°æå®çä¸ç»æé®æ·»å å°èåæ ç缩å¾å·¥å
·æ ä¸ã è¿åä¸ä¸ª boolean 对象表示æ¯å¦æåå°æ·»å äºç¼©ç¥å¾.
ç±äºç©ºé´æé, 缩å¾å·¥å ·æ ä¸çæé®æ°éä¸è¦è¶ è¿7个ã 䏿¦è®¾ç½®äºç¼©ç¥å¾å·¥å ·æ ï¼åæ æ³å é¤ã ä½ä½ å¯ä»¥éè¿è°ç¨ API ä¼ éä¸ä¸ªç©ºæ°ç»æ¥æ¸ 餿é®.
buttons æ¯ä¸ä¸ª Button å¯¹è±¡çæ°ç»:
ButtonObjecticonNativeImage - The icon showing in thumbnail toolbar.clickFunctiontooltipstring (å¯é) - æé®çæç¤ºææ¬.flagsstring[] (å¯é) - æ§å¶æé®çç¹å®ç¶æåè¡ä¸ºã é»è®¤æ åµä¸ï¼å¼ä¸º['enabled']ã
flags 屿§æ¯ä¸ä¸ªæ°ç»ï¼å
å«ä»¥ä¸stringç±»åçå¼:
enabled- 该æé®å¤äºæ´»å¨ç¶æå¹¶å¯ä¾ç¨æ·ä½¿ç¨.disabled- æé®å·²ç¦ç¨ã ä¼ä»¥ä¸ç§è§è§ç¶æè¡¨ç¤ºå®ä¸ä¼ååºç¨æ·æä½ç形弿¾ç¤ºãdismissonclick- 彿é®è¢«ç¹å»æ¶ï¼ç¼©ç¥å¾çªå£ç«å³å ³éãnobackground- ä» ä» ä½¿ç¨å¾åèä¸ç»å¶è¾¹æ¡ãhidden- 该æé®å¯¹ç¨æ·ä¸å¯è§ãnoninteractive- æé®å·²å¯ç¨ï¼ä½ä¸äº¤äºï¼ä¸ç»å¶æé®æä¸çç¶æã æ¤å¼ç¨äºå¨éç¥ä¸ä½¿ç¨æé®çå®ä¾ã
win.setThumbnailClip(region) Windowsâ
regionRectangle - Region of the window
å°çªå£çåºå设置为å¨ä»»å¡æ 䏿¬åå¨çªå£ä¸æ¹æ¶æ¾ç¤ºç缩ç¥å¾å¾åã éè¿æå®ç©ºåºåï¼{ x: 0, y: 0, width: 0, height: 0 }ï¼å¯ä»¥éç½®æ´ä¸ªçªå£ç缩ç¥å¾ã
win.setThumbnailToolTip(toolTip) Windowsâ
toolTipstring
设置å¨ä»»å¡æ 䏿¬åå¨çªå£ç¼©ç¥å¾ä¸æ¶æ¾ç¤ºçå·¥å ·æç¤ºã
win.setAppDetails(options) Windowsâ
é项对象appIdstring (å¯é) - çªå£ç App User Model ID. è¯¥é¡¹å¿ é¡»è®¾ç½®, å¦åå ¶ä»é项尿²¡æææ.appIconPathstring (å¯é) -çªå£ç Relaunch Icon.appIconIndexInteger (å¯é) -appIconPathä¸ç徿 ç´¢å¼ã æªè®¾ç½®appIconPathæ¶å¿½ç¥ã é»è®¤å¼ä¸º0.relaunchCommandstring (å¯é) - çªå£ç éæ°å¯å¨å½ä»¤.relaunchDisplayNamestring (å¯é) - çªå£çéæ°å¯å¨æ¾ç¤ºåç§°.
设置çªå£ä»»å¡æ æé®ç屿§ã
relaunchCommand and relaunchDisplayName must always be set together. 妿å
¶ä¸ä¸ä¸ªå±æ§æ²¡æè®¾ç½®ï¼é£ä¹è¿ä¸¤ä¸ªå±æ§é½ä¸ä¼ä½¿ç¨ã
win.setAccentColor(accentColor) Windowsâ
accentColorboolean | string | null - The accent color for the window. By default, follows user preference in System Settings. To reset to system default, passnull.
Sets the system accent color and highlighting of active window border.
The accentColor parameter accepts the following values:
- Color string - Like
true, but sets a custom accent color using standard CSS color formats (Hex, RGB, RGBA, HSL, HSLA, or named colors). Alpha values in RGBA/HSLA formats are ignored and the color is treated as fully opaque. true- Enable accent color highlighting for the window with the system accent color regardless of whether accent colors are enabled for windows in SystemSettings.false- Disable accent color highlighting for the window regardless of whether accent colors are currently enabled for windows in System Settings.null- Reset window accent color behavior to follow behavior set in System Settings.
示ä¾
const win = new BrowserWindow({ frame: false })
// Set red accent color.
win.setAccentColor('#ff0000')
// RGB format (alpha ignored if present).
win.setAccentColor('rgba(255,0,0,0.5)')
// Enable accent color, using the color specified in System Settings.
win.setAccentColor(true)
// Disable accent color.
win.setAccentColor(false)
// Reset window accent color behavior to follow behavior set in System Settings.
win.setAccentColor(null)
win.getAccentColor() Windowsâ
Returns string | boolean - the system accent color and highlighting of active window border in Hex RGB format.
If a color has been set for the window that differs from the system accent color, the window accent color will be returned. Otherwise, a boolean will be returned, with true indicating that the window uses the global system accent color, and false indicating that accent color highlighting is disabled for this window.
win.showDefinitionForSelection() macOSâ
å webContents.showDefinitionForSelection() ç¸å.
win.setIcon(icon) Windows Linuxâ
iconNativeImage | string
设置çªå£å¾æ
win.setWindowButtonVisibility(visible) macOSâ
visibleboolean
设置æ¯å¦çªå£äº¤éç¯éè¦æ¾ç¤ºã
win.setAutoHideMenuBar(hide) Windows Linuxâ
hideboolean
设置çªå£èåæ æ¯å¦èªå¨éèã 䏿¦è®¾ç½®ï¼èåæ å°åªå¨ç¨æ·åå» Alt 鮿¶æ¾ç¤ºã
妿èåæ å·²ç»å¯è§, è°ç¨ setAutoHideMenuBar(true)æ¶ä¸ä¼ç«å»éèã
win.isMenuBarAutoHide() Windows Linuxâ
è¿å boolean - 夿çªå£çèåæ æ¯å¦èªå¨éè.
win.setMenuBarVisibility(visible) Windows Linuxâ
visibleboolean
设置èåæ æ¯å¦å¯è§ã 妿èåæ èªå¨éèï¼ç¨æ·ä»ç¶å¯ä»¥éè¿åå» Alt 鮿¥å¤åºèåæ ã
win.isMenuBarVisible() Windows Linuxâ
è¿å boolean - 夿çªå£çèåæ æ¯å¦å¯è§.
win.isSnapped() Windowsâ
Returns boolean - whether the window is arranged via Snap.
The window is snapped via buttons shown when the mouse is hovered over window maximize button, or by dragging it to the edges of the screen.
win.setVisibleOnAllWorkspaces(visible[, options]) macOS Linuxâ
visibleboolean
设置çªå£æ¯å¦å¨ææå·¥ä½ç©ºé´ä¸å¯è§
[!NOTE] This API does nothing on Windows.
win.isVisibleOnAllWorkspaces() macOS Linuxâ
è¿å boolean - 夿çªå£æ¯å¦å¨ææå·¥ä½ç©ºé´ä¸å¯è§.
[!NOTE] This API always returns false on Windows.
win.setIgnoreMouseEvents(ignore[, options])â
ignoreboolean
忽ç¥çªå£å çææé¼ æ äºä»¶
卿¤çªå£ä¸åççææé¼ æ äºä»¶å°è¢«ä¼ éå°æ¤çªå£ä¸é¢ççªå£, ä½å¦ææ¤çªå£å ·æç¦ç¹, å®ä»ç¶ä¼æ¥æ¶é®çäºä»¶
win.setContentProtection(enable) macOS Windowsâ
enableboolean
鲿¢çªå£å å®¹è¢«å ¶ä»åºç¨æè·
On Windows, it calls SetWindowDisplayAffinity with WDA_EXCLUDEFROMCAPTURE. å¯¹äº Windows 10 2004以ä¸çï¼æ¬çªå£å°å®å
¨ä»æåä¸ç§»é¤ï¼å¨ä½çæ¬ Windows ä¸å
¶è¡ä¸ºå°±åæ¯ WDA_MONITOR ææäºé»è²çªå£ã
On macOS, it sets the NSWindow's sharingType to NSWindowSharingNone. Unfortunately, due to an intentional change in macOS, newer Mac applications that use ScreenCaptureKit will capture your window despite win.setContentProtection(true). See here.
win.isContentProtected() macOS Windowsâ
Returns boolean - whether or not content protection is currently enabled.
win.setFocusable(focusable) macOS Windowsâ
focusableboolean
设置çªå£æ¯å¦å¯èç¦
å¨ macOS ä¸ï¼è¯¥æ¹æ³ä¸ä¼ä»çªå£ä¸ç§»é¤ç¦ç¹ã
win.isFocusable() macOS Windowsâ
Returns boolean - Whether the window can be focused.
win.setParentWindow(parent)â
parentBrowserWindow | null
设置 parent 为å½åçªå£çç¶çªå£. 为nullæ¶è¡¨ç¤ºå°å½åçªå£è½¬ä¸ºé¡¶çº§çªå£
win.getParentWindow()â
è¿å BrowserWindow | null - å¦ææ²¡æç¶çªå£å为 nullã
win.getChildWindows()â
è¿å BrowserWindow[] - é¦é¡µçåçªå£.
win.setAutoHideCursor(autoHide) macOSâ
autoHideboolean
设置è¾å ¥æ¶æ¯å¦éèå æ
win.selectPreviousTab() macOSâ
å½å¯ç¨æ¬å°é项å¡ï¼å¹¶ä¸çªå£ä¸æå¦ä¸ä¸ªæ ç¾æ¶ï¼éæ©ä¸ä¸ä¸ªé项å¡ã
win.selectNextTab() macOSâ
å½å¯ç¨æ¬å°é项å¡ï¼å¹¶ä¸çªå£ä¸æå¦ä¸ä¸ªæ ç¾æ¶ï¼éæ©ä¸ä¸ä¸ªé项å¡ã
win.showAllTabs() macOSâ
Shows or hides the tab overview when native tabs are enabled.
win.mergeAllWindows() macOSâ
å½å¯ç¨æ¬å°é项å¡å¹¶ä¸åå¨å¤ä¸ªæå¼çªå£æ¶ï¼å°ææçªå£åå¹¶å°ä¸ä¸ªå¸¦æå¤ä¸ªé项å¡ççªå£ä¸ã
win.moveTabToNewWindow() macOSâ
妿å¯ç¨äºæ¬æºé项å¡å¹¶ä¸å½åçªå£ä¸æå¤ä¸ªé项å¡ï¼åå°å½åé项å¡ç§»å¨å°æ°çªå£ä¸ã
win.toggleTabBar() macOSâ
妿å¯ç¨äºæ¬æºé项å¡å¹¶ä¸å½åçªå£ä¸åªæä¸ä¸ªé项å¡ï¼å忢é项塿 æ¯å¦å¯è§ã
win.addTabbedWindow(browserWindow) macOSâ
browserWindowBrowserWindow
å¨è¯¥çªå£ä¸æ·»å ä¸ä¸ªçªå£ä½ä¸ºé项å¡ï¼ä½äºçªå£å®ä¾çé项å¡ä¹åã
win.setVibrancy(type[, options]) macOSâ
typestring | null - Can betitlebar,selection,menu,popover,sidebar,header,sheet,window,hud,fullscreen-ui,tooltip,content,under-window, orunder-page. æ´å¤è¯¦ç»ä¿¡æ¯ï¼è¯·æ¥é macOS documentation
卿µè§å¨çªå£ä¸æ·»å ä¸ä¸ªå¨æç¹æã ä¼ é null æç©ºå符串å°ä¼ç§»é¤çªå£ä¸ç卿ææã The animationDuration parameter only animates fading in or fading out the vibrancy effect. Animating between different types of vibrancy is not supported.
win.setBackgroundMaterial(material) Windowsâ
materialstringauto- Let the Desktop Window Manager (DWM) automatically decide the system-drawn backdrop material for this window. é»è®¤ä¸ºè¯¥å¼ãnone- Don't draw any system backdrop.mica- Draw the backdrop material effect corresponding to a long-lived window.acrylic- Draw the backdrop material effect corresponding to a transient window.tabbed- Draw the backdrop material effect corresponding to a window with a tabbed title bar.
This method sets the browser window's system-drawn background material, including behind the non-client area.
See the Windows documentation for more details.
[!NOTE] This method is only supported on Windows 11 22H2 and up.
win.setWindowButtonPosition(position) macOSâ
positionPoint | null
卿 æ¡çªå£ä¸è®¾ç½®èªå®ä¹æ§å¶æé®çä½ç½®ã Passing null will reset the position to default.
win.getWindowButtonPosition() macOSâ
Returns Point | null - The custom position for the traffic light buttons in frameless window, null will be returned when there is no custom position.
win.setTouchBar(touchBar) macOSâ
touchBarTouchBar | null
设置çªå£çè§¦æ¸æ¡å¸å± 设置为 null æundefinedå°æ¸
é¤è§¦æ¸æ¡. æ¤æ¹æ³åªå¨ è®¾å¤æè§¦æ¸æ¡æ¶æçæã
[!NOTE] The TouchBar API is currently experimental and may change or be removed in future Electron releases.
win.setBrowserView(browserView) Experimental Deprecatedâ
browserViewBrowserView | null - AttachbrowserViewtowin. 妿已ç»éå äºå ¶ä»BrowserViewï¼é£ä¹å®ä»¬å°ä¼è¢«ä»æ¤çªå£ä¸ç§»é¤ã
[!WARNING] The
BrowserViewclass is deprecated, and replaced by the newWebContentsViewclass.
win.getBrowserView() Experimental Deprecatedâ
è¿å BrowserView | null - éå å° win ç BrowserView ã 妿æªéå ï¼åè¿å nullã 妿éå äºå¤ä¸ª BrowserViewï¼åæåºé误ã
[!WARNING] The
BrowserViewclass is deprecated, and replaced by the newWebContentsViewclass.
win.addBrowserView(browserView) Experimental Deprecatedâ
browserViewBrowserView
æ¿ä»£ setBrowserView çAPIï¼æ¯æå¤ä¸ªbrowserViewä¸èµ·ä½¿ç¨ã
[!WARNING] The
BrowserViewclass is deprecated, and replaced by the newWebContentsViewclass.
win.removeBrowserView(browserView) Experimental Deprecatedâ
browserViewBrowserView
[!WARNING] The
BrowserViewclass is deprecated, and replaced by the newWebContentsViewclass.
win.setTopBrowserView(browserView) Experimental Deprecatedâ
browserViewBrowserView
æé« browserView äºå
¶å®éå å° win ç BrowserView ä¹ä¸ ã 妿 browserView æªéå å° winï¼åæåºé误ã
[!WARNING] The
BrowserViewclass is deprecated, and replaced by the newWebContentsViewclass.
win.getBrowserViews() Experimental Deprecatedâ
Returns BrowserView[] - a sorted by z-index array of all BrowserViews that have been attached with addBrowserView or setBrowserView. The top-most BrowserView is the last element of the array.
[!WARNING] The
BrowserViewclass is deprecated, and replaced by the newWebContentsViewclass.
win.setTitleBarOverlay(options) Windows Linuxâ
é项对象colorString (optional) - The CSS color of the Window Controls Overlay when enabled.symbolColorString (optional) - The CSS color of the symbols on the Window Controls Overlay when enabled.heightInteger (optional) - The height of the title bar and Window Controls Overlay in pixels.
On a window with Window Controls Overlay already enabled, this method updates the style of the title bar overlay.
On Linux, the symbolColor is automatically calculated to have minimum accessible contrast to the color if not explicitly set.