複数ウィンドウの管理
複数のBrowserWindowインスタンスを作成、調整し、相互に通信する方法を学びます。メインプロセスとIPCの基礎を直接発展させる重要なスキルです。
「複数ウィンドウの管理」はCoddyKit上の無料Electron Desktop App Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはElectron Desktop App Development学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Electron Desktop App Developmentコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Beyond a Single Window
Real desktop apps often need multiple windows: a main editor, a settings panel, a preview. The main process owns and tracks all of them.
Creating a Window
Each window is a BrowserWindow instance created in the main process.
const { BrowserWindow } = require('electron');
function createWindow() {
const win = new BrowserWindow({ width: 600, height: 400 });
win.loadFile('index.html');
return win;
}Keeping References
If you do not keep a reference, the window can be garbage collected. Store windows in a variable or array.
let windows = [];
function track(win) {
windows.push(win);
win.on('closed', () => {
windows = windows.filter(w => w !== win);
});
}Parent and Child Windows
You can make one window the parent of another. Child windows move with the parent and can be modal.
new BrowserWindow({
parent: mainWindow,
modal: true,
width: 300,
height: 200
});Window-to-Window Messaging
Windows do not share memory. They communicate through the main process using IPC as a relay.
ipcMain.on('to-other', (e, msg) => {
otherWindow.webContents.send('from-main', msg);
});Identifying Windows
Each window has a unique id. Use it to target a specific window when relaying messages.
function findWindow(id, list) {
return list.find(w => w.id === id);
}
console.log(findWindow(2, [{ id: 1 }, { id: 2 }]));Showing Windows Gracefully
Avoid a white flash by creating windows hidden and showing them once ready.
const win = new BrowserWindow({ show: false });
win.once('ready-to-show', () => win.show());Managing Focus
Use focus() and the blur event to know which window the user is interacting with, useful for save prompts.
Closing and Cleanup
Listen for close to prompt for unsaved changes, and closed to release references.
win.on('close', (e) => {
if (hasUnsaved) {
e.preventDefault();
}
});Single Instance Lock
Use app.requestSingleInstanceLock() to focus the existing window instead of opening duplicates.
Coordinating State
Centralize shared state in the main process so every window reads from one source of truth, avoiding drift.
Quick Check
Test your window management knowledge.
Recap
You learned to create and track multiple BrowserWindow instances, manage parent/child relationships, relay messages via the main process, and clean up references properly.
よくある質問
「複数ウィンドウの管理」レッスンは無料ですか?
はい。「複数ウィンドウの管理」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Electron Desktop App Developmentコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Electron Desktop App Developmentコースには全4レッスンが含まれています。
「複数ウィンドウの管理」で何を学びますか?
複数のBrowserWindowインスタンスを作成、調整し、相互に通信する方法を学びます。メインプロセスとIPCの基礎を直接発展させる重要なスキルです。 ブラウザで直接実行するハンズオンコードでElectron Desktop App Developmentを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Electron Desktop App Developmentを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのElectron Desktop App Developmentは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「複数ウィンドウの管理」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このElectron Desktop App Developmentレッスンでコードを書いて実行できますか?
はい。すべてのElectron Desktop App Developmentレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。