.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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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 / 4
    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,"...
       · This article is a load of bull. Background white and opacity 0 makes the control...
     

    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

    - Introducing .NET
    - Building Business Objects for an Application
    - Methods for an Application`s Business Logic ...
    - Properties of an Application`s Business Logi...
    - Classes and Properties in an Application`s B...
    - Organizing Code for the Business Logic Layer...
    - Building the Business Logic Layer
    - Completing a Business Layer with Windows Wor...
    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 11 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek