これを行うコードがあります。
「WPFでRectクラスを使用して2つの長方形を作成し、ラベルのサイズを変更するために、それらをラベルの左右の境界に配置して、マウスポインターがどちらかに配置されているかどうかを示します。ユーザーがそれらの領域をクリックしてドラッグした場合」
コードは次のようになります。
//the event handler
private void thelabel_MouseMove_1(object sender, MouseEventArgs e)
{
//if the mouse button is not pressed
if (Mouse.LeftButton == MouseButtonState.Released)
{
//locate the current mouse location
Point currentLocation = e.MouseDevice.GetPosition(this);
//the label's position
Point labelposition = thelabel.TransformToAncestor(thegrid).Transform(new Point(0, 0));
//create two Rects with 3 pixels as width and just as long as each one's corresponding border, and position them on each bottom and left borders of the label
var bottomHandle = new Rect(labelposition.X - thelabel.Width, labelposition.Y + thelabel.Height, thelabel.Width, 3);
var leftHandle = new Rect(labelposition.X - thelabel.Width, labelposition.Y, 3, thelabel.Height);
Point relativeLocation = this.TranslatePoint(currentLocation, this);
//if the left handle contains the mouse location i.e the mouse is on the left handle
if (leftHandle.Contains(relativeLocation))
{
this.Cursor = Cursors.SizeWE;
}
//if the bottom handle contains the mouse location
else if (bottomHandle.Contains(relativeLocation))
{
this.Cursor = Cursors.SizeNS;
}
else
{
//but this doesn't work when the mouse leaves the label and the two rects !
this.Cursor = Cursors.Arrow;
}
}
コードの最後の行
else
{
this.Cursor = Cursors.Arrow;
}
マウスがラベル上にない場合、マウスは呼び出されなくなり、カーソルは矢印に戻らないため、機能しませOnMouseMove
ん。
カーソルを矢印に戻すために、ラベルを含むだけでなく、OnMouseMove
にを追加してみました。ただし、これは、マウスがいずれかのにあるかどうかの判断に影響します。Grid
Canvas
Rect
Rect
したがって、問題は、「マウスが2つのRectを離れるかどうかを、ラベルのOnMouseMoveイベントの外部から、またはその他の方法でどのように判断できるか」です。