silverlight invisible hit area - silverlight

I would like to know how to make an invisible hit area in silverlight. I have created a UIelement leftmousebuttondown event that calls a planeprojectionfunction(this function rotates the UIelment on its X-axis). However as the element is rotated there is less surface area for the mouse to click on. Any ideas?

Use a canvas and set the opacity to 1%. It will increase CPU usage a little bit (usually not noticeable) but that's a quick and easy approach.
EDIT:
As KeithMahoney pointed out, don't forget to set the background to a brush with some color. Canvas by default does not have a background brush.

Related

Image glimmer or sparkle animation

I've been searching for awhile, but haven't been able to find anything. I'd like to be able to add kind of a glimmer or sparkly animation on an image element in wpf.
Essentially the effect here I'm after here is the same that you get with trading cards that are "foil's".
I'd like to have an image, and then be able to add this animation to it at will. I'm thinking maybe some kind of user control, or template possibly. Hopefully generic enough that I can just toss an image at it and it will just overlay the image and run.
Any ideas?
A simple construction that easily can be turned into a control is by nesting the image in a Grid and adding a second Grid (on top) as a sibling.
De second grid can be given a linear gradient brush that is primarily transparent but does contain a white glimmer.
This brush can be animated; you could move it and change the opacity of the grid/brush.
This way you do not change the image.

Winforms semi-transparent PNG over semi-transparent PNG

I think I must be missing something obvious, but I'm unable to find this after several hours of searching. Is there no way to use a PictureBox or other control to contain an image with partial transparent/alpha-blended pixels, and place that over another image and have the blending be based on the image under it?
For example, this produces the results I want:
Place a panel on a form.
Add an OnPaint handler.
In the OnPaint handler draw 1 PNG, then draw another PNG over it, using Graphics.DrawImage for both.
This does not:
Place a PictureBox on a form and set it to a PNG.
Place another PictureBox on the form and set it to a PNG.
Place the 2nd picture box over the first.
...even if the 2nd picture box is just empty and has a background color of Transparent, it still covers the picture below it.
I've read this stems from all winform controls being windows, so by nature they aren't transparent.
...but even the 15 year old platform I'm migrating from, Borland's VCL, had several windowless controls, so it's hard to imaging winforms doesn't at least have some easy solution?
My first example above is one answer, true, but that adds a lot of work when you can only use one big panel and draw all of your "controls" inside of it. Much nicer if you can have separate controls with separate mouse events/etc. Even if not an image control, and a control I have to draw myself, that would be fine, as long as I can just put one image in each control. In VCL they called this a "paint box", just a rectangle area you could place on a form and draw whatever you want on it. Has it's own mouse events, Bounds, etc. If you don't draw anything in it, it is like it's not even there (100% transparent) other than the fact it still gets mouse events, so can be used as a "hot spot" or "target" as well.
The PictureBox control supports transparency well, just set its BackColor property to Transparent. Which will make the pixels of its Parent visible as the background.
The rub is that the designer won't let you make the 2nd picture box a child of the 1st one. All you need is a wee bit of code in the constructor to re-parent it. And give it a new Location since that is relative from the parent. Like this:
public Form1() {
InitializeComponent();
pictureBox1.Controls.Add(pictureBox2);
pictureBox2.Location = new Point(0, 0);
pictureBox2.BackColor = Color.Transparent;
}
Don't hesitate to use OnPaint() btw.
Sorry, I just found this... once I decided to Google for "winforms transparent panel" instead of the searches I was doing before, the TransPictureBox example show seems to do exactly what I need:
Transparency Problem by Overlapped PictureBox's at C#
Looks like there are 2 parts to it:
Set WS_EX_TRANSPARENT for the window style
Override the "draw background" method (or optionally could probably make the control style Opaque).

How do I incorporate speed and/or acceleration into a (WPF) scrollviewer?

I have a WPF scrollViewer that I use for panning (MouseDown, MouseMove, MouseUp) and I would like to include an acceleration effect that incorporates inertia. So, if the mouse moves beyond a threshold speed and I release the mouse, it continues to pan but slows down as a function of the initial speed. Any ideas, thoughts or examples?
I've done basically this before and started by looking at ScrollViewer but in the end threw it away and used a Viewbox with a Canvas as the child for absolute positioning of content (I was supporting zoom aswel as scroll, it was basically DeepZoom without the scaled images for zooming so there was pixelation when zoomed right in). I wrote code for determining the gesture direction and the speed of the gesture from the mouse events and converted this to a scroll direction and speed and then animated the Canvas.Left and Canvas.Top properties of the scrollable content (which was in the Children collection of the Viewbox's child Canvas) with DoubleAnimation. It worked well, however there may be a better way...heres a thread suggesting hosting DeepZoom in WPF via a Frame control (although I wouldn't do it that way).
EDIT: Basically the ScrollViewer was just too restricting. Even if you get into the ControlTemplate and get references to the ScrollBars directly, it is the position of the Thumb of these scrollbars that you would need to animate and I'm pretty sure this is what I found I couldn't do (it was almost 3 years ago :)

WPF - how to best implement a panel with draggable/zoomable children?

I'm trying to modify the default graph viewer of the Graph# library because its user interface is awful (just try dragging a node outside of the boundaries, you'll see!)
The basic setup is this: there is a GraphCanvas control (inherited from Panel) which has children of Vertex and Edge control types. What I want to achieve is:
GraphCanvas has scroll bars if the contents do not fit in the screen;
GraphCanvas can also be scrolled by "dragging" it (just click on an empty space and drag);
GraphCanvas can be zoomed in and out (via CTRL+mouse wheel);
Vertices can be dragged around. If a vertex is dragged outside the current boundaries of GraphCanvas, the boundaries are increased. The scroll bars should reflect this, however the current viewport should not scroll away while the vertex is being dragged . The same goes if dragging a vertex reduces the boundaries of GraphCanvas - it should stay the same size until the drag operation is finished and resize only then. Automatically scrolling the viewport during a drag operation is awfully confusing and easily introduces dragging errors. See the original implementation if you want to know what I mean.
Although I've got a fair bit of experience with .NET, I'm still a complete beginner in WPF. My current attempt is (in the measure/arrange layout phase) to give each vertext the XY coordinate it desires (even if negative) and implement zooming/scrolling by handling mouse events on the GraphCanvas and modifying the RenderTransform property. Dragging just changes the XY coordinates on the specific vertex (probably triggering the re-layout of the whole thing which would be nice to avoid too). Scrollbars are implemented by placing the GraphCanvas inside a ScrollViewer and implementing IScrollInfo on the GraphCanvas.
Unfortunately there seems to be a problem: I can get mouse events on the GraphCanvas itself only if it has a background at the point. That would be OK, I want a white background anyway, but in the negative coordinates of the GraphCanvas it does not draw the background - and thus does not respond to mouse events.
I'm also wondering if I'm doing the Right Thing by allowing all my child controls (vertices and edges) to go into negative coordinates. How would you implement this?
Added: To clarify about the background problem check out the following screenshot:
(source: valts.21.lv)
What you see here is a simple Windows Forms form with a WPF Host control on it. That has a ScrollViewer in it, and the ScrollViewer has the GraphCanvas in it. The GraphCanvas contains 4 vertices and 6 edges.
The GraphCanvas is stretched to fill the ScrollViewer. But since some of the vertices are at negative coordinates, it has a RenderTransform applied which simply shifts everything to the right (TranslateTransform). It also has a white background brush.
Note the gray area on the left. That's still a part of the GraphCanvas, but the background brush somehow doesn't exted there. Also, if I left-click there with my mouse (not on a node, but on the gray area), I do NOT get an event. If I left-click on the white area, I get all events just fine.
Call CaptureMouse on canvas.mouseDown and ReleaseMouseCapture on mouse up. Also, if you set your canvas background to transparent it will still be hit testable
You can attach a 'Draggable' behavior to each element.

DropShadowBitmapEffect Doesn't work on TextBlock

Does anyone know why the DropShadowBitmapEffect and the EmbossBitmapEffect won't work on a TextBlock (not textBOX) in WPF? OuterGlow, Blur and Bevel seem to work fine.
The transparent background brush is apparently not the answer because you can get a dropshadow with a null background brush. The default softness on a dropshadow is 50% and if you have a small font, the softness dissipates the shadow too much. There seems to be a steep drop off around softness of 39% (at which point the shadow more or less disappears). Try setting it to 0 and slowly moving you're way up until you find a number that still shows the shadow.
Yet another note: the softness is definitely a factor, but be aware in Xaml the valid values are really only 0 to 1, but in Blend it shows it as a percentage up to 100. So if you set the value to 100 in Xaml, it will be completely dissipated.
The background brush = transparent solution still may work for the embossing effect
Bitmap effects work by looking at the post-rendered pixels and running standard image manipulation on them. It should only be dependent on the color of the pixels. I wonder if their algorithms don't work well on white. Try changing the color to see if that has an effect -- if it does, you might want to try putting a black panel underneath with drop shadow set on it.
Edit: The questioner found the answer
"Thanks for pointing me in the correct general direction. It wasn't the color of the text or the DropShadow that mattered, what is needed is to make the Background Brush on the TextBlock the Transparent Brush (Alpha = 0) instead of null."
Important Sidenote: you shouldn't really be using BitmapEffects any more. Use the Effect property based on ShaderModel effects introduced in .net 3.5 SP1, it uses hardware rendering and has far better performance.
More Information

Resources