2011-02-07 73 views
7

我必須在WPF中開發一個半透明表單,但控件不應該是transperent。我是WPF的新手。如何在WPF中創建一個半透明表單

我曾嘗試不同的東西等預先設置不透明度= 0.5,但沒有結果

請在細節和步驟解釋一步

謝謝!

  • 我知道AllowTransperency可以被設置爲True僅當WindowStyle設置爲無,但我需要出示邊境以及

UPDATE: 帕夫洛Glazkov,您有什麼看法此解決方案

<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" Opacity="1" AllowsTransparency="True" WindowStyle="None" Background="Transparent"> 
<Grid Background="Transparent"> 
    <Border Margin="2,2,12,34" Name="border1" BorderBrush="Lavender" BorderThickness="5" CornerRadius="20,0,20,0"></Border> 
    <Button Height="23" Margin="93,101,110,0" Name="button1" VerticalAlignment="Top" Background="CadetBlue" Foreground="White">Hello WPF</Button> 
    <Button Height="24" Margin="0,8,20,0" Name="button2" VerticalAlignment="Top" HorizontalAlignment="Right" Width="21" Click="button2_Click">X</Button> 
</Grid> 

+3

表單在WPF中不存在;你是指一個窗口......還是不同的東西? – 2011-02-07 14:18:52

+0

關於「Pavlo Glazkov,你對這個解決方案有什麼看法」......好吧,很難說。這取決於你的目標。如果這種邊界是你想要的,那麼這是一條路。唯一的問題是,在這種情況下,你有絕對透明的窗口。你實際上可以點擊它。如果這不是所需的行爲,則應將背景設置爲不完全透明的顏色。 – 2011-02-07 20:07:11

+0

@ Pavlo Glazkov - 你說的對,這種類型的答案只是臨時解決方案。基本上我很驚訝地發現MS沒有在WPF中提供這種功能。我知道在2005年有些冗長而複雜的代碼使用API​​來實現這種功能,但現在是2011年,我們仍然需要編寫冗長的代碼,它很醜陋。那麼你談論的那些顏色不是完全透明的顏色? – Student 2011-02-07 20:56:09

回答

12

首先,你需要設置AllowTransperencyTrue。然後,可以將窗口的背景設置爲透明的(至所需的程度)刷:

<Window x:Class="MyWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     WindowStyle="None" 
     AllowsTransparency="True" 
     Background="{DynamicResource WindowBackground}"> 
    <Window.Resources> 
     <SolidColorBrush x:Key="WindowBackground" 
         Color="White" 
         Opacity="0.5"/> 
    </Window.Resources> 
    ... 
</Window> 

請注意,AllowTransperency只能如果WindowStyle組被設置爲TrueNone

更新: 如果你不想設置WindowStyleNone,並希望保持非標準邊框和窗口按鈕也將在Windows Vista/7的唯一工作與Windows Aero主題的替代品。

的技巧是,你可以用下面的代碼擴展了「玻璃」區域到整個窗口:

public static class WindowUtils 
{ 
    /// <summary> 
    /// Extends the glass area into the client area of the window 
    /// </summary> 
    /// <param name="window">Window to extend the glass on.</param> 
    /// <param name="thikness">Thickness of border to extend.</param> 
    public static void ExtendGlass(this Window window, Thickness thikness) { 
     try { 
      int isGlassEnabled = 0; 
      Win32.DwmIsCompositionEnabled(ref isGlassEnabled); 
      if (Environment.OSVersion.Version.Major > 5 && isGlassEnabled > 0) { 
       // Get the window handle 
       var helper = new WindowInteropHelper(window); 
       var mainWindowSrc = HwndSource.FromHwnd(helper.Handle); 

       if (mainWindowSrc != null) { 
        if (mainWindowSrc.CompositionTarget != null) { 
         mainWindowSrc.CompositionTarget.BackgroundColor = Colors.Transparent; 
        } 

        // Get the dpi of the screen 
        System.Drawing.Graphics desktop = 
         System.Drawing.Graphics.FromHwnd(mainWindowSrc.Handle); 

        float dpiX = desktop.DpiX/96; 
        float dpiY = desktop.DpiY/96; 

        // Set Margins 
        var margins = new MARGINS { 
         cxLeftWidth = (int)(thikness.Left * dpiX), 
         cxRightWidth = (int)(thikness.Right * dpiX), 
         cyBottomHeight = (int)(thikness.Bottom * dpiY), 
         cyTopHeight = (int)(thikness.Top * dpiY) 
        }; 

        window.Background = Brushes.Transparent; 

        Win32.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins); 
       } 
      } 
      else { 
       window.Background = SystemColors.WindowBrush; 
      } 
     } 
     catch (DllNotFoundException) { 

     } 
    } 
} 

public class Win32 
{ 
    [DllImport("dwmapi.dll")] 
    public static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS pMarInset); 

    [DllImport("dwmapi.dll")] 
    public static extern int DwmIsCompositionEnabled(ref int en); 

    [DllImport("user32.dll")] 
    public static extern bool SetCursorPos(int X, int Y); 


    [DllImport("User32", EntryPoint = "ClientToScreen", SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] 
    public static extern int ClientToScreen(IntPtr hWnd, [In, Out] POINT pt); 
} 

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

[StructLayout(LayoutKind.Sequential)] 
public class POINT 
{ 
    public int x = 0; 
    public int y = 0; 
} 

到玻璃延伸到你需要調用在該ExtendGlass擴展方法整個窗口窗口的SizeChanged事件處理程序,並通過一個Thickness覆蓋整個窗口:

public MyWindow() { 
    InitializeComponent(); 

    SizeChanged += OnSizeChanged; 
} 

private void OnSizeChanged(object sender, SizeChangedEventArgs e) { 
    double horisontalThickness = Width/2; 
    double verticalThickness = Height/2; 

    var glassThickness = new Thickness(horisontalThickness, verticalThickness, horisontalThickness, verticalThickness); 

    this.ExtendGlass(glassThickness); 
} 
3

你可以嘗試this,它會創建一個玻璃背景爲你的窗口(看起來像Vista的和Windows7透明效果)

Here是微軟進一步的解釋。