17 July 2007

Focusing (graphically talking) controls


Few days ago my nephew show me a web called neopets, and there I watched plenty of mini flash games, I was thinking on the possibilities on WPF and I decided to start playing with the effects on runtime, here I show you how to change on runtime the focus of a control:
Download sample here



using System.Windows.Media.Effects;
using System.Windows.Threading;
using System.Windows.Input;

...


BlurBitmapEffect blur;
Button b;

System.Timers.Timer t_1;
public void AutoFocus(object o, EventArgs e)
{
b = (Button)o;
blur = new BlurBitmapEffect();
b.BitmapEffect = blur;
blur.Radius=5;
t_1 = new System.Timers.Timer(1);
t_1.Elapsed +=new System.Timers.ElapsedEventHandler(OnTime);
t_1.Start();
}

...


void OnTime(object sender, System.Timers.ElapsedEventArgs e)
{
this.Dispatcher.Invoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate
{
if (blur.Radius > 0)
blur.Radius--;
else
t_1.Stop();
return null;
}), null);
}

...


public void AutoDifuse(object o, EventArgs e)
{
BlurBitmapEffect blur = new BlurBitmapEffect();
blur.Radius = 5;
((Button)o).BitmapEffect = blur;
}

...



The code is easy to understand, the only matter is the strange way to use a Timer, because if you try to ommit the Dispatcher.Invoke you will have threading problems. The rest I think is easy to understand. Tomorrow I'll show you how to move controls on runtime


1 comment:

Anonymous said...

Didnt know where else to contact you. Regarding your article

http://www.codeproject.com/KB/menus/Ribbon_Panel.aspx

how would you feel if I was to integrate your code into the OpenRibbon project, www.walms.co.uk/OpenRibbon

dan@walms.co.uk