在游戏开发中,全屏显示(Full Screen)是常见的需求,目的是让游戏在屏幕上以最大尺寸显示,避免被其他窗口或控件干扰。下面是几种常见平台和语言下的实现方式:
✅ 一、Windows(C++/DirectX/OpenGL)
1. 使用 SetWindowLong 或 SetWindowLongPtr
#include <windows.h>
// 设置窗口为全屏
SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_OVERLAPPEDWINDOW);
SetWindowLongPtr(hWnd, GWL_STYLE, WS_STYLE_FULLSCREEN);
// 设置窗口为全屏模式
SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_OVERLAPPEDWINDOW | WS_EX_TOOLWINDOW);
SetWindowLongPtr(hWnd, GWL_STYLE, WS_STYLE_FULLSCREEN);
// 设置窗口为全屏
SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_OVERLAPPEDWINDOW | WS_EX_TOOLWINDOW);
SetWindowLongPtr(hWnd, GWL_STYLE, WS_STYLE_FULLSCREEN);
// 设置全屏
if (IsWindowVisible(hWnd) && !IsWindowEnabled(hWnd)) {
ShowWindow(hWnd, SW_HIDE);
SetWindowLongPtr(hWnd, GWL_STYLE, WS_POPUP);
SetWindowLongPtr(hWnd, GWL_EXSTYLE, WS_EX_TOPMOST);
ShowWindow(hWnd, SW_SHOW);
}
2. 使用 SetProcessDpiAwareness(Windows 10+)
SetProcessDpiAwareness(ProcessDpiAwareness::DpiAwarenessSystemAware);
✅ 二、Windows Forms (C#)
1. 设置窗体为全屏
this.FormBorderStyle = FormBorderStyle.None;
this.Dock = DockStyle.Fill;
this.WindowState = FormWindowState.Maximized;
✅ 三、WPF (C#)
1. 设置窗体为全屏
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800" Height="600">
<Grid>
<Grid.RowDefinition Height="*"/>
<Grid.ColumnDefinition Width="*"/>
<Rectangle Fill="Blue" Width="100" Height="100" />
</Grid>
</Window>
在代码中:
this.WindowStyle = WindowStyle.None;
this.Show();
✅ 四、Unity (C#)
1. 设置为全屏
void Start() {
Screen.fullScreen = true;
Screen.fullScreenMode = FullScreenMode.Windowed;
}
或者使用 Application.SetStyle:
Application.SetStyle(ApplicationStyle.FullScreen);
✅ 五、WebGL(JavaScript/Three.js)
1. 使用 requestFullscreen()(Chrome/Edge)
document.body.requestFullscreen();
✅ 六、Android(Java/Kotlin)
1. 设置为全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMIT_HEIGHT, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMIT_HEIGHT);
✅ 七、iOS(Swift)
1. 设置为全屏
window?.setWindowLevel(.floating)
window?.isUserInteractionEnabled = false
window?.isHidden = true
✅ 总结
| 平台 | 实现方式 |
|---|---|
| Windows | SetWindowLong / SetWindowLongPtr |
| Windows Forms | FormBorderStyle.None / Dock = Fill |
| WPF | WindowStyle.None / Dock = Fill |
| Unity | Screen.fullScreen = true |
| WebGL | requestFullscreen() |
| Android | getWindow().setFlags(...) |
| iOS | window.setWindowLevel(...) |
如你有特定平台或语言的需求,可以告诉我,我可以提供更详细的代码示例。