Aero Glassを利用した半透過ウィンドウの作成 - C#

Aero Glassの機能を利用した半透過ウィンドウを作成します。

概要

DwmIsCompositionEnabled APIにてDWM合成を有効化し、DWM合成が利用可能かチェックします。DWM合成が有効である場合は DwmExtendFrameIntoClientArea により、半透明のウィンドウにします。第一引数にはウィンドウハンドルを、第二引数には半透明にする範囲を指定します。 最初のフィールド(leftWidth)に-1を指定した場合はウィンドウ全体が半透明のウィンドウとなります。

プログラム

コード

Windows Formアプリケーションを新規作成し、下記のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace AeroApplication
{
  public partial class Form1 : Form
  {
    [DllImport("dwmapi.dll")]
    private static extern int DwmIsCompositionEnabled(out bool enabled);

    [DllImport("dwmapi.dll", PreserveSig = true)]
    static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

    //[DllImport("dwmapi.dll", PreserveSig = false)]
    //static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS margins);

    [StructLayout(LayoutKind.Sequential)]
    public struct MARGINS
    {
      public int leftWidth;
      public int rightWidth;
      public int topHeight;
      public int bottomHeight;
    }
    
    public Form1()
    {
      InitializeComponent();

      BackColor = Color.Black;

      bool DwmEnabled=false;
      DwmIsCompositionEnabled(out DwmEnabled);
      if (DwmEnabled == true) {
        MARGINS margin;
        margin.leftWidth=-1;
        margin.rightWidth=0;
        margin.topHeight=0;
        margin.bottomHeight=0;

        DwmExtendFrameIntoClientArea(this.Handle, ref margin); 
      }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }
  }
}

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。全面半透明のウィンドウが表示できます。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-01-07
作成日: 2010-07-29
iPentec all rights reserverd.