1 Device Independent coordinate
In Win32 API, we get mouse position as (x,y) in device coordinate, which means x, y are with unit of "pixels".
In WPF, it uses Device Independent coordinate, but what's the unit? mm? inch?
The answer is NO. It actually uses a weird unit, one-96th inch!
WPF assumes your screen's PPI is 96, then it calculates the coordinate by:
logical coordinate = physical coordinate (inch) * 96
e.g.
The device point of (10 inch, 10 inch) has the logical cooridnate of (960, 960).
Below are some points about WPF's logical coordinate.
- It's Device Independent. The same coordinate represents the same position physically.
- Its unit is not an inch, it's (inch/96)
- Since most Video card nowadays has PPI > 96, it's smaller than device(pixel) coordinates.
2 Example
This example prints the mouse's current position relative to the window in Logical Coordinate.
using System;
using System.Windows;
using System.Windows;
namespace LogicalCooridnateTest
{
class MainWindow : Window
{
[STAThread]
public static void Main()
{
Application app = new Application();
app.Run(new MainWindow());
}
public MainWindow()
{
this.MouseMove += MainWindow_MouseMove;
}
private void MainWindow_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
this.Content = e.GetPosition(this);
}
}
}
No comments:
Post a Comment