在窗体中为控件设置透明度

时间:2016-01-03 05:35:01

标签: c# windows visual-studio-2015

我是编程新手。我找不到为控件设置透明度的方法。请帮忙。像Form.transparency或其他东西。

1 个答案:

答案 0 :(得分:0)

使表单透明的最简单方法是将Form.Opacity属性设置为值。 或者,您可以尝试 API 调用,

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

[DllImport("user32.dll", SetLastError = true)]
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 const int LWA_COLORKEY = 0x1;

致电,

SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) ^ WS_EX_LAYERED);
SetLayeredWindowAttributes(Handle, 0, 128, LWA_ALPHA);