Prev | Current Page 336 | Next

Matthew MacDonald

"Pro WPF with VB 2008: Windows Presentation Foundation with .NET 3.5"

As you move the mouse
away, the MouseLeave event will fire first for the button and then for the StackPanel.
You can also react to two events that fire whenever the mouse moves: PreviewMouseMove
(a tunneling event) and MouseMove (a bubbling event). All of these events provide your code
with the same information: a MouseEventArgs object. The MouseEventArgs object includes
properties that tell you the state that the mouse buttons were in when the event fired, and a
GetPosition() method that tells you the coordinates of the mouse in relation to an element of
your choosing. Here??™s an example that displays the position of the mouse pointer in deviceindependent
pixels relative to the form:
Private Sub MouseMoved(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim pt As Point = e.GetPosition(Me)
lblInfo.Text = _
String.Format("You are at ({0},{1}) in window coordinates", pt.X, pt.Y)
End Sub
In this case, the coordinates are measured from the top-left corner of the client area (just
below the title bar). Figure 6-7 shows this code in action.
Figure 6-7.Watching the mouse
You??™ll notice that the mouse coordinates in this example are not whole numbers. That??™s
because this screen capture was taken on a system running at 120 dpi, not the standard 96 dpi.
As explained in Chapter 1, WPF automatically scales up its units to compensate, using more
physical pixels.


Pages:
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348