批处理之家's Archiver

whiter 发表于 2019-4-7 12:54

设置windows下窗口程序半透明工具

windows下自带的编译工具
%windir%/Microsoft.NET/Framework/v4.0.30319/csc.exe /nologo trans.cs
用法trans.exe [进程名] [透明度]
进程名 看任务管理器或者tasklist
透明度 0~255[code]using System;
using System.Runtime.InteropServices;
namespace App
{
    class Program
    {
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
        [DllImport("user32.dll", SetLastError = true)]
        static extern int SetWindowLong(IntPtr hWnd, int nIndex, uint dwNewLong);
        [DllImport("user32.dll", SetLastError = true)]
        static extern int GetWindowLong(IntPtr hWnd, int nIndex);
        static void Main(string[] args)
        {
            byte trans = 168;
            var processName = "chrome";
            if(args.Length == 1){
                processName = args[0];
            }
            if(args.Length == 2){
                processName = args[0];
                trans = Convert.ToByte(args[1]);
            }
            Console.WriteLine("processName:"+processName);
            Console.WriteLine("透明度0~255:"+trans);
            Program program = new Program();
            System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName(processName);
            System.Console.WriteLine("当前打开进程个数:"+ps.Length);
            if(ps.Length < 1) {
                Console.WriteLine(processName+" 没有在执行");
                return;
            };
            foreach(System.Diagnostics.Process p in ps){
                bool isTransed = program.MakeWindowTransparent(p.MainWindowHandle, trans);
                if(isTransed){
                    Console.WriteLine("修改成功");
                    return;
                }
            }
            Console.WriteLine("修改失败");
        }
        bool MakeWindowTransparent(IntPtr hWnd, byte factor)
        {
            const int GWL_EXSTYLE = (-20);
            const uint WS_EX_LAYERED = 0x00080000;
            int Cur_STYLE = GetWindowLong(hWnd, GWL_EXSTYLE);
            SetWindowLong(hWnd, GWL_EXSTYLE, (uint)(Cur_STYLE | WS_EX_LAYERED));
            const uint LWA_COLORKEY = 1;
            const uint LWA_ALPHA = 2;
            const uint WHITE = 0xffffff;
            return SetLayeredWindowAttributes(hWnd, WHITE, factor, LWA_COLORKEY | LWA_ALPHA);
        }
    }
}
[/code]

老刘1号 发表于 2019-4-7 13:07

支持,不过这个happy好像写过

whiter 发表于 2019-4-7 13:26

[b]回复 [url=http://www.bathome.net/redirect.php?goto=findpost&pid=219064&ptid=52477]2#[/url] [i]老刘1号[/i] [/b]
我记得之前的应该是c++写的,这个是c#的

lockxxx 发表于 2023-11-22 11:12

成功,感谢分享

页: [1]

Powered by Discuz! Archiver 7.2  © 2001-2009 Comsenz Inc.