2007年6月2日土曜日

Aero Glass

すりガラス効果がついたウィンドウ


Vistaっぽいウィンドウにしているこの「すりガラス」とか「半透明」の効果、

実は2種類あった。

クライアント領域(アプリケーションウィンドウ内)での描写技術としてのWPFと
ウィンドウ同士の合成技術としてのWin32は別々のもの

つまり、↓こんなのはWPFの半透明で










円が透けて、下に赤い四角とボタンが見える


メディアプレイヤーの↓こんなやつはWin32のDWM(Desktop Window Manager)




WPF単体でも、ウィンドウのStyleをnoneにしてAllowTransparentをtureにすればウィンドウの下まで見える、半透明ウィンドウができるけど、DWMを使うとそのマシンに設定された枠で半透明になるのがちょっと違う。

で、最初のやつを背景(Window.Background を No brush)にして、DWMで半透明にすると、
↓こんな感じになる










ただし、ローカルでAeroが切られていると・・・










ちょっとカワイソウなことに・・・ま、デフォルトで斜めにグラデーションが入っているからそんなにみっともなくはない?

コード的には GlassHelperというDWMを叩くクラスを作って、

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Interop;
using System.Runtime.InteropServices;

namespace AeroGlass
{
class GlassHelper
{
public static bool ExtendGlassFrame(Window window, Thickness margin)
{
if (!DwmIsCompositionEnabled())
return false;

IntPtr hwnd = new WindowInteropHelper(window).Handle;
if (hwnd == IntPtr.Zero)
throw new InvalidOperationException("ガラス効果を加える前にウィンドウを表示せよ");

window.Background = Brushes.Transparent;
HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;

MARGINS margins = new MARGINS(margin);
DwmExtendFrameIntoClientArea(hwnd, ref margins);
return true;
}

struct MARGINS
{
public MARGINS(Thickness t)
{
Left = (int)t.Left;
Right = (int)t.Right;
Top = (int)t.Top;
Bottom = (int)t.Bottom;
}
public int Left;
public int Right;
public int Top;
public int Bottom;
}

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

[DllImport("dwmapi.dll", PreserveSig = false)]
static extern bool DwmIsCompositionEnabled();

}
}



OnSourceInitialized をオーバーライドして実行

protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
GlassHelper.ExtendGlassFrame(this, new Thickness(-1));
}

0 件のコメント: