Using some Mouse Property Of a Rectangle in XAMLIn this article I am going to show how a rectangle's color change as mouse do different.For this application on my window I use a rectangle. Here in rectangle tag we found many property like as mousemove, mouseexit, mouseclick etc.On any event of mouse like as mouseup, mousedown, mouseclick etc. we can perform any type of operation. Here on every different behaviour of mouse I am changing the color of rectangle.
For this I design a window as in window.xml
<--!
<window x:class="Mouseoperation.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="Mouseoperation" height="300" width="359">
<grid>
<ellipse></ellipse>
<rectangle name="MyRect" height="100" width="300" strokethickness="10" stroke="Blue" fill="Red" mousedown="MyRectMouseDown" mouseenter="MyRectMouseEnter" mouseleave="MyRectMouseLeave" mousewheel="MyRectMouseWheel"></rectangle>
</grid>
</window>
-->
The window will become like as:
Figure 1: Our designing window
This is the window1.xaml.cs code, where we define that how the color of rectangale will change on every different behaviour of Mouse.
<--!
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Mouseoperation
{
public partial class Window1 : System.Windows.Window
{
public Window1()
{
InitializeComponent();
}
void MyRectMouseDown(object sender, System.EventArgs e)
{
MyRect.Fill = Brushes.Black;
}
void MyRectMouseEnter(object sender, System.EventArgs e)
{
MyRect.Fill = Brushes.Aqua;
}
void MyRectMouseLeave(object sender, System.EventArgs e)
{
MyRect.Fill = Brushes.DarkSeaGreen;
}
void MyRectMouseWheel(object sender, System.EventArgs e)
{
MyRect.Fill = Brushes.Magenta;
}
void MyRectMouseButtonLeft(object sender, System.EventArgs e)
{
MyRect.Fill = Brushes.MediumVioletRed;
}
}
}
-->
When we Run the Programme.
Figure 2 In Running Mode Application
When mouse comes inside the Rectangle
Figure 3 when mouse comes inside the Rectangle the color become change
When Mouse clicked inside the Rectangle.
Figure 4 when mouse clicked inside the Rectangle the color become change When mouse comes outside the rectangle.
Figure 5 When mouse go outside the Rectangale the color becomes change.