C#--镂空窗体设计

功能

  1. 绘制镂空窗体
  2. 窗体上面的按键不透明

一、简介

窗体镂空,虽然可以实现透明窗体,但是并未实现真正意义上的透明,点击窗体可以点击到窗体背后的窗体。

二、效果展示

在这里插入图片描述
三、代码实现

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 通用测试平台
{
    public partial class Form1 : Form
    {
        #region 变量
        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);
        #endregion
        #region 构造函数
        public Form1()
        {
            InitializeComponent();
            SetWindowLong(Handle, GWL_EXSTYLE, WS_EX_LAYERED);
            SetLayeredWindowAttributes(Handle, 0x00FF00, 255, LWA_COLORKEY);
        }
        #endregion
        #region 窗体初始化
        private void Form1_Load(object sender, EventArgs e)
        {
            //开启透明窗体
            TransparencyKey = BackColor;
        } 
        #endregion
    }
}

猜你喜欢

转载自blog.csdn.net/qq_37120496/article/details/115251533