|
|
|
|
|
Home
»
WPF
»
Input Events in WPF: Part 2
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
711
|
|
Downloads :
|
0
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
|
As you learn about keyboard event
in my previous article and learn that the Input Events divided into three
categories Keyboard events, Mouse events, Stylus events. This part of article explain about
the Mouse events.
Mouse Events:
There are so many tasks which performed by the mouse event but the most
fundamental events you to react when the mouse is moved over an element. Both
are direct events, which means they don't use tunneling or bubbling. Instead,
they originate in one element and are raised by just that element. This makes
sense because of the way controls are nested in a WPF window. All mouse button
events provide a MouseButtonEventArgs object. The MouseButtonEventArgs class
derives from MouseEventArgs (which means it includes the same coordinate and
button state information) and it adds a few members.
Example of the Mouse Event
Xaml Code
<Grid
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"
mc:Ignorable="d"
x:Class="InputExamples.MousePosition"
Width="640"
Height="480">
<Label
d:LayoutOverrides="Height"
x:Name="firstCoordinates"/>
<Rectangle
Stroke="#FF000000"
Fill="#FFFFFFFF"
x:Name="Rectangle"/>
<Rectangle
Stroke="#FF000000"
Fill="#FFFFFFFF"
x:Name="secondEllipse"/>
<Ellipse
d:LayoutOverrides="Height"
Stroke="#FF000000"
Width="14"
Height="14"
x:Name="firstEllipse"/>
<Ellipse
d:LayoutOverrides="Width"
Stroke="#FF000000"
Width="14"
Height="14"
x:Name="fourthEllipse"/>
<Ellipse
d:LayoutOverrides="Width"
Stroke="#FF000000"
Width="14"
Height="14"
x:Name="thirdEllipse"/>
<Label
d:LayoutOverrides="Width,
Height"
x:Name="secondCoordinates"
Margin="287,254,0,192"
HorizontalAlignment="Left"
Width="117"
/>
<Label
d:LayoutOverrides="Width,
Height"
Width="100"
x:Name="thirdCoordinates"
Margin="422,254,118,192"
/>
<Label
d:LayoutOverrides="Width,
Height"
x:Name="fourthCoordinates"
Margin="137,257,378,192"
/>
<Rectangle
Fill="blue"
Width="16"
Height="16"
x:Name="DragEllipse"
Margin="311,232,313,232"
/>
</Grid>
Vb Code
Imports System
Imports
System.Net
Imports
System.Windows
Imports
System.Windows.Controls
Imports
System.Windows.Data
Imports
System.Windows.Input
Imports
System.Windows.Media
Imports
System.Windows.Media.Animation
Imports
System.Windows.Navigation
Imports
System.Windows.Shapes
Namespace
InputExamples
Partial Public
Class MousePosition
Private ellipseTransform
As New
TranslateTransform()
Public Sub
New()
Me.InitializeComponent()
End Sub
Protected
Overrides Sub
OnInitialized(ByVal e
As EventArgs)
MyBase.OnInitialized(e)
DragEllipse.RenderTransform = ellipseTransform
AddHandler
CompositionTarget.Rendering,
AddressOf Me.CompositionTarget_Rendering
End Sub
Private Sub
CompositionTarget_Rendering(ByVal sender
As Object,
ByVal e As
EventArgs)
Dim mouse1
As Point =
Mouse.GetPosition(firstEllipse)
Dim mouse2
As Point =
Mouse.GetPosition(secondEllipse)
Dim mouse3
As Point =
Mouse.GetPosition(thirdEllipse)
Dim mouse4
As Point =
Mouse.GetPosition(fourthEllipse)
firstCoordinates.Content = mouse1.ToString()
secondCoordinates.Content = mouse2.ToString()
thirdCoordinates.Content = mouse3.ToString()
fourthCoordinates.Content = mouse4.ToString()
Dim position
As Point =
Mouse.GetPosition(DragEllipse)
ellipseTransform.X += position.X - (DragEllipse.Width / 2)
ellipseTransform.Y += position.Y - (DragEllipse.Height / 2)
End Sub
End
Class
End
Namespace
Output Window

Conclusion
Hope this article helps you to understand the
Input Events in WPF. Remaining part of this article you will see in my next
article.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
Manish Tewatia
Manish is Very much interested in Microsoft & LifeStyle Accessory Designand working with Microsoft technologies. His expert areas are ASP.NET, ADO.NET, C# .NET, WPF, WCF, Windows Phone 7, Android, SQL Server, HTML , XAML, etc…
He is doing MCA and he love to drive fast bikes.
inspired from Mr. Mahesh Chand.
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|