我不能通过C#中的句柄设置窗口的透明度吗? [英] can't I set transparency of a window by its handle in c#?

查看:157
本文介绍了我不能通过C#中的句柄设置窗口的透明度吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置所有窗口的透明度.我有以下代码.

i am trying to set transparency of all windows. I have following code.

public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);

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

    public const int GWL_EXSTYLE = -20;
    public const int WS_EX_LAYERED = 0x80000;
    public const int LWA_ALPHA = 0x2;

    public Form1()
    {
        InitializeComponent();
        this.Load += new EventHandler(Form1_Load);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Process[] processlist = Process.GetProcesses();

        foreach (Process theprocess in processlist)
        {
            SetWindowLong(theprocess.Handle, GWL_EXSTYLE,
                GetWindowLong(theprocess.Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED);
            SetLayeredWindowAttributes(theprocess.Handle, 0, 128, LWA_ALPHA);
        }

    }
}

执行代码时什么也没发生.

nothing happens when I execute the code.

怎么了?

推荐答案

SetWindowLong需要一个窗口句柄(hWnd),但您正在向它传递一个进程句柄. 更改

SetWindowLong takes a window handle (hWnd), but you're passing it a process handle instead. Change all instances of

theprocess.Handle

theProcess.MainWindowHandle

更改后,它可以在我对其进行测试的Windows XP机器上运行.现在,我将不得不修改代码以使窗口恢复正常;)幸运的是,Visual Studio 2010窗口不受影响.

After changing that, it worked on the Windows XP machine I tested it on. Now I'm gonna have to modify the code to get the windows back to normal ;) Luckily, the Visual Studio 2010 window was unaffected.

这篇关于我不能通过C#中的句柄设置窗口的透明度吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆