トップ回答者
タイトルバーに喰い込むようにコントロールを配置したい

質問
-
[Visual Studio 2008 RTM, WindowsVista Business]
[XAMLコード]
<Window
x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
AllowsTransparency="True" Background="Transparent" WindowStyle="None">
<Border BorderBrush="#FF404040" BorderThickness="1" CornerRadius="5">
<Border BorderBrush="#FFFFFFFF" BorderThickness="1" CornerRadius="5">
</Border>
</Border>
</Window>[C#コード]
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interop;
using System.Windows.Media;namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);GlassExtender.ExtendGlassFrame(this, new Thickness(-1));
}
}public static class GlassExtender
{
[DllImport("dwmapi.dll", PreserveSig = false)]
static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref MARGINS MARGINS);[DllImport("dwmapi.dll", PreserveSig = false)]
static extern bool DwmIsCompositionEnabled();struct MARGINS
{
public int Left;
public int Right;
public int Top;
public int Bottom;
public MARGINS(Thickness t)
{
Left = (int)t.Left;
Right = (int)t.Right;
Top = (int)t.Top;
Bottom = (int)t.Bottom;
}
}public static bool ExtendGlassFrame(Window target, Thickness t)
{
if (!DwmIsCompositionEnabled()) return false;IntPtr handle = new WindowInteropHelper(target).Handle;
if (handle == IntPtr.Zero) throw new InvalidOperationException("このウィンドウはGlass機能を使用できません。");target.Background = Brushes.Transparent;
HwndSource.FromHwnd(handle).CompositionTarget.BackgroundColor = Colors.Transparent;MARGINS margins = new MARGINS(t);
DwmExtendFrameIntoClientArea(handle, ref margins);return true;
}
}
}上記ソースは、WindowStyleがNoneのWindowにDwmExtendFrameIntoClientAreaでVistaのAero効果を付けているのですが、
透明なWindowが表示されてしまいます。
これは、WPFの仕様なのですか?
よろしくお願いします。
回答
すべての返信
-
以下のページ
Custom Window Control - GlassWindow - Source Code
http://pavanpodila.spaces.live.com/blog/cns!9C9E888164859398!345.entry
が,参考になりそうですけどね。
(Feel free to use the code as you wish. と書かれていますし)
Window から派生させた (自作の) GlassWindow クラスに,
MacOS X スタイルのほうでは,独自に ControlTemplate をあてています。
GlassWindow の AttachToVisualTree() と連携しています。
(TemplatePart属性で紐付けしています)
追加したいものを加えればやれるんじゃないかな。
-
yayadon さんからの引用 以下のページ
Custom Window Control - GlassWindow - Source Code
http://pavanpodila.spaces.live.com/blog/cns!9C9E888164859398!345.entry
が,参考になりそうですけどね。
(Feel free to use the code as you wish. と書かれていますし)
Window から派生させた (自作の) GlassWindow クラスに,
MacOS X スタイルのほうでは,独自に ControlTemplate をあてています。
GlassWindow の AttachToVisualTree() と連携しています。
(TemplatePart属性で紐付けしています)
追加したいものを加えればやれるんじゃないかな。
yayadon様、ご回答ありがとうございます。
ご呈示いただいたサンプルソースは以前に拝見しています。
ControlTemplateやTemplatePartの使用方法がわかり易いのですが、私がしたいモノと若干違います。
このサンプルソースでは、AllowsTransparencyをTrueにしているためDWMでGlass効果にできません。
実行時に非クライアント領域にクライアント領域のコントロールの一部(検索バーコントロールなど)を喰い込ませて、且つDWMでGlass効果を有効にしたWPF Windowっていうのは、どのようにすれば良いのでしょうか。
(デザイン時) (実行時)
┌────────┐ ┌────────┐
│ │← 非クライアント領域 →│ ┌───┐│
├───┬───┬┤ ├───┴───┴┤
│ └───┘│ │ │
│ │ │ │
│ │← クライアント領域 →│ │
│ │ │ │
└────────┘ └────────┘
質問内容に不備があり、申し訳ございませんが、もう一度よろしくお願いします。
-
DwmExtendFrameIntoClientArea でなく,
DwmEnableBlurBehindWindow でもダメなんでしたっけ?
質問の意味がわかっていないのかもしれませんが,
AllowsTransparency を True にすると,
WindowStyle が None にしないといけない関係で,
そもそもタイトルバーは表示されないですよね?
で,先のブログのサンプルでは,
Grid をタイトルバーとして使うカラクリをいれてるコードがあります。