Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 744
Screen
Access display and screen information for responsive layouts.
The Electron.Screen API provides access to screen and display information, including screen size, display metrics, cursor position, and multi-monitor configurations. This is essential for creating responsive applications that adapt to different screen configurations.
Gets information about all available displays.
Returns:
An array of displays that are currently available.
Gets the current position of the mouse cursor on screen.
Returns:
The current absolute position of the mouse pointer.
Gets the display that most closely intersects the provided bounds.
Parameters:
rectangle- The rectangle to find the matching display for
Returns:
The display that most closely intersects the provided bounds.
Gets the display that is closest to the specified point.
Parameters:
point- The point to find the nearest display for
Returns:
The display nearest the specified point.
macOS: The height of the menu bar in pixels.
Returns:
The height of the menu bar in pixels.
Gets information about the primary display (main screen).
Returns:
The primary display.
Emitted when a new Display has been added.
Emitted when one or more metrics change in a display. The changedMetrics is an array of strings that describe the changes. Possible changes are bounds, workArea, scaleFactor and rotation.
Emitted when oldDisplay has been removed.
// Get primary displayvarprimaryDisplay=awaitElectron.Screen.GetPrimaryDisplayAsync();Console.WriteLine($"Primary display: {primaryDisplay.Size.Width}x{primaryDisplay.Size.Height}");// Get all displaysvardisplays=awaitElectron.Screen.GetAllDisplaysAsync();Console.WriteLine($"Available displays: {displays.Length}");// Get display near cursorvarcursorPoint=awaitElectron.Screen.GetCursorScreenPointAsync();varnearestDisplay=awaitElectron.Screen.GetDisplayNearestPointAsync(cursorPoint);Console.WriteLine($"Nearest display scale factor: {nearestDisplay.ScaleFactor}");// Get all displays for multi-monitor setupvardisplays=awaitElectron.Screen.GetAllDisplaysAsync();foreach(vardisplayindisplays){Console.WriteLine($"Display {display.Id}:");Console.WriteLine($" Size: {display.Size.Width}x{display.Size.Height}");Console.WriteLine($" Position: {display.Bounds.X},{display.Bounds.Y}");Console.WriteLine($" Scale Factor: {display.ScaleFactor}");Console.WriteLine($" Work Area: {display.WorkArea.Width}x{display.WorkArea.Height}");}// Create window on appropriate displayvardisplays=awaitElectron.Screen.GetAllDisplaysAsync();vartargetDisplay=displays.FirstOrDefault(d =>d.Bounds.X>0)??displays.First();varwindowOptions=newBrowserWindowOptions{Width=Math.Min(1200,targetDisplay.WorkArea.Width),Height=Math.Min(800,targetDisplay.WorkArea.Height),X=targetDisplay.WorkArea.X+(targetDisplay.WorkArea.Width-1200)/2,Y=targetDisplay.WorkArea.Y+(targetDisplay.WorkArea.Height-800)/2};varwindow=awaitElectron.WindowManager.CreateWindowAsync(windowOptions);// Monitor display changesElectron.Screen.OnDisplayAdded+=(display)=>{Console.WriteLine($"Display added: {display.Id}");UpdateWindowPositions();};Electron.Screen.OnDisplayRemoved+=(display)=>{Console.WriteLine($"Display removed: {display.Id}");UpdateWindowPositions();};Electron.Screen.OnDisplayMetricsChanged+=(display,metrics)=>{Console.WriteLine($"Display {display.Id} metrics changed: {string.Join(", ",metrics)}");UpdateWindowPositions();};voidUpdateWindowPositions(){// Recalculate window positions based on current displays}// Account for menu bar height on macOSif(RuntimeInformation.IsOSPlatform(OSPlatform.OSX)){varmenuBarHeight=awaitElectron.Screen.GetMenuBarHeightAsync();varwindowOptions=newBrowserWindowOptions{Y=menuBarHeight,// Position below menu barTitleBarStyle=TitleBarStyle.Hidden// Hide title bar for custom look};}- Electron.WindowManager - Position windows based on screen information
- Electron.App - Handle display-related application events
- Electron Screen Documentation - Official Electron screen API
Want to contribute to this documentation? Please fork and create a PR! The Wiki is autogenerated from the /docs content in the repository.