4

Is there any api or something that we make sure, Glass effect is already actived? In some codes that i saw, if DllNotFoundException throws, then they make sure it's not active or not exists. is there a better or standard way?

This is the solution for Using Aero Effect to extend glass area in WPF.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Jalal
  • 6,594
  • 9
  • 63
  • 100
  • For a list of DWM functions, refer to [DWM Functions](http://msdn.microsoft.com/en-us/library/aa969527.aspx). – Jeff Mercado Feb 25 '11 at 07:08
  • Are you targeting versions of Windows prior to Windows Vista (such as Windows XP)? If not, there's no reason to check for the `DllNotFoundException`. The purpose of that test is not to determine if the Aero effect is *enabled*, but in case the host operating system does not *support* the Desktop Window Manager (DWM) functions responsible for driving the Aero interface. – Cody Gray - on strike Feb 25 '11 at 07:28

1 Answers1

10

On this MSDN page it suggests you can detect Glass using DwmIsCompositionEnabled:

When the status of desktop composition is changed, a WM_DWMCOMPOSITIONCHANGED message is broadcast. There are no parameters telling you if it's being enabled or disabled, so it's up to you to call DwmIsCompositionEnabled if you're interested. The code to do the check is straightforward-the tricky part is deciding how you want your window to look if composition is disabled.

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

// Check to see if composition is Enabled
if (Environment.OSVersion.Version.Major >= 6 && DwmIsCompositionEnabled())
{
    // enable glass rendering
}
else
{
    // fallback rendering
}

However I'm not sure whether you can "Enable Aero" but "Disable Glass" and if so, what the result of the method would be.

Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275
  • This doesn't really address the same problem that catching `DllNotFoundException` does. On versions of Windows prior to Vista, `dwmapi.dll` does not exist. Trying to call functions from that DLL will therefore fail. You can only call `DwmIsCompositionEnabled` successfully if the host operating system is Vista or later. – Cody Gray - on strike Feb 25 '11 at 07:27
  • 1
    mmmm :-? I think if we migrate it with `Environment.OS.Version >= 6` then it will be complete! – Jalal Feb 25 '11 at 07:36
  • @Jalal: Indeed, that's the exception-proof way of doing things. – Cody Gray - on strike Feb 25 '11 at 07:44
  • Apologies - I didn't consider pre-Vista. If you need to target them, checking the OS.Version as you suggest might be the best way. – Danny Tuppeny Feb 25 '11 at 09:00
  • Environment.OSVersion.Version.Major >= 6 this will only be true if vista or newer – Hooch Sep 29 '11 at 16:12
  • @DannyTuppeny In documentation for the `PreserveSig` field is: "When you set the PreserveSig field to false, the resulting method signature contains a void return type instead of an integer (HRESULT) return type." So, how it is possible that your signature `public static extern bool DwmIsCompositionEnabled()` works (and it works, I tested it :))? According to the documentation, correct form is: `public static extern void DwmIsCompositionEnabled(out bool enabled)` – Dominik Palo Feb 26 '16 at 11:41
  • @DominikPalo I have no idea =D Someone has asked a similar thing before, but it wasn't really answered! https://social.msdn.microsoft.com/Forums/vstudio/en-US/04f36843-ccbc-40e0-8017-8253c4aa139e/how-does-dllimportattributepreservesig-handle-modified-signatures?forum=netfxbcl – Danny Tuppeny Feb 26 '16 at 17:57