Windows 7 & DWN: did you know that not all windows must obey Flip3D and Aero Peek

    You probably know that in Vista and Windows 7 there is a Flip3D function , and in Windows 7 there is also Aero Peek . But you probably do not know that you can make your window (application) not obey the rules for all windows in these Windows features. To do this, you should study the Desktop Window Manager API . So where to start? As always, if we are writing an application in .net (WinForms or WPF), we need to import the necessary methods. It is worth noting that in the case of WinForms it is very simple to get HWND windows, for this there is the Form :: Handle property , in the case of WPF you can use the WindowInteropHelper class , so you can write HWND of the main window like this:

    Aero peekFlip3d





    1. IntPtr hwnd = new WindowInteropHelper(Application.Current.MainWindow).Handle;
    * This source code was highlighted with Source Code Highlighter.


    Now back to the necessary functions. The first function that you should pay attention to is DwmIsCompositionEnabled , it allows us to determine if Aero Glass is enabled on the computer:

    1. public partial class FormSample : Form
    2. {
    3.   [DllImport("dwmapi.dll", PreserveSig = false)]
    4.   public static extern bool DwmIsCompositionEnabled();
    5.   public FormSample()
    6.   {
    7.     InitializeComponent();
    8.     if (Environment.OSVersion.Version.Major < 6)
    9.     {
    10.       // Dwm не работает, старая версия Windows
    11.     }
    12.     else if (!DwmIsCompositionEnabled())
    13.     {
    14.       // Aero Glass и Aero 3D отключены (не поддерживаются)
    15.     }
    16.     else
    17.     {
    18.       // Aero Glass и Aero 3D работают
    19.     }
    20.   }
    21. }
    * This source code was highlighted with Source Code Highlighter.


    Ok, now we know when you can “play” with Dwm, the next function that will help us work with Dwn is DwmSetWindowAttribute . This is the main function with which we can control our window and tell the system how to display it.

    For example, the first action. The task, we want to make such an application that is displayed even when the user turns on the Aero Peek mode (an action similar to a gadget). Imagine a bunch of running windows on your monitor, you are too lazy to look for the desired window using WinKey + Tab, but you want to see the windows you need using WinKey + Space - it can be a regular messenger, and some kind of profiler with which you follow work of something, well, and much more. So, for example, here in this article “Joel 'Jaykul' Bennett - Fun with PInvoke and Aero Peek “describes how to do this with the popular Miranda messenger (from this article, I was interested in Dwn and what features it still provides). True, this article uses the DwmNCRenderingPolicy flags, which are actually suitable for setting values ​​when using the DWMWA_NCRENDERING_POLICY flag, rather than DWMWA_EXCLUDED_FROM_PEEK. For a regular form, this can be done like this:

    1. public partial class FormSample : Form
    2. {
    3.   [Flags]
    4.   public enum DwmWindowAttribute
    5.   {
    6.     ExcludedFromPeek = 12
    7.   }
    8.   [DllImport("dwmapi.dll", PreserveSig = false)]
    9.   public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
    10.   public static void RemoveFromAeroPeek(IntPtr hwnd)
    11.   {
    12.     int attrValue = 1; // TRUE
    13.     DwmSetWindowAttribute(hwnd, (int)DwmWindowAttribute.ExcludedFromPeek, ref attrValue, sizeof(int));
    14.   }
    15.   public FormSample()
    16.   {
    17.     InitializeComponent();
    18.     // Делаем видимым при Aero Peek
    19.     RemoveFromAeroPeek(Handle);
    20.   }
    21. }
    * This source code was highlighted with Source Code Highlighter.


    The result will be like this (to make it more clear what I'm writing about):

    Remove Aero Peek Result

    You can also make your window disobey Aero 3D rules (this is when we use WinKey + Tab to switch between windows). To do this, consider the attribute DWMWA_FLIP3D_POLICY , for it you can set the values ​​to “Normal behavior”, “Show on top of 3D”, “Show on 3D”, though either “normal behavior” or “show on 3D” worked for me, I couldn’t do it on top .

    1. public partial class FormSample : Form
    2. {
    3.   [Flags]
    4.   public enum DwmWindowAttribute
    5.   {
    6.     Flip3DPolicy = 8
    7.   }
    8.   // Flip 3D policies
    9.   public enum Flip3DPolicy
    10.   {
    11.     Default = 0,
    12.     ExcludeBelow,
    13.     ExcludeAbove
    14.   }
    15.   [DllImport("dwmapi.dll", PreserveSig = false)]
    16.   public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
    17.   public static void SetFlip3DPolicy(IntPtr hwnd)
    18.   {
    19.     int attrValue = (int)Flip3DPolicy.ExcludeBelow;
    20.     DwmSetWindowAttribute(hwnd, (int)DwmWindowAttribute.Flip3DPolicy, ref attrValue, sizeof(int));
    21.   }
    22.   public FormSample()
    23.   {
    24.     InitializeComponent();
    25.     SetFlip3DPolicy(Handle);
    26.   }
    27. }
    * This source code was highlighted with Source Code Highlighter.


    The result is as follows: How it can be used is already more difficult to imagine. Well ... my business is to tell :) Good luck!

     Set 3DFlip Policy Result



    Progg it

    Also popular now: