Click here to Skip to main content
15,893,622 members
Articles / Desktop Programming / Windows Forms

Task Manager

Rate me:
Please Sign up or sign in to vote.
4.90/5 (105 votes)
1 Jan 2010CPOL4 min read 204.7K   21K   229  
Manage your daily tasks and To-Do list using some exciting features of Windows 7.
using System;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Runtime.InteropServices;
using MS.WindowsAPICodePack.Internal;


namespace Microsoft.WindowsAPICodePack.Shell
{
    
    /// <summary>
    /// WPF Glass Window
    /// Inherit from this window class to enable glass on a WPF window
    /// </summary>
    public class GlassWindow : Window
    {
        #region properties

        /// <summary>
        /// Get determines if AeroGlass is enabled on the desktop. Set enables/disables AreoGlass on the desktop.
        /// </summary>
        public bool AeroGlassCompositionEnabled
        {
            set
            {
                DesktopWindowManagerNativeMethods.DwmEnableComposition(
                    value ? CompositionEnable.DWM_EC_ENABLECOMPOSITION : CompositionEnable.DWM_EC_DISABLECOMPOSITION );
            }
            get
            {
                return DesktopWindowManagerNativeMethods.DwmIsCompositionEnabled( );
            }
        }

        #endregion

        #region events

        /// <summary>
        /// Fires when the availability of Glass effect changes.
        /// </summary>
        public event AeroGlassCompositionChangedEvent AeroGlassCompositionChanged;

        #endregion

        #region operations

        /// <summary>
        /// Makes the background of current window transparent from both Wpf and Windows Perspective
        /// </summary>
        public void SetAeroGlassTransparency( )
        {
            // Set the Background to transparent from Win32 perpective 
            HwndSource.FromHwnd( windowHandle ).CompositionTarget.BackgroundColor = System.Windows.Media.Colors.Transparent;

            // Set the Background to transparent from WPF perpective 
            this.Background = Brushes.Transparent;
        }

        /// <summary>
        /// Excludes a UI element from the AeroGlass frame.
        /// </summary>
        /// <param name="element">The element to exclude.</param>
        /// <remarks>Many non-WPF rendered controls (i.e., the ExplorerBrowser control) will not 
        /// render properly on top of an AeroGlass frame. </remarks>
        public void ExcludeElementFromAeroGlass( FrameworkElement element )
        {
            if( AeroGlassCompositionEnabled )
            {
                // calculate total size of window nonclient area
                HwndSource hwndSource = PresentationSource.FromVisual( this ) as HwndSource;
                CoreNativeMethods.RECT windowRect = new CoreNativeMethods.RECT( );
                CoreNativeMethods.RECT clientRect = new CoreNativeMethods.RECT( );
                DesktopWindowManagerNativeMethods.GetWindowRect( hwndSource.Handle, ref windowRect );
                DesktopWindowManagerNativeMethods.GetClientRect( hwndSource.Handle, ref clientRect );
                Size nonClientSize =
                    new Size(
                        (double)(windowRect.right - windowRect.left) - (double)(clientRect.right - clientRect.left),
                        (double)(windowRect.bottom - windowRect.top) - (double)(clientRect.bottom - clientRect.top) );

                // calculate size of element relative to nonclient area
                GeneralTransform transform = element.TransformToAncestor( this );
                Point topLeftFrame =
                    transform.Transform( new Point( 0, 0 ) );
                Point bottomRightFrame =
                    transform.Transform(
                        new Point(
                            element.ActualWidth + nonClientSize.Width,
                            element.ActualHeight + nonClientSize.Height ) );

                // Create a margin structure
                MARGINS margins = new MARGINS( );
                margins.cxLeftWidth = (int)topLeftFrame.X;
                margins.cxRightWidth = (int)(this.ActualWidth - bottomRightFrame.X);
                margins.cyTopHeight = (int)(topLeftFrame.Y);
                margins.cyBottomHeight = (int)(this.ActualHeight - bottomRightFrame.Y);

                // Extend the Frame into client area
                DesktopWindowManagerNativeMethods.DwmExtendFrameIntoClientArea( windowHandle, ref margins );
            }
        }

        /// <summary>
        /// Resets the AeroGlass exclusion area.
        /// </summary>
        public void ResetAreoGlass()
        {
            MARGINS margins = new MARGINS( true );
            DesktopWindowManagerNativeMethods.DwmExtendFrameIntoClientArea( windowHandle, ref margins );
        }

        #endregion

        #region implementation
        private IntPtr windowHandle;

        private IntPtr WndProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled )
        {
            if( msg == DWMMessages.WM_DWMCOMPOSITIONCHANGED
                || msg == DWMMessages.WM_DWMNCRENDERINGCHANGED )
            {
                if( AeroGlassCompositionChanged != null )
                {
                    AeroGlassCompositionChanged.Invoke(
                        this,
                        new AeroGlassCompositionChangedEvenArgs( AeroGlassCompositionEnabled ) );
                }

                handled = true;
            }
            return IntPtr.Zero;
        }

        /// <summary>
        /// OnSourceInitialized
        /// Override SourceInitialized to initialize windowHandle for this window.
        /// A valid windowHandle is available only after the sourceInitialized is completed
        /// </summary>
        /// <param name="e">EventArgs</param>
        protected override void OnSourceInitialized( EventArgs e )
        {
            base.OnSourceInitialized( e );
            WindowInteropHelper interopHelper = new WindowInteropHelper( this );
            this.windowHandle = interopHelper.Handle;

            // add Window Proc hook to capture DWM messages
            HwndSource source = HwndSource.FromHwnd( windowHandle );
            source.AddHook( new HwndSourceHook( WndProc ) );

            ResetAreoGlass( );
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Chief Technology Officer
Pakistan Pakistan
Passion and positive dedication is essential part of success. I believe on hardworking and sharing knowledge with others. I always try to be a better than I am and think positive for positive result.

My Blogs

My Linked-In Profile

Comments and Discussions