缓冲区大小
获取帧缓冲区尺寸并设置视口:
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
帧缓冲区大小可能因显示器DPI变化而独立于窗口尺寸。
窗口尺寸
获取窗口内容区域尺寸:
int width, height;
glfwGetWindowSize(window, &width, &height);
获取窗口边框到内容区域的边距:
int left, top, right, bottom;
glfwGetWindowFrameSize(window, &left, &top, &right, &bottom);
设置窗口尺寸:
glfwSetWindowSize(window, 800, 600);
限制窗口最小/最大尺寸:
glfwSetWindowSizeLimits(window, 800, 600, 1920, 1080); // 限制范围
glfwSetWindowSizeLimits(window, 800, 600, GLFW_DONT_CARE, GLFW_DONT_CARE); // 仅限制最小值
设置窗口宽高比:
glfwSetWindowAspectRatio(window, 16, 9); // 固定比例16:9
缩放比例
获取DPI缩放比例:
float xscale, yscale;
glfwGetWindowContentScale(window, &xscale, &yscale);
窗口位置
获取/设置窗口位置:
int xpos, ypos;
glfwGetWindowPos(window, &xpos, &ypos);
glfwSetWindowPos(window, 100, 100);
创建时预设位置:
glfwWindowHint(GLFW_POSITION_X, 10);
glfwWindowHint(GLFW_POSITION_Y, 10);
窗口标题
获取/修改标题:
const char* title = glfwGetWindowTitle(window);
glfwSetWindowTitle(window, u8"UTF-8标题");
窗口图标化与最大化
图标化或恢复窗口:
glfwIconifyWindow(window); // 最小化
glfwRestoreWindow(window); // 恢复
最大化窗口:
glfwMaximizeWindow(window);
窗口可见性
隐藏/显示窗口:
glfwHideWindow(window);
glfwShowWindow(window);
检查可见状态:
int visible = glfwGetWindowAttrib(window, GLFW_VISIBLE);
创建时预设可见性:
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // 初始隐藏
窗口焦点
获取焦点状态:
int focused = glfwGetWindowAttrib(window, GLFW_FOCUSED);
主动请求焦点:
glfwFocusWindow(window);
任务栏闪烁
请求用户注意(闪烁任务栏图标):
glfwRequestWindowAttention(window);
窗口透明度
获取/设置透明度(0.0完全透明,1.0不透明):
float opacity = glfwGetWindowOpacity(window);
glfwSetWindowOpacity(window, 0.5f);
其他窗口属性
通过glfwGetWindowAttrib
查询的属性列表:
GLFW_FOCUSED
:窗口是否拥有输入焦点GLFW_ICONIFIED
:窗口是否最小化GLFW_MAXIMIZED
:窗口是否最大化GLFW_HOVERED
:光标是否悬停在内容区GLFW_RESIZABLE
:窗口是否可调整大小GLFW_DECORATED
:是否显示标题栏和边框GLFW_TRANSPARENT_FRAMEBUFFER
:帧缓冲区是否支持透明
通过glfwSetWindowAttrib
可修改部分属性(如禁用调整大小):
glfwSetWindowAttrib(window, GLFW_RESIZABLE, GLFW_FALSE);