透明控制台DllImport

本文关键字:DllImport 控制台 透明 | 更新日期: 2023-09-27 18:07:53

我想透明控制台,但我有一个编译错误:transparent .cs(39,48):错误CS0019:操作符'^'不能应用于'System '类型的操作数。IntPtr'和'int'

using System;
using System.Runtime.InteropServices;
namespace Transparency
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll")]
        static extern bool SetLayeredWindowAttributes(IntPtr hWnd, uint crKey,byte bAlpha, uint dwFlags);
        [DllImport("user32.dll", SetLastError = true)]
        internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex); 
        [DllImport("kernel32.dll", SetLastError = true)]
        internal static extern IntPtr GetConsoleWindow(); 
        static void Main(string[] args)
        {
            int GWL_EXSTYLE = -20;
            int WS_EX_LAYERED = 0x80000;
            uint LWA_ALPHA = 0x2;
            //int LWA_COLORKEY = 0x1;
            // Obtain our handle (hWnd)
            IntPtr Handle = GetConsoleWindow();
            SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED);
            // Opacity = 0.5 = (255/2)
            SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA);
        }
    }
}

透明控制台DllImport

我认为您需要按位或单管|以及将GetWindowLong的返回类型更改为int。参见pinvoke.net。