Thursday, April 21, 2011

Five Minute Silverlight 5 Aides-Memoire #4 – Mouse Click Count

Rainbow Rectangle
  1. <Rectangle Width="154"
  2.            Height="77"
  3.            MouseLeftButtonDown="RectangleMouseLeftButtonDown">
  4.     <Rectangle.Fill>
  5.         <LinearGradientBrush x:Name="GradientFill" StartPoint="0,0" EndPoint="1,1">
  6.             <GradientStop Offset="0" Color="Red" />
  7.             <GradientStop Offset="0.1667" Color="Orange" />
  8.             <GradientStop Offset="0.334" Color="Yellow" />
  9.             <GradientStop Offset="0.5001" Color="Green" />
  10.             <GradientStop Offset="0.6668" Color="Blue" />
  11.             <GradientStop Offset="0.8335" Color="Indigo" />
  12.             <GradientStop Offset="1" Color="Violet" />
  13.         </LinearGradientBrush>
  14.     </Rectangle.Fill>
  15. </Rectangle>
  16. <TextBlock x:Name="ClickCountTextBlock"
  17.            Foreground="White"
  18.            HorizontalAlignment="Center"
  19.            VerticalAlignment="Center"
  20.            FontSize="40"
  21.            Opacity="0.4"
  22.            Text="" />

Click Count in MouseEventArgs
  1. private void RectangleMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  2. {
  3.     this.ClickCountTextBlock.Text = e.ClickCount.ToString();
  4.     var topLeft = new Point(0, 0);
  5.     var topRight = new Point(1, 0);
  6.     var bottomLeft = new Point(0, 1);
  7.     var bottomRight = new Point(1, 1);
  8.  
  9.     switch (e.ClickCount)
  10.     {
  11.         case 2:
  12.             this.GradientFill.StartPoint = topRight;
  13.             this.GradientFill.EndPoint = bottomLeft;
  14.             break;
  15.         case 3:
  16.             this.GradientFill.StartPoint = bottomRight;
  17.             this.GradientFill.EndPoint = topLeft;
  18.             break;
  19.         case 4:
  20.             this.GradientFill.StartPoint = bottomLeft;
  21.             this.GradientFill.EndPoint = topRight;
  22.             break;
  23.         default:
  24.             this.GradientFill.StartPoint = topLeft;
  25.             this.GradientFill.EndPoint = bottomRight;
  26.             break;
  27.     }
  28. }

DoubleTripleClick

No comments: