C#やモバイルアプリ関係のログ

C#やXamarin、スマートフォンアプリのことを書いておきたいと思います

c# Windows_Forms 背景が透明なウィンドウ

C#
Windows Forms
透明なウィンドウ
f:id:Jirobe_Katori:20201220020134p:plain

github.com

方法①

Form1.cs

using System.Drawing;
using System.Windows.Forms;

namespace TransparentWindowSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //ウィンドウを透明に Start
            //ここの色は何でもよい
            BackColor = Color.Red;
            TransparencyKey = BackColor;
            //ウィンドウを透明に End
        }
    }
}

方法②

Formクラスの継承した実装にOnPaint(PaintEventArgs e)メソッドを用意
Form1.cs

using System.Drawing;
using System.Windows.Forms;

namespace TransparentWindowSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //ウィンドウを透明に Start
        protected override void OnPaint(PaintEventArgs e)
        {
            //ここの色は何でもよい
            TransparencyKey = Color.Red;
            e.Graphics.FillRectangle(new SolidBrush(TransparencyKey), ClientRectangle);

            base.OnPaint(e);
        }
        //ウィンドウを透明に End
    }
}

参考文献

docs.microsoft.com qiita.com