创建与自定义颜色WPF Vista的玻璃效果 [英] Create vista glass effect on wpf with custom color

查看:146
本文介绍了创建与自定义颜色WPF Vista的玻璃效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多教程,告诉你如何使用WPF应用程序Vista的玻璃效果如之一。



我不想用默认的颜色主题,用户选择。换句话说,如果我申请了Vista的玻璃效应到我的WPF应用程序将是等于无论用户选择:








这是我已经尝试,它是有些一个解决方案:




1)让整个桌面的图片。稍后我会弄清楚如何使用代码做到这一点



2)放在画布图像。我正好有Outlook中打开,当我把我的桌面上的捕获。同时将您希望与一些透明度







3 )创建属性X和Y,实现INotifyPropertyChanged接口,这样我们可能会落后更新代码中的图像的位置:




 公共部分类主窗口:窗口,INotifyPropertyChanged的
{
双_X;
公共双X
{
得到
{
返回_X;
}

{
_X =价值;
NotifyPropertyChanged(X);
}
}

双_Y;
公共双Y
{
得到
{
返回_Y;
}

{
_Y =价值;
NotifyPropertyChanged(Y);
}
}
公共事件PropertyChangedEventHandler的PropertyChanged;

私人无效NotifyPropertyChanged(字符串信息)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(信息)) ;
}
}



不要忘记设置: this.DataContext =这一点;



$ b> $ b

4)现在我们需要用相对位置将图像放置到桌面没有窗户。所以我们创建一个事件处理程序,只要在移动窗口,我们修正图像的像的位置:




 无效MainWindow_Loaded(对象发件人,RoutedEventArgs E)
{
this.DataContext =这一点;

this.LocationChanged + =新的EventHandler(MainWindow_LocationChanged);

}

无效MainWindow_LocationChanged(对象发件人,EventArgs五)
{
X = - ((System.Windows.Window)(发件人)) .RestoreBounds.TopLeft.X;
Y = - ((System.Windows.Window)(发件人))RestoreBounds.TopLeft.Y。
}



最后,你应该有一些这样的:





该解决方案将工作的伟大,如果我在那里有整个桌面的图像。每次桌面改变,我将不得不更新图像源。还更新图像源的时候,我将不得不捕获桌面映像没有我的窗口。我不知道如何让桌面的图像没有我的主窗口。也许我将不得不隐藏我的窗口得到的屏幕截图,然后再次显示了我的窗前


解决方案

如果你想在WPF半透明窗口,只需设置窗口透明度的东西< 1,设置AllowsTransparency为true,不幸的是,你必须WindowStyle设置为无为好。这意味着你必须,如果你想让它重新创建窗口镶边。


There are a lot of tutorials that show you how to use vista glass effect in wpf application like this one.

I don't want to use the default color theme that the user selects. In other words if I apply the vista glass efect to my wpf application it will be equal to whatever the user selects in:


This is what I have tried and it is somewhat of a solution:

1) get a picture of the entire desktop. I will later figure out how to do this with code

2) place the image in a canvas. I happen to have outlook open when I took the capture of my desktop. Also place a rectangle on top with the color that you want to use with some transparency

3) Create the properties X and Y, implement the INotifyPropertyChanged interface so that we may update the position of the image in code behind:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    double _X;
    public double X
    {
        get
        {
            return _X;
        }
        set
        {
            _X = value;
            NotifyPropertyChanged("X");
        }
    }

    double _Y;
    public double Y
    {
        get
        {
            return _Y;
        }
        set
        {
            _Y = value;
            NotifyPropertyChanged("Y");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

Dont forget to set: this.DataContext = this; in order to succesfuly bind the properties when the window is done loading

4) Now we need to place the image with position relative to the desktop not to the window. So we create an event handler whenever the window moves we fix the position of the image like:

void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.DataContext = this;

        this.LocationChanged += new EventHandler(MainWindow_LocationChanged);

    }

    void MainWindow_LocationChanged(object sender, EventArgs e)
    {
        X = -((System.Windows.Window)(sender)).RestoreBounds.TopLeft.X;
        Y = -((System.Windows.Window)(sender)).RestoreBounds.TopLeft.Y;            
    }

Finally you should have something like:

This solution will work great if I where to have an image of the entire desktop. Every time the desktop changes I will have to update the image source. Also when updating the image source I will have to capture the desktop image without my window. I don't know how to get an image of the desktop without my main window. Maybe I will have to hide my window get the screen capture and then show my window again

解决方案

If you want a semi-transparent window in WPF, just set the windows Opacity to something < 1, set AllowsTransparency to true and, unfortunately, you have to set WindowStyle to None as well. This means you'll have to recreate the window chrome if you want it.

这篇关于创建与自定义颜色WPF Vista的玻璃效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆