4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WindowsFormアプリケーションで背景透過させる方法

Last updated at Posted at 2025-07-26

経緯

かなり前にVPetというwindowsの画面上でペットを愛でることができる、というゲームが出ました。WindowsFormアプリケーションでも似たようなことができないかなーと背景透過してみました。

実装

早速やっていきましょう

ボタンを配置

まずはWindowsフォームアプリケーションで何か配置します。今回は例としてボタンを配置します。

image.png

コード

フォームアプリケーションで背景透過を実装する方法は驚くほど簡単でした。

this.TransparencyKey = this.BackColor;

を追加するだけでできます。また、this.TransparencyKeyに別のカラーコードを設定することでどんな色でも設定できます。

Form1.cs
using System;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //背景色を透過色に指定
            this.TransparencyKey = this.BackColor;

        }
    }
}

実行結果

以下のように背景が透過されました。
image.png

余談

背景は透過されましたが、アプリケーションバーや枠が残っていて気になるという方もいると思います。その際は次のコードを追加することで完全なオーバーレイUIを作成することができます。

this.FormBorderStyle = FormBorderStyle.None;

Form1.cs
using System;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //背景色を透過色に指定
            this.TransparencyKey = this.BackColor;
            //ここ!ボーダーレスウィンドウに設定
            this.FormBorderStyle = FormBorderStyle.None;

        }
    }
}

実行結果

画像のように、完全に画面に浮いたUIを作成することができます。

image.png

感想

この透過機能を使用したFormアプリケーションはなかなか見かけないので色々応用できそうに思います。今後何か透過を利用した仕組みが思いついたら、また記事にしようと思います。

参考文献

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?