[英]Blending a WPF Window with the desktop
是否可以(以及如何)調整用於在桌面上顯示WPF表單的混合模式?
我有一個窗口,可以覆蓋整個屏幕。 這是XAML:
<Window x:Class="RedGreenBarsWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Red/Green Overlay" Topmost="True" Height="300" Width="525" AllowsTransparency="True" WindowStyle="None" Background="Transparent" WindowStartupLocation="Manual" IsHitTestVisible="False">
<Canvas Name="canvas" />
</Window>
不能單擊它,並且在加載窗口時會對其進行調整大小並移動以覆蓋整個屏幕。 然后,我在畫布上繪制一些形狀,如下所示:
System.Windows.Media.Brush red = new SolidColorBrush(System.Windows.Media.Color.FromArgb(200, 255, 0, 0));
System.Windows.Size s = new System.Windows.Size(System.Windows.SystemParameters.PrimaryScreenWidth, System.Windows.SystemParameters.PrimaryScreenHeight);
int lines = 20;
for (double i = 0; i < s.Width; i += s.Width / lines)
{
System.Windows.Shapes.Rectangle rect = new System.Windows.Shapes.Rectangle
{
Width = s.Width / lines,
Height = s.Height
};
rect.Fill = red;
Canvas.SetTop(rect, 0);
Canvas.SetLeft(rect, i * 2);
canvas.Children.Add(rect);
}
這確實是應該做的,但不是我想要的。 這是在Photoshop中完成的可視化效果(我的圖像看起來像“正常”):
我需要找出一種使它看起來像右邊的紅色框的方法,其中文本不會被覆蓋的顏色淡化。 我已經搜索了很多東西,盡管那里有一些庫可以用Window中的元素來完成此任務,但我需要混合模式才能擴展到整個桌面。 如何才能做到這一點?
您需要使用其他混合功能。 到目前為止,我相信您沒有簡單的解決方案; WPF已請求此功能,但尚不支持。
這個另一個問題也是關於為WPF筆刷使用不同的混合功能。 它還包含指向一個不錯的(但很長)教程的鏈接,該教程關於如何實現自定義混合(實際上是一個完整的混合庫)。
這些頁面包含zip,幾乎每個頁面上都有源代碼。 您需要以下內容來構建它們:(引用).NET 3.5 SP1,DirectX SDK,以及CodePlex上WPF Futures中的Shader Effects BuildTask和模板。
我在這里復制鏈接:
編輯:要捕獲桌面圖像,您可以嘗試使用具有透明背景的簡單FrameworkElement
(例如Rectangle
(“ Transparent
是畫筆)。 然后,您可以使用RenderTargetBitmap
類將其轉換為ImageBrush。 這是一個代碼片段,可以幫助您入門:
public Brush RectangleToBrush(Rectangle rect)
{
int w = (int)rect.ActualWidth;
int h = (int)rect.ActualHeight;
var rtb = new RenderTargetBitmap(w, h, 96d, 96d, PixelFormats.Default);
rtb.Render(rect);
ImageBrush brush = new ImageBrush(BitmapFrame.Create(rtb));
return brush;
}
您可以使用Grid將此矩形放在.xaml文件中,以使其覆蓋整個窗口:
<Window ...>
<Grid>
<Rectangle x:Name="DesktopCaptureRectangle"/>
<Grid>
<!-- Your controls here -->
</Grid>
</Grid>
</Window>
注意:我不確定此解決方案在性能方面是否會很好...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.