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);
    }
}

猜你喜欢

转载自blog.csdn.net/ymtianyu/article/details/110063963
今日推荐