User Id :    Password :      New Member   Forgot Password
 
Using some Mouse Property Of a Rectangle in XAML
Description This article is showing that how a rectangle control use in XAML.How we can perform mouse operation on rectangle?   No. of Views     986
  Rating     0
Author Rahul Saxena   Posted On     05 Aug 2011
Tags WPF,General,WPF : How To,XAML    

Sample Code   Download Code

In 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.

About Author

About Author Rahul shows great interests in working with Microsoft technologies. He specializes in the implementation of DataBase & Graphics. His area of expertise includes: C#, ASP.NET,ADO.NET,Windows Forms & Web Services. He hails from background , Master's in Computer Application. With programming he loves photography, traveling and reading books. Rahul Saxena
No Photo
 
Country India
Company N/A
Home Page N/A

Rate this article

Rating options from poor, fair, good, very good to excelent.  
 

Comments

     
Write your comment here.  
Comment
Verification Code   
  
    
 
Section sponsored by