「Xamarin.Macでウインドウ座標をビュー座標に変換する」の版間の差分

提供: MonoBook
ナビゲーションに移動 検索に移動
6行目: 6行目:
  
 
<source lang="csharp">
 
<source lang="csharp">
public override void MouseDown(NSEvent theEvent)
+
public class CustomView : NSView
 
{
 
{
     var wLocation = theEvent.LocationInWindow;
+
     public override void MouseDown(NSEvent theEvent)
    var vLocation = ConvertPointFromView(wLocation, null);
+
    {
 +
        // これウインドウ座標
 +
        var wLocation = theEvent.LocationInWindow;
 +
       
 +
        // ビュー座標に変換
 +
        // 似たような名前のメソッドがいっぱいあるので注意。
 +
        var vLocation = this.ConvertPointFromView(wLocation, aView:null);
 +
    }
 
}
 
}
 
</source>
 
</source>

2017年6月13日 (火) 02:29時点における版

Xamarin.MacにおけるMouseDownなどのイベント引数で飛んでくるマウスの座標は「ウインドウ座標」となっている。

しかし、ほとんどの場合、マウスクリックされた場所の「ビュー座標」が欲しい。 とくにNSViewを継承したCustomViewを作っている場合はウインドウ座標とか外の世界の話であってそんなものを貰っても困る。 そういうときはウインドウ座標をビュー座標に変換する。

public class CustomView : NSView
{
    public override void MouseDown(NSEvent theEvent)
    {
        // これウインドウ座標
        var wLocation = theEvent.LocationInWindow;
        
        // ビュー座標に変換
        // 似たような名前のメソッドがいっぱいあるので注意。
        var vLocation = this.ConvertPointFromView(wLocation, aView:null);
    }
}

関連項目

参考文献