2013-06-04 16 views
8

透明な単純なアプリケーションを作成したいと思いますが、通常の枠線、閉じるボタン、最小化、最大化ボタンを保持します。C#WPF透明な枠線付きの枠線

私は標準

<Window 
    WindowStyle="None" 
    AllowsTransparency="True" 
    Background="Transparent"> 
</Window> 

を使用して、ウィンドウを透明にする方法を知っているが、これは国境や右上のボタンを削除します。私は、ソートのソリューションを提供し、このスレッド、

Transparent window with a border

を読んで、本当に、私はちょうど私が窓を透明にしていない場合があることになる標準の境界を保つことができるようにしたいです。私はウィンドウを移動し、サイズを変更したり、閉じることができます。これは可能ですか?

答えて

6

あなたは、この目的のために使用することができますbased on this tutorial on Microsoft.com私は一緒に迅速Transparancy Converterクラスを投げた:あなたがこれをでたら、

using System; 
using System.Runtime.InteropServices; 
using System.Windows; 
using System.Windows.Interop; 

namespace WpfApplication2 
{ 
    class TransparancyConverter 
    { 
     private readonly Window _window; 

     public TransparancyConverter(Window window) 
     { 
      _window = window; 
     } 

     public void MakeTransparent() 
     { 
      var mainWindowPtr = new WindowInteropHelper(_window).Handle; 
      var mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr); 
      if (mainWindowSrc != null) 
       if (mainWindowSrc.CompositionTarget != null) 
        mainWindowSrc.CompositionTarget.BackgroundColor = System.Windows.Media.Color.FromArgb(0, 0, 0, 0); 

      var margins = new Margins 
      { 
       cxLeftWidth = 0, 
       cxRightWidth = Convert.ToInt32(_window.Width) * Convert.ToInt32(_window.Width), 
       cyTopHeight = 0, 
       cyBottomHeight = Convert.ToInt32(_window.Height) * Convert.ToInt32(_window.Height) 
      }; 

      if (mainWindowSrc != null) DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); 
     } 

     [StructLayout(LayoutKind.Sequential)] 
     public struct Margins 
     { 
      public int cxLeftWidth; 
      public int cxRightWidth; 
      public int cyTopHeight; 
      public int cyBottomHeight; 
     } 

     [DllImport("DwmApi.dll")] 
     public static extern int DwmExtendFrameIntoClientArea(IntPtr hwnd, ref Margins pMarInset); 
    } 
} 

あなたのXAMLに透明な背景の属性を追加し、Window_Loadedイベントをサブスクライブして呼び出しますMakeTransparent方法:

<Window etc etc Background="Transparent" Loaded="Window_Loaded"> 

private void Window_Loaded(object sender, RoutedEventArgs e) 
{ 
    var transparancyConverter = new TransparancyConverter(this); 
    transparancyConverter.MakeTransparent(); 
} 

スクリーンショットは、以下である:

Screenshot

+0

大変ありがとうございました。私はそれを持って遊びます。 –

+0

ほとんどの作業。それはウィンドウ7では動作していますが、ウィンドウ8では動作していないようです。前もって感謝します! –

+0

@StefanVasiljevicあなたが8に多大な運があるとは思わない、上記のコードはWindows 8には存在しないAeroを悲しみに使っています – JMK

1

まず、背景色のrgb(a)カラーの(a)lpha設定を見てみましょう。 アルファ設定はオブジェクトカラーの不透明度を設定します。

私はこれを投稿しているので、私の前にはもっと簡潔に見える別の投稿があり、あなたにはおそらくより適していることがわかりました。