locked
How to defocus "backround" when i use opacity or alfa channel with object? RRS feed

  • Question

  • I want to defocus wpf form background when I using opacity. It is possible do it in expression blend? When I use blur effect, its defocus foregorund.

    Thanks for any idea.

    Tuesday, March 20, 2012 8:57 PM

All replies

  • Hi, would you mind maybe re-wording your question or using a different translator? Having trouble understanding your question.

    Please mark answers as helpful when used, and answered when completed.

    Tuesday, March 20, 2012 9:09 PM
  • How can I get the background visible though the transparent layer to seem distoted.
    Tuesday, March 20, 2012 9:44 PM
  • I'm sorry, still having trouble understanding. If something is transparent, it can't seem distorted, because it's transparent. Maybe a code example or an example of the effect you're looking to accomplish perhaps?

    Please mark answers as helpful when used, and answered when completed.

    Tuesday, March 20, 2012 11:27 PM
  • Hey Zeek.

    You will need to split your background items and your foreground items.

    If you place the blur effect on the layoutRoot, assuming that is where your background is... it will blur everything in the LayoutRoot.

    Something similar to this could work...

    <Grid x:Name="LayoutRoot" Background="White">
    		<Grid x:Name="myBackgroundItems">
    			<Grid.Effect>
    				<BlurEffect Radius="100"/>
    			</Grid.Effect>
    			<Rectangle Stroke="Black">
    				<Rectangle.Fill>
    					<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
    						<GradientStop Color="Black" Offset="0"/>
    						<GradientStop Offset="1" Color="White"/>
    					</LinearGradientBrush>
    				</Rectangle.Fill>
    			</Rectangle>
    		</Grid>
    		<Grid x:Name="myForegroundItems" Margin="0">
    			<TextBlock TextWrapping="Wrap" Text="TextBlock" Margin="0" FontSize="96" HorizontalAlignment="Center" VerticalAlignment="Center"/>
    		</Grid>
    	</Grid>

    This would blur the background grid but not the foreground grid.

    ~Christine

    Tuesday, March 20, 2012 11:48 PM
  • Thanks Christine ,it helped me a  little but it is not totally what I wanted.

    I want this windows aero effect for my transparent form. - this is background

    - this is transparent but little distorted

    Wednesday, March 21, 2012 12:23 AM
  • Ahhh!  Got it.

    Download this project and open it in blend.  

    http://wpfdraggableglass.codeplex.com/SourceControl/changeset/changes/480678c0bdde

    Look at this section of the c# code...

    #region Draggable glass functionality
    
    		private bool IsOnGlass(int lParam)
    		{
    			int x = lParam << 16 >> 16, y = lParam >> 16;
    			var point = PointFromScreen(new Point(x, y));
    
    			// In XAML: <Grid x:Name="windowGrid">...</Grid>
    			var result = VisualTreeHelper.HitTest(windowGrid, point);
    
    			if (result != null)
    			{
    				// A control was hit - it may be the grid if it has a background
    				// texture or gradient over the glass
    				return result.VisualHit == windowGrid;
    			}
    
    			// Nothing was hit - assume that this area is covered by glass anyway
    			return true;
    		}
    
    		private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    		{
    			switch (msg)
    			{
    				// Ignore clicks if desktop composition isn't enabled
    				case DwmApiInterop.WM_NCHITTEST:
    					if (DwmApiInterop.IsCompositionEnabled()
    						&& DwmApiInterop.IsOnClientArea(hwnd, msg, wParam, lParam)
    						&& IsOnGlass(lParam.ToInt32()))
    					{
    						handled = true;
    						return new IntPtr(DwmApiInterop.HTCAPTION);
    					}
    
    					return IntPtr.Zero;
    
    				// Also toggle glass painting on this window when desktop composition is toggled
    				case DwmApiInterop.WM_DWMCOMPOSITIONCHANGED:
    					try
    					{
    						AdjustGlassFrame();
    					}
    					catch (InvalidOperationException)
    					{
    						FallbackPaint();
    					}
    					return IntPtr.Zero;
    
    				default:
    					return IntPtr.Zero;
    			}
    		}
    
    		#endregion
    
    		#region Aero Glass extensions - implementation details not essential to this sample
    
    		private IntPtr hwnd;
    		private HwndSource hsource;
    
    		private void Window_SourceInitialized(object sender, EventArgs e)
    		{
    			try
    			{
    				if ((hwnd = new WindowInteropHelper(this).Handle) == IntPtr.Zero)
    				{
    					throw new InvalidOperationException("Could not get window handle for the main window.");
    				}
    
    				hsource = HwndSource.FromHwnd(hwnd);
    				hsource.AddHook(WndProc);
    
    				AdjustGlassFrame();
    			}
    			catch (InvalidOperationException)
    			{
    				FallbackPaint();
    			}
    		}
    
    		private void AdjustGlassFrame()
    		{
    			if (DwmApiInterop.IsCompositionEnabled())
    			{
    				ExtendGlassIntoClientArea(0, 0, 32, 35);
    			}
    			else
    			{
    				FallbackPaint();
    			}
    		}
    
    		private void ExtendGlassIntoClientArea(int left, int right, int top, int bottom)
    		{
    			var margins = new MARGINS { cxLeftWidth = left, cxRightWidth = right, cyTopHeight = top, cyBottomHeight = bottom };
    			int hresult = DwmApiInterop.ExtendFrameIntoClientArea(hwnd, ref margins);
    
    			if (hresult == 0)
    			{
    				hsource.CompositionTarget.BackgroundColor = Colors.Transparent;
    				Background = Brushes.Transparent;
    			}
    			else
    			{
    				throw new InvalidOperationException("Could not extend glass window frames in the main window.");
    			}
    		}
    
    		private void FallbackPaint()
    		{
    			Background = Brushes.White;
    		}
    
    		#endregion

    I'd like to tell you I completely understand that code, but I couldn't keep a straight face while fibbing to you. :D  But it seems to work.

    Let us know if it helps.

    ~Christine

    Here is the effect...

    .

    .

    .

    Sorry I forgot the class file...

    /* 
     * Copyright (c) 2011 NOVALISTIC
     * 
     * Permission is hereby granted, free of charge, to any person obtaining a copy
     * of this software and associated documentation files (the "Software"), to deal
     * in the Software without restriction, including without limitation the rights
     * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     * copies of the Software, and to permit persons to whom the Software is
     * furnished to do so, subject to the following conditions:
     * 
     * The above copyright notice and this permission notice shall be included in
     * all copies or substantial portions of the Software.
     * 
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
     * THE SOFTWARE.
     */
    
    using System;
    using System.Runtime.InteropServices;
    using System.Security;
    
    namespace WPFDraggableGlass
    {
    	#region Structures
    
    	/// <summary>
    	/// Defines a structure containing margin dimensions.
    	/// </summary>
    	[StructLayout(LayoutKind.Sequential)]
    	public struct MARGINS
    	{
    		public int cxLeftWidth, 
    			cxRightWidth, 
    			cyTopHeight, 
    			cyBottomHeight;
    	}
    
    	#endregion
    
    	public class DwmApiInterop
    	{
    		#region Constants
    
    		public const int HTCLIENT = 0x1;
    		public const int HTCAPTION = 0x2;
    
    		public const int WM_NCHITTEST = 0x84;
    		public const int WM_DWMCOMPOSITIONCHANGED = 0x31E;
    
    		#endregion
    
    		#region Helper methods
    
    		/// <summary>
    		/// Determines whether the cursor is on the client area.
    		/// </summary>
    		/// <param name="hWnd">The window handle that is receiving and processing the window message.</param>
    		/// <param name="uMsg">The window message.</param>
    		/// <param name="wParam">Additional message information.</param>
    		/// <param name="lParam">Additional message information.</param>
    		/// <returns>true if the cursor is on the client area; otherwise, false.</returns>
    		public static bool IsOnClientArea(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam)
    		{
    			if (uMsg == WM_NCHITTEST)
    			{
    				if (DefWindowProc(hWnd, uMsg, wParam, lParam).ToInt32() == HTCLIENT)
    				{
    					return true;
    				}
    			}
    
    			return false;
    		}
    
    		#endregion
    
    		#region Managed wrapper methods
    
    		/// <summary>
    		/// Determines whether desktop composition is enabled on the client system.
    		/// </summary>
    		/// <returns>true if desktop composition is enabled; otherwise, false.</returns>
    		public static bool IsCompositionEnabled()
    		{
    			bool isEnabled = false;
    			NativeMethods.DwmIsCompositionEnabled(ref isEnabled);
    			return isEnabled;
    		}
    
    		/// <summary>
    		/// Extends the glass frame into the client area.
    		/// </summary>
    		/// <param name="hWnd">The window handle whose client area to extend glass into.</param>
    		/// <param name="margins">The amount of glass to extend around the client area.</param>
    		/// <returns>S_OK on success; otherwise, an HRESULT error code.</returns>
    		public static int ExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins)
    		{
    			return NativeMethods.DwmExtendFrameIntoClientArea(hWnd, ref margins);
    		}
    
    		/// <summary>
    		/// Invokes the default window message procedure for the given window handle.
    		/// </summary>
    		/// <param name="hWnd">The window handle that is receiving and processing the window message.</param>
    		/// <param name="uMsg">The window message.</param>
    		/// <param name="wParam">Additional message information.</param>
    		/// <param name="lParam">Additional message information.</param>
    		/// <returns>The result of the message processing, which depends on the message.</returns>
    		public static IntPtr DefWindowProc(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam)
    		{
    			return NativeMethods.DefWindowProc(hWnd, uMsg, wParam, lParam);
    		}
    
    		#endregion
    	}
    
    	[SuppressUnmanagedCodeSecurity]
    	internal static class NativeMethods
    	{
    		[DllImport("dwmapi.dll")]
    		internal static extern void DwmIsCompositionEnabled(ref bool isEnabled);
    
    		[DllImport("dwmapi.dll")]
    		internal static extern int DwmExtendFrameIntoClientArea(IntPtr hWnd, ref MARGINS margins);
    
    		[DllImport("user32.dll")]
    		internal static extern IntPtr DefWindowProc(IntPtr hWnd, int uMsg, IntPtr wParam, IntPtr lParam);
    	}
    }


    Wednesday, March 21, 2012 1:50 AM
  • But how to set this effect for window grid ? When allows transparency in main window is checked, this effect doesnt work for transparent grid. Sorry when it looks like stupid question.
    Wednesday, March 21, 2012 9:53 AM
  • One of the threads I read last night suggested it wasn't possible on a windowless window.  However, it was an old post and I've only found a dozen or so posts or blogs related to WPF and Aero style.  You'd think there'd be more since it was introduced with Vista.

    What I have found all seem to point to this blog...

    http://blogs.msdn.com/b/adam_nathan/archive/2006/05/04/589686.aspx

    Try his steps and see if it will work.  Sadly the pictures aren't showing so it isn't really clear, when skimming over it, what type of window he is using.

    I'll play around with it this afternoon if you haven't found a solution by then.

    If you do find a solution, please share it.  You've got me kind of excited about the possibilities with this!

    ~Christine

    And no question is ever stupid!  It is how we all learn. :)

    ...

    I forgot this blog kept coming up in my searches but I didn't get much from it.

    http://blogs.msdn.com/b/wpfsdk/archive/2008/09/08/custom-window-chrome-in-wpf.aspx

    And this one is on forcing wpf to use a specific windows theme, such as the Aero style...

    http://arbel.net/2006/11/03/forcing-wpf-to-use-a-specific-windows-theme/

    And here is a control someone posted in code project...

    http://www.c-sharpcorner.com/uploadfile/jonpreecebsc/C-Sharp-windows-aero-style-wizard-control/

    Wednesday, March 21, 2012 1:16 PM