【多媒体交互】透明无边框窗口

技术应用方向

  • 桌面宠物
  • 透明屏互动

核心技术点

  • 窗体隐藏
  • 背景透明
  • 穿透点击

脚步实现步骤

  • 使用Windows API,DllImport引入user32.dll、dwmapi.dll中的相关函数
  • 在Unity中获取当前窗口句柄【GetActiveWindow
  • 自定义边框结构【MARGINS】
    • 窗体阴影:【1,1,1,1】
    • 顶栏透明:【0,0,30,0】
    • 全透明边:【-1,-1,-1,-1】
  • 将窗口边框扩展到客户区域【DwmExtendFrameIntoClientArea
  • 将窗口样式设置为分层和透明【SetWindowLong
  • 设置透明颜色或整体透明度【SetLayeredWindowAttributes
  • 将窗口位置设置为始终置顶【SetWindowPos
  • 允许应用在后台运行【Application.runInBackground = true】

Unity场景设置

  • 将相机【Camera】的背景类型(Clear Flags)设置为Solid Color,并将颜色设置为完全透明(alpha=0)
  • 在相机的设置中,可能需要开启HDR(高动态范围)和关闭MSAA(多重采样抗锯齿)以避免透明边缘问题
  • 【Project Setting】【Resolution and Presentation】设置
    • 开启Fullscreen Window
    • 开启Resizable Window
    • 开启Visible In Background
    • 关闭Use DXGI flip model swapchain for D3D11
csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;
 
public class WindowTransparent : MonoBehaviour
{
    // 定义一个结构来存储窗口边框的边距大小
    private struct MARGINS
    {
        public int cxLeftWidth;
        public int cxRightWidth;
        public int cyTopHeight;
        public int cyBottomHeight;
    }
 
    // 导入 user32.dll 以获取活动窗口句柄 (HWND)
    [DllImport("user32.dll")]
    private static extern IntPtr GetActiveWindow();
 
    // 导入 Dwmapi.dll 以将窗口边框扩展到客户区域
    [DllImport("Dwmapi.dll")]
    private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
 
    // 导入 user32.dll 以修改窗口属性
    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
 
    // 导入 user32.dll 以设置窗口位置
    [DllImport("user32.dll", SetLastError = true)]
    static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
 
    // 导入 user32.dll 以设置分层窗口属性 (透明度)
    [DllImport("user32.dll")]
    static extern int SetLayeredWindowAttributes(IntPtr hWnd, uint crKey, byte bAlpha, uint dwFlags);
 
    // 代码中使用的常量和变量
    const int GWL_EXSTYLE = -20;  // 修改窗口样式的索引
    const uint WS_EX_LAYERED = 0x00080000;  // 分层窗口的扩展样式
    const uint WS_EX_TRANSPARENT = 0x00000020;  // 透明窗口的扩展样式
    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);  // 窗口插入位置(始终置顶)
    const uint LWA_COLORKEY = 0x00000001;  // 设置颜色键的标志(用于透明度)
    private IntPtr hWnd;  // 活动窗口的句柄
 
    private void Start()
    {
#if !UNITY_EDITOR
        // 获取活动窗口的句柄
        hWnd = GetActiveWindow();
 
        // 创建一个边距结构来定义边框大小
        MARGINS margins = new MARGINS { cxLeftWidth = -1 };
 
        // 将窗口边框扩展到客户区域(玻璃效果)
        DwmExtendFrameIntoClientArea(hWnd, ref margins);
 
        // 将窗口样式设置为分层和透明
        SetWindowLong(hWnd, GWL_EXSTYLE, WS_EX_LAYERED);
 
        // 设置窗口颜色键(用于透明度)
        SetLayeredWindowAttributes(hWnd, 0, 0, LWA_COLORKEY);
 
        // 将窗口位置设置为始终置顶
        SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, 0);
#endif
 
        // 允许应用在后台运行
        Application.runInBackground = true;
    }
}
相关推荐
初级代码游戏9 小时前
套路化编程 C# winform 自适应缩放布局
开发语言·c#·winform·自动布局·自动缩放
大空大地202610 小时前
流程控制语句--switch多分支语句使用、while循环语句的使用、do...while语句、for循环
c#
方见华Richard10 小时前
世毫九实验室(Shardy Lab)研究成果清单(2025版)
人工智能·经验分享·交互·原型模式·空间计算
微祎_11 小时前
Flutter for OpenHarmony:构建一个 Flutter 平衡球游戏,深入解析动画控制器、实时物理模拟与手势驱动交互
flutter·游戏·交互
爱喝白开水a11 小时前
前端AI自动化测试:brower-use调研让大模型帮你做网页交互与测试
前端·人工智能·大模型·prompt·交互·agent·rag
kylezhao201912 小时前
C#序列化与反序列化详细讲解与应用
c#
JQLvopkk12 小时前
C# 实践AI :Visual Studio + VSCode 组合方案
人工智能·c#·visual studio
故事不长丨12 小时前
C#线程同步:lock、Monitor、Mutex原理+用法+实战全解析
开发语言·算法·c#
kingwebo'sZone12 小时前
C#使用Aspose.Words把 word转成图片
前端·c#·word
GLDbalala12 小时前
Unity基于自定义管线实现经典经验光照模型
unity·游戏引擎