.NET
  Home arrow .NET arrow Mouse Input and the WPF
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Mouse Input and the WPF
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2008-07-02

    Table of Contents:
  • Mouse Input and the WPF
  • Mouse Input
  • Mouse Input and Hit Testing
  • Mouse State

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Mouse Input and the WPF


    (Page 1 of 4 )

    This second part of a five-part article series on the input handling mechanisms available with the Windows Presentation Foundation focuses on the mouse. It is excerpted from Programming WPF, Second Edition, written by Chris Sells and Ian Griffiths (O'Reilly, 2007; ISBN: 0596510373). Copyright © 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

    Attached Events

    It is possible to define an attached event. This is the routed-event equivalent of an attached property: an event defined by a different class than the one from which the event will be raised. This keeps the input system open to extension. If a new kind of input device is invented, it could define new events as attached events, enabling them to be raised from any UI element.

    In fact, the WPF input system already works this way. The mouse, stylus, and keyboard events examined in this chapter are just wrappers for underlying attached events defined by theMouse,Keyboard, andStylus classes in theSystem.Windows.Inputnamespace. This means we could change theGrid element in Example 4-1 to use the attached events defined by theMouseclass, as shown in Example 4-6.

    Example 4-6. Attached event handling

    <Grid Mouse.PreviewMouseDown="PreviewMouseDownGrid"
          Mouse.MouseDown="MouseDownGrid">

    This would have no effect on the behavior, because the names Example 4-1 used for these events are aliases for the attached events used in this example.

    Handling attached events from code looks a little different. Normal CLR events don’t support this notion of attached events, so we can’t use the ordinary C# event syntax like we did in Example 4-4. Instead, we have to call the AddHandlermethod, passing in theRoutedEventobject representing the attached event (see Example 4-7).

    Example 4-7. Explicit attached event handling

    myEllipse.AddHandler(Mouse.PreviewMouseDownEvent,
       
    new MouseButtonEventHandler(PreviewMouseDownEllipse)); myEllipse.AddHandler(Mouse.MouseDownEvent,
       
    new MouseButtonEventHandler(MouseDownEllipse));

    Alternatively, we can use the helper functions provided by theMouseclass. Example 4-8 uses this to perform exactly the same job as the preceding two examples.

    Example 4-8. Attached event handling with helper function

    Mouse.AddPreviewMouseDownHandler(myEllipse, PreviewMouseDownEllipse); Mouse.AddMouseDownHandler(myEllipse, MouseDownEllipse);

    Example 4-8 is more compact than Example 4-7 because we were able to omit the explicit construction of the delegate, relying instead on C# delegate type inference. Example 4-7 cannot do this because AddHandlercan attach a handler for any kind of event, so in its function signature the second parameter is of the baseDelegate type. By convention, classes that define attached events usually provide corresponding helper methods like these to let you use this slightly neater style of code.

    More .NET Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming WPF, Second Edition,"...
     

    Buy this book now. This article is excerpted from Programming WPF, Second Edition, written by Chris Sells and Ian Griffiths (O'Reilly, 2007; ISBN: 0596510373). Check it out today at your favorite bookstore. Buy this book now.

    .NET ARTICLES

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT