Click here to Skip to main content
15,887,434 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,

I wanted to try the nice WPF glass window. But I face a problem when I try to add dwmapi.dll as a reference in Visual Studio.

There is a message box displaying the following:

A reference to ~~dwmapi.dll could not be added .please make sure you that the file is accessible and that it is valid assembly or com component

Some tutorials mention that I can import dwmapi.dll, but that does not work either.

Can you tell me what the problem is please? I am using Windows Vista and Visual Studio 2008.
Posted
Updated 30-Mar-10 10:23am
v3

Its simple, you cannot add a reference to the DWM of the windows, BUT you can import it by using the DllImport (http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx[^])

What I usually do is:

Create the following class:
C#
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;

namespace WpfApplication1
{
    public static class GlassLib
    {
        struct MARGINS
        {
            public MARGINS(Thickness t)
            {
                Left = (int)t.Left;
                Right = (int)t.Right;
                Top = (int)t.Top;
                Bottom = (int)t.Bottom;
            }
            public int Left;
            public int Right;
            public int Top;
            public int Bottom;
        }

        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern void DwmExtendFrameIntoClientArea(
            IntPtr hwnd, ref MARGINS margins);

        [DllImport("dwmapi.dll", PreserveSig = false)]
        static extern bool DwmIsCompositionEnabled();

        // Extension Method for the window
        public static void ApplyGlass(this Window window)
        {
            try
            {
                if (DwmIsCompositionEnabled())
                {
                    IntPtr hwnd =
                        new WindowInteropHelper(window).Handle;
                    if (hwnd == IntPtr.Zero)
                        throw new InvalidOperationException(
                            "The Window must be shown before extending glass.");

                    window.Background = Brushes.Transparent;
                    HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor =
                        Colors.Transparent;

                    // Setting the MARGINS to -1 in all directions will
                    // cover the client area with Glass Effect
                    MARGINS margins = new MARGINS(new Thickness(-1));
                    DwmExtendFrameIntoClientArea(hwnd, ref margins);
                }
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }
    }
}



Then in the Window you want to add the Glass Effect you do:
C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += new RoutedEventHandler(MainWindow_Loaded);
    }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        this.ApplyGlass();
    }
}


Build, Run, and there you go GLASS..

Note that in order for this to work you must either have a Windows OS 6 (Vista) or greater OR handle the Exception that WILL be thrown by the ApplyGlass Method.

Hope this helps

All Best,

Raul Mainardi Neto
 
Share this answer
 
Comments
tpartee 31-Mar-11 19:07pm    
Huh, it works when I first launch the window, but after moving it around and/or interacting with it, the shell-rendered dropshadow disappears. I've also noticed that if your custom chrome (UI) in front of the DWM chrome is not rectangular (i.e. I have rounded corners) then the "blended" or "partial alpha" pixels always render at #FF000000 (pure black) instead of doing a transparency blend. =/ Any workarounds for these two issues?
Raul Mainardi Neto 1-Apr-11 19:36pm    
Hum... just made a simple test and wasn't able to reproduce the error that you are facing.. can you send me a sample with the error so I can provide you a solution?

Best regards
Raul Mainardi Neto
tpartee 7-Apr-11 14:34pm    
As is usual with 'examples' the code is often integrated into proprietary corporate code, so can't share that. Instead I created a more performant purely XAML workaround that not only perfectly mimics the effects, it will work on non-Aero systems like XP as well and allow alpha blending without flaw.
Thank you , Raul .

It was very helpful for me ! :)
 
Share this answer
 
-- Thanks a lot unvaluable infos...
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900