Transparent window layer that is click-through and always stays on top

前端 未结 4 1554
梦谈多话
梦谈多话 2020-12-02 10:48

This is some code that I picked up which I tried to implement. Its purpose is to create a form layer which is transparent, full screen, borderless, clickthrough, and always

4条回答
  •  旧时难觅i
    2020-12-02 11:06

    A little extension/modification to Jaska's code, which the form is transparent

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
    
            this.TopMost = true; // make the form always on top
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; // hidden border
            this.WindowState = FormWindowState.Maximized; // maximized
            this.MinimizeBox = this.MaximizeBox = false; // not allowed to be minimized
            this.MinimumSize = this.MaximumSize = this.Size; // not allowed to be resized
            this.TransparencyKey = this.BackColor = Color.Red; // the color key to transparent, choose a color that you don't use
        }
    
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                // Set the form click-through
                cp.ExStyle |= 0x80000 /* WS_EX_LAYERED */ | 0x20 /* WS_EX_TRANSPARENT */;
                return cp;
            }
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            // draw what you want
            e.Graphics.FillEllipse(Brushes.Blue, 30, 30, 100, 100);
        }
    }
    

提交回复
热议问题