7

我看到了无边界窗口周围阴影的这段代码,但这是我的问题。using System.Windows.Interop;有下划线,我在参考文献中找不到。在public static void DropShadowToWindow(Window window)这个窗口中也有下划线,所以我猜它与互操作有关......

using System.Drawing.Printing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

class DwmDropShadow
{

    [DllImport("dwmapi.dll", PreserveSig = true)]
    private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int   attrValue, int attrSize);

    [DllImport("dwmapi.dll")]
    private static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref Margins pMarInset);

    /// <summary>
    /// Drops a standard shadow to a WPF Window, even if the window isborderless. Only works with DWM (Vista and Seven).
    /// This method is much more efficient than setting AllowsTransparency to true and using the DropShadow effect,
    /// as AllowsTransparency involves a huge permormance issue (hardware acceleration is turned off for all the window).
    /// </summary>
    /// <param name="window">Window to which the shadow will be applied</param>
    public static void DropShadowToWindow(Window window)
    {
        if (!DropShadow(window))
        {
             window.SourceInitialized += new EventHandler(window_SourceInitialized);
        }
    }

    private static void window_SourceInitialized(object sender, EventArgs e) //fixed typo
    {
        Window window = (Window)sender;

        DropShadow(window);

        window.SourceInitialized -= new EventHandler(window_SourceInitialized);
    }

    /// <summary>
    /// The actual method that makes API calls to drop the shadow to the window
    /// </summary>
    /// <param name="window">Window to which the shadow will be applied</param>
    /// <returns>True if the method succeeded, false if not</returns>
    private static bool DropShadow(Window window)
    {
        try
        {
            WindowInteropHelper helper = new WindowInteropHelper(window);
            int val = 2;
            int ret1 = DwmSetWindowAttribute(helper.Handle, 2, ref val, 4);

            if (ret1 == 0)
            {
                 Margins m = new Margins { Bottom = 0, Left = 0, Right = 0, Top = 0 };
                 int ret2 = DwmExtendFrameIntoClientArea(helper.Handle, ref m);
                  return ret2 == 0;
            }
            else
            {
                 return false;
            }
        }
        catch (Exception ex)
        {
            // Probably dwmapi.dll not found (incompatible OS)
            return false;
        }
    }
}
4

2 回答 2

9

WindowsBase.DLL在 .NET Framework 3.0 中引入。它位于 c:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\WindowsBase.dll

于 2013-10-18T22:47:46.337 回答
5

以防万一有人来这里搜索System.Windows.Interop.CompositionMode:它没有进入最终的 .Net 4.5 版本。

请参阅http://visualstudio.uservoice.com/forums/121579-visual-studio/suggestions/2644120-bring-back-the-hwndhost-isredirected-and-compositi?page=2&per_page=20

于 2014-08-04T15:00:51.790 回答