我正在尝试设置所有窗口的透明度。我有以下代码。
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);
}
}
}
执行代码时没有任何反应。
有什么问题?
答案 0 :(得分:5)
SetWindowLong
采用窗口句柄(hWnd),但你传递的是一个进程句柄。
更改
theprocess.Handle
到
theProcess.MainWindowHandle
更改后,它在我测试的Windows XP机器上运行。现在我将不得不修改代码以使窗口恢复正常;)幸运的是,Visual Studio 2010窗口不受影响。
答案 1 :(得分:2)
这部分代码:^ WS_EX_LAYERED
翻转WS_EX_LAYERED位,
我想你想要:| WS_EX_LAYERED
答案 2 :(得分:1)
您是否尝试过设置the Opacity?
this.Opacity = 0.50;