C#透过Win32Api更改软件透明度

导入user32及相关方法

    //根据窗体的类名或者标题名来找到窗口的句柄
    [DllImport("user32.dll", EntryPoint = "FindWindow")]
    private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

    //指定扩展窗口,以便控制窗口的透明度
    [DllImport("user32", EntryPoint = "SetWindowLong")]
    private static extern uint SetWindowLong(IntPtr hwnd,int nIndex,uint dwNewLong);

    //设置窗体的透明度,dwFlags指定为2时,bAlpha才能起作用
    [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
    private static extern int SetLayeredWindowAttributes(IntPtr hwnd,int crKey,int bAlpha,int dwFlags);

初使化几个常量和变量

    private const uint WS_EX_LAYERED = 0x80000;
    private const int WS_EX_TRANSPARENT = 0x20;
    private const int GWL_EXSTYLE = (-20); //设置为分层窗口 20 才能进行控制
    private const int LWA_ALPHA = 0x2;
    private static IntPtr myHandle = new IntPtr(0);

定义一个公用方法供外部调用

    public static void changeOpacity(string windowClass, string windowName, int windowOpacity) 
    {
        if (windowClass != "") myHandle = FindWindow(windowClass, null);
        else if (windowName != "") myHandle = FindWindow(null, windowName);
        else
        {
            MessageBox.Show("找不到指定的窗口,未正确指定视窗类名或窗体名!", "Error");
            return;
        }
        if (myHandle == IntPtr.Zero)
        {
            MessageBox.Show("指定的窗口获取失败!", "Error");
            return;
        }
        SetWindowLong(myHandle, GWL_EXSTYLE, WS_EX_LAYERED);
        SetLayeredWindowAttributes(myHandle, 0, windowOpacity, LWA_ALPHA);
    }

以下为完整代码类

想偷懒的朋友可以直接新建一个类叫SetWindowAttr,把下面的代码直接贴上去,在外部直接叫用这个类的changeOpacity方法传入相关参数即可

public class SetWindowAttr
{
    //根据窗体的类名或者标题名来找到窗口的句柄
    [DllImport("user32.dll", EntryPoint = "FindWindow")]
    private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);

    //指定扩展窗口,以便控制窗口的透明度
    [DllImport("user32", EntryPoint = "SetWindowLong")]
    private static extern uint SetWindowLong(IntPtr hwnd,int nIndex,uint dwNewLong);

    //设置窗体的透明度,dwFlags指定为2时,bAlpha才能起作用
    [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
    private static extern int SetLayeredWindowAttributes(IntPtr hwnd,int crKey,int bAlpha,int dwFlags);

    private const uint WS_EX_LAYERED = 0x80000;
    private const int WS_EX_TRANSPARENT = 0x20;
    private const int GWL_EXSTYLE = (-20); //设置为分层窗口 20 才能进行控制
    private const int LWA_ALPHA = 0x2;
    private static IntPtr myHandle = new IntPtr(0);

    public static void changeOpacity(string windowClass, string windowName, int windowOpacity) 
    {
        if (windowClass != "") myHandle = FindWindow(windowClass, null);
        else if (windowName != "") myHandle = FindWindow(null, windowName);
        else
        {
            MessageBox.Show("找不到指定的窗口,未正确指定视窗类名或窗体名!", "Error");
            return;
        }
        if (myHandle == IntPtr.Zero)
        {
            MessageBox.Show("指定的窗口获取失败!", "Error");
            return;
        }
        SetWindowLong(myHandle, GWL_EXSTYLE, WS_EX_LAYERED);
        SetLayeredWindowAttributes(myHandle, 0, windowOpacity, LWA_ALPHA);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值