You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Godot中创建自定义窗口:解决无边框窗口无法移动缩放问题

在Godot中实现自定义窗口(类似Avast样式)

borderless选项的正常表现

是的,开启borderless(无边框)选项后,系统原生的窗口标题栏、边框交互逻辑会被完全禁用,窗口无法通过原生方式移动、缩放,这是该选项的正常行为。

实现自定义窗口效果的步骤

要做出类似Avast的自定义标题栏+可交互窗口效果,需要自行实现窗口的移动、缩放逻辑,同时搭建自定义标题栏UI,具体操作如下:

1. 基础窗口设置

  • 在项目设置中开启borderless选项,同时可设置窗口初始尺寸,禁用系统默认缩放(避免与自定义逻辑冲突)。
  • 以Window节点作为项目根节点,方便统一管理窗口属性。

2. 搭建自定义标题栏

  • 添加PanelContainer作为标题栏容器,设置背景色、固定高度,置于窗口顶部。
  • 在标题栏内添加功能按钮:最小化、最大化/还原、关闭,使用Button节点实现,并绑定对应逻辑:
    • 关闭窗口:调用get_tree().quit()或get_window().close()
    • 最小化:调用get_window().minimize()
    • 最大化/还原:通过get_window().is_maximized()判断状态,调用get_window().maximize()或get_window().unmaximize()

3. 实现窗口移动功能

给标题栏节点添加输入事件处理逻辑(可通过_input(event)函数实现):

var is_dragging = false
var drag_start_offset = Vector2.ZERO

func _input(event):
    # 处理鼠标按下事件,记录拖动起始偏移
    if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
        is_dragging = event.pressed
        if is_dragging:
            drag_start_offset = get_global_mouse_position() - get_window().position
            # 若窗口处于最大化状态,拖动时先还原
            if get_window().is_maximized():
                get_window().unmaximize()
                # 调整偏移,让鼠标位置对应标题栏原位置
                drag_start_offset.x = get_global_mouse_position().x - (get_viewport_rect().size.x / 2)
    # 处理鼠标拖动事件,更新窗口位置
    elif event is InputEventMouseMotion and is_dragging:
        get_window().position = get_global_mouse_position() - drag_start_offset

4. 实现窗口缩放功能

在窗口的四个角落、四条边缘添加小型Control节点作为缩放手柄,给每个手柄绑定输入事件处理:
以右下角手柄为例:

var is_resizing = false
var resize_start_size = Vector2.ZERO
var resize_start_mouse_pos = Vector2.ZERO

func _on_resize_handle_input(event):
    if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
        is_resizing = event.pressed
        if is_resizing:
            resize_start_size = get_window().size
            resize_start_mouse_pos = get_global_mouse_position()
    elif event is InputEventMouseMotion and is_resizing:
        var mouse_delta = get_global_mouse_position() - resize_start_mouse_pos
        # 更新窗口尺寸,限制最小大小避免异常
        get_window().size = (resize_start_size + mouse_delta).clamp(Vector2(400, 300), Vector2(1920, 1080))

其他方向的手柄可调整delta的计算逻辑,比如左边缘仅修改窗口x坐标与宽度,上边缘仅修改y坐标与高度。

5. 细节优化

  • 给标题栏、缩放手柄添加鼠标悬停样式,提升交互反馈。
  • 窗口最大化时隐藏缩放手柄,避免误操作。
  • 保留系统默认窗口快捷键(如Alt+F4关闭),提升使用习惯兼容性。

内容的提问来源于stack exchange,提问作者stepic Game

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 04:56:19