簡體   English   中英

使背景透明

[英]Making Background Transparent

在此處輸入圖片說明 我試圖為Windows 10制作一個臨時的屏幕鍵盤,並且需要背景透明,以使其對用戶更方便(按鍵已經是透明的)。 但是,我不知道如何使背景透明。

任何幫助將不勝感激。

我相信我本質上是在下面的這個線程中尋找代碼的更新版本:

using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class TransparentWindow : MonoBehaviour
{
    [SerializeField]
    private Material m_Material;

    private struct MARGINS
    {
        public int cxLeftWidth;
        public int cxRightWidth;
        public int cyTopHeight;
        public int cyBottomHeight;
    }

    [DllImport("user32.dll")]
    private static extern IntPtr GetActiveWindow();

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);

    [DllImport("user32.dll")]
    static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll", EntryPoint = "SetLayeredWindowAttributes")]
    static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, byte bAlpha, int dwFlags);

    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    private static extern int SetWindowPos(IntPtr hwnd, int hwndInsertAfter, int x, int y, int cx, int cy, int uFlags);

    [DllImport("Dwmapi.dll")]
    private static extern uint DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);

    const int GWL_STYLE = -16;
    const uint WS_POPUP = 0x80000000;
    const uint WS_VISIBLE = 0x10000000;
    const int HWND_TOPMOST = -1;

    void Start()
    {
 // You really don't want to enable this in the editor, but it works there..

    int fWidth = Screen.width;
    int fHeight = Screen.height;
    var margins = new MARGINS() { cxLeftWidth = -1 };
    var hwnd = GetActiveWindow();

    SetWindowLong(hwnd, GWL_STYLE, WS_POPUP | WS_VISIBLE);

    // Transparent windows with click through
    SetWindowLong(hwnd, -20, 524288 | 32);//GWL_EXSTYLE=-20; WS_EX_LAYERED=524288=&h80000, WS_EX_TRANSPARENT=32=0x00000020L
    SetLayeredWindowAttributes(hwnd, 0, 255, 2);// Transparency=51=20%, LWA_ALPHA=2
    SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, fWidth, fHeight, 32 | 64); //SWP_FRAMECHANGED = 0x0020 (32); //SWP_SHOWWINDOW = 0x0040 (64)
    DwmExtendFrameIntoClientArea(hwnd, ref margins);


    }

    void OnRenderImage(RenderTexture from, RenderTexture to)
    {
        Graphics.Blit(from, to, m_Material);
    }
}

給出的代碼無效,因此我認為它已經過時了。 我不知道如何自己更新它,因為它有點超出我的技能范圍。 當我將代碼上傳到Unity時,它只是說代碼中有錯誤,並且它不是有效的腳本。 但是,當我打開腳本時,沒有錯誤出現。

我希望能夠對鍵盤后面的任何東西(例如台式機)有一個相對較好的視野,但實際上我只是看到一個黑色的平面。

更新:顯然,該錯誤消息是由我的腳本與班級名稱不同引起的。 昨天我花了4個多小時來嘗試修復該錯誤消息,而這個命名事件就是其中的原因:(。感謝Ruzihm。無論如何,現在錯誤消息消失了,當我運行或構建程序時,我的透明窗口材料就來了向上:深粉紅色,然后我將Unity版本改回了2018.2.16f1,但沒有成功,然后刪除了#if!Unity Editor行,以使透明度在編輯器中可以完美地工作,但在我構建它時卻沒有。請注意,在構建和在編輯器中運行時,單擊一直有效。

正如評論中發現的那樣,當照相機的透明標志設置為solid color並將粉紅色的透明窗口材料替換為白色透明材料時,此問題已解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM