C# Winform 窗体美化(四、镂空窗体)

四、镂空窗体

例子下载

直接贴效果图吧:

1、控件的透明
控件镂空

2、窗体的透明
窗体镂空

代码如下:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        SetWindowLong(Handle, GWL_EXSTYLE, WS_EX_LAYERED);
        SetLayeredWindowAttributes(Handle, 0x00FF00, 255, LWA_COLORKEY);//替换某种颜色为透明(0x00FF00:绿色)
    }
    private const uint WS_EX_LAYERED = 0x80000;//异形窗体特效的实现
    private const int GWL_EXSTYLE = -20;//设定一个新的扩展风格
    private const int LWA_COLORKEY = 1;//透明方式
    [DllImport("user32", EntryPoint = "SetWindowLong")]
    //改变指定窗口的属性
    private static extern uint SetWindowLong(IntPtr hwnd, int nIndex, uint dwNewLong);
    [DllImport("user32", EntryPoint = "SetLayeredWindowAttributes")]
    //设置分层窗口透明度
    private static extern int SetLayeredWindowAttributes(IntPtr hwnd, int crKey, int bAlpha, int dwFlags);

    private void Form1_Load(object sender, EventArgs e)
    {
        //TransparencyKey = BackColor;
    }
}

注意:
TransparencyKey = BackColor;

这句代码可以使窗口透明。

猜你喜欢

转载自blog.csdn.net/myinc/article/details/70183041