tags:

views:

1171

answers:

4

If I had a Canvas with n number of Visual objects of any shape or size, how would I highlight/outline a Visual object programmatically?

Is there something built into WPF to help me?

A: 

If you wrap those Visual objects in a Border ... and make the Opacity 0 by default ... then you could programmatically turn the Opacity to 1 whenever you need to.

Does that help?

cplotts
Will the Border always be a rectangle, or will it take the shape of the underlying object?
Dylan
To answer my own question, the Border will remain a square. Though this can work, I'm looking for the outline to be the same shape the Visual object.
Dylan
In that case, can you just duplicate the Visual (or the outer non-rectangular 'border' part of the visual), but give it a little more width and a little more height (so that it appears like a border) ... and then, of course, use the Opacity trick above? Would that work?
cplotts
+3  A: 

Having read the comments in the other answer -- in which you state that you wish the outline to conform to the shape of the visual -- I can only suggest you take a look at the BitmapEffects. There is one for an Outer Glow effect.

Duplicating the visuals themselves would probably be less efficient, and would probably produce a great number of complications due to other aspects of those items, such as any bindings.

Edit: In .NET 4.0, BitmapEffects property and the BitmapEffect class are obsolete. You'll get an exception thrown. Instead, you now have the System.Windows.Media.Effects.Effect class and its derived classes, along with properties such as Visual.VisualEffect.

Joel B Fant
lol, I just figured this out and was about to post it. I guess you get the rep.
Dylan
Whee. Glad you were looking in the right places. :)
Joel B Fant
Nice suggestion, don't know why I didn't think of it first ... especially given that I just made a mouse over effect the other day. <laugh> One thing to point out about BitmapEffects, however, is that they are a known performance issue. So, watch out for that.
cplotts
no longer applicable in .Net4; made an obsolete property.
metao
@metao: You should have also provided information on the replacement, if you wanted your comment and downvote to be more useful.
Joel B Fant
I felt that would be redundant, as cplotts already posted one. But I see you updated your answer, so everybody wins in any case. :)
metao
DO'H. Yeah, I missed that.
Joel B Fant
As others said, no more BitmapEffects on 4.0. (thank God!). To simulate the "outer glow" bitmap effect on 3.5, use the drop shadow effect and set the shadow depth to 0. That will do the trick for now.
Theo Zographos
+4  A: 

Joel has a great suggestion about using BitmapEffects.

However, if you can use .NET 3.5 SP1, I would steer you toward GPU rendered effects. So, instead of using UIElement.BitmapEffect, you would use UIElement.Effect. .NET 3.5 SP1 has two GPU effects built in: BlurEffect and DropShadowEffect. You can also create your own GPU effects ... and this is where you would use ShaderEffect.

Check out Greg Schechter's blog post series for more info.

Here is a piece of code for a mouse over effect that I just implemented a few days ago (it actually shows up sort of like an outer glow bitmap effect due to the ShadowDepth being 0, but not exactly):

<Path
    x:Name="mouseOverEffect"
    Width="80"
    Height="43.916"
    Stretch="None"
    Fill="#FFFFFFFF"
    Opacity="0"
>
    <Path.Data>
        <PathGeometry FillRule="Nonzero">
            <PathFigure IsClosed="True" StartPoint="39.9592899612151,25.9913931634531">
                <LineSegment Point="80.0000001464848,43.9159987905149"/>
                <LineSegment Point="39.9513899394755,4.97379893856246E-14"/>
                <LineSegment Point="1.77635636294422E-15,43.9159987905149"/>
                <LineSegment Point="39.9592899612151,25.9913931634531"/>
            </PathFigure>
        </PathGeometry>
    </Path.Data>
    <Path.Effect>
        <DropShadowEffect
            Color="#FFFFFFFF"
            BlurRadius="10"
            ShadowDepth="0"
        />
    </Path.Effect>
</Path>
cplotts
Unfortunately, I'm not able to use SP1 yet because some of our customers are in very restricted (ie government) environments.
Dylan
A: 

I have some problem regarding this. I am running the code in in Window7. I have 2 Path and also the Path Effect and data=Geometry Object. But Effect is working for the Last Path only.For the First one Data is not displaying. If i will remove the Effect then it is displaying. This is the Problem with Window7 32 bit only. Please give me some idea.

    <Border x:Name="brdSaved" Margin="5,2,0,12" CornerRadius="10" Grid.Row="0"
 VerticalAlignment="Center" HorizontalAlignment="Left" 
    Background="Black">
        <Path Name="pthSaved" Fill="White" SnapsToDevicePixels="True"
          Margin="6,1,7,4" StrokeThickness="0.3" Stroke="White"  
          Effect="{DynamicResource tbEffect}"
          VerticalAlignment="Center" HorizontalAlignment="Right"/>
    </Border>

Effect="{DynamicResource tbEffect}" VerticalAlignment="Center" HorizontalAlignment="Right"/>

In CS am assigning Geometry Object to Data