MonoMac/カスタムコントロールでマウスボタンが押された座標を取得する
目次
概要編集
マウスのボタンが押されるとNSViewクラスのMouseDownメソッドやMouseUpメソッドが呼ばれる。 これらのメソッドに引数として渡されるtheEvent変数に含まれる座標はウインドウ領域の座標(ウインドウの角を原点とした座標)である。 常識的に考えてウインドウ領域の座標が欲しい場面などほとんどないわけで、これでは非常に扱いづらい。
実装編集
ウインドウ領域の座標をビュー領域の座標に変換してやるにはNSViewクラスのConvertPointFromViewメソッドを使用する。なんとも面倒くさい。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Drawing;
using MonoMac.Foundation;
using MonoMac.AppKit;
namespace ContentEditor
{
public partial class EditorView : MonoMac.AppKit.NSView
{
// 〜〜〜省略〜〜〜
public override void MouseDown(NSEvent theEvent)
{
// ウインドウ領域の座標をビュー領域の座標に変換する。
// aViewはnull指定で自身になる
var locationInView = this.ConvertPointFromView( aPoint:theEvent.LocationInWindow, aView:null );
Console.WriteLine(locationInView);
base.MouseUp(theEvent);
}
}
}