Yesterday I was asked how one can do mousewheel zooms in Silverlight 1.1 given that 1.1 doesn’t provide any mechanism for registering managed handlers for mousewheel events.
It can still be done, thanks to the fact that Silverlight 1.1 permits managed methods to be called from JavaScript. Here’s an example. Start with a XAML file containing a Canvas with a ScaleTransform. Here’s one that displays a smiley face:
<Canvas>
<Canvas.RenderTransform>
<ScaleTransform x:Name="ZoomTransform" />
</Canvas.RenderTransform>
<Ellipse Canvas.Left="20" Canvas.Top="20" Height="200"
Width="200" Stroke="Black" StrokeThickness="10" Fill="Yellow" />
<Ellipse Canvas.Left="80" Canvas.Top="80" Height="35"
Width="25" Stroke="Black" Fill="Black" />
<Ellipse Canvas.Left="140" Canvas.Top="80" Height="35"
Width="25" Stroke="Black" Fill="Black" />
<Path Data="M 70, 150 A 60, 60 0 0 0 170, 150" Stroke="Black"
StrokeThickness="15" StrokeStartLineCap="Round"
StrokeEndLineCap="Round" />
</Canvas>
Next, register the XAML code-behind class as a scriptable class and include scriptable ZoomIn and ZoomOut methods, too:
[System.Windows.Browser.Scriptable]
public partial class Page : Canvas
{
public void Page_Loaded(object o, EventArgs e)
{
InitializeComponent();
WebApplication.Current.RegisterScriptableObject
("magic", this);
}
[System.Windows.Browser.Scriptable]
public void ZoomIn()
{
ZoomTransform.ScaleX += 0.1;
ZoomTransform.ScaleY += 0.1;
}
[System.Windows.Browser.Scriptable]
public void ZoomOut()
{
ZoomTransform.ScaleX -= 0.1;
ZoomTransform.ScaleY -= 0.1;
}
}
Then, in the Silverlight control’s onLoad event handler (in JavaScript), register a JavaScript handler for mousewheel events. This example works in IE and Firefox, and maybe in other browsers as well:
var _control;
function onLoad(control, context, root)
{
_control = control;
if (window.addEventListener)
window.addEventListener('DOMMouseScroll',
OnMouseWheelTurned, false);
window.onmousewheel = document.onmousewheel =
OnMouseWheelTurned;
}
Finally, implement the mousewheel event handler in JavaScript and in it, include calls to the ZoomIn and ZoomOut methods written in C#:
function OnMouseWheelTurned(event)
{
var delta = 0;
if (!event)
event = window.event;
if (event.wheelDelta)
{
delta = event.wheelDelta;
if (window.opera)
delta = -delta;
}
else if (event.detail)
delta = -event.detail;
if (delta)
{
if (delta > 0)
_control.content.magic.ZoomIn();
else
_control.content.magic.ZoomOut();
}
if (event.preventDefault)
event.preventDefault();
event.returnValue = false;
}
Try it and you’ll see that it works very well. Silverlight 1.1’s DOM integration features make up for a variety of shortcomings in the platform itself. I rarely ever write a 1.1 app that doesn’t use DOM integration in one way or another!
On Feb 22 2008 9:26 PMBy jprosise
I understand you are talking about the now-called Silverlight 2.0?
Anyway, thanks for the nice tip! :)
You've been kicked (a good thing) - Trackback from DotNetKicks.com
its a bit of a show stopper on part of MS not to have out of the box support for mousewheel events
however your nice little trick solves that problem. thanks for sharing this really helpful tip :-)
Too bad this trick doesn't work in fullscreen mode: here the events captured in the HTML are not sent to the silverlight control...
Dear Reader, I present to you eighteenth in a infinite number of posts of " The Weekly Source Code
I've updated my Deep Zoom demo to add mouse wheel support for zooming. There is no way to capture mouse
I've updated my Deep Zoom demo to add mouse wheel support for zooming. There is no way to capture
Dear Reader, I present to you eighteenth in a infinite number of posts of " The Weekly Source Code ."
Jeff Proise has put up a nice little Silverlight page turning demo using an old Microsoft Systems Journal
Jeff Proise has put up a nice little Silverlight page turning demo using an old Microsoft Systems Journal
it look nice.
Hi, I am new to silverlight.
I really want to try the solution, but cant get it to work.
I am using expression blend.
Does the example work for website?
Please help!
reply[at]arai.se
Jeff Proise has put up a nice little Silverlight page turning demo using an old Microsoft Systems Journal
There are no superzoom (ie +12x zoom) comcapts like that, no. Part of the reason these cameras, as well as most comcapts in general use small sensors is to keep their size down. A dSLR size sensor needs much larger lenses for the same effective range.If you really want a big sensor with a long zoom, you're going to have to go for a dSLR. Both Nikon and Canon offer 18-200mm (basically 11.1x zoom) lenses for their respective mounts. Third party maker Tamron has a 18-270 (15x zoom) lens. Well actually I take that back [partially]. Strictly speaking you don't have to go with a dSLR. If you want something somewhat smaller, there is the aformentioned G1/Micro4:3 system. However while it does have a size and weight advantage over a dSLR, it's still going to be a fair bit larger and heavier and more expensive than an equivalent superzoom when coupled with upcoming 14-140mm (10x) lens. Again that's just the trade off of having the bigger sensor.