Create vista glass effect on wpf with custom color

旧城冷巷雨未停 提交于 2019-12-20 07:31:47

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/10004802/create-vista-glass-effect-on-wpf-with-custom-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!