共用方式為


Ink.NearestPoint 方法 (Point, Single%)

傳回最接近指定點的 Stroke 物件,並傳回 Stroke 物件上最接近指定點的點。

命名空間:  Microsoft.Ink
組件:  Microsoft.Ink (在 Microsoft.Ink.dll 中)

語法

'宣告
Public Function NearestPoint ( _
    point As Point, _
    <OutAttribute> ByRef pointOnStroke As Single _
) As Stroke
'用途
Dim instance As Ink
Dim point As Point
Dim pointOnStroke As Single
Dim returnValue As Stroke

returnValue = instance.NearestPoint(point, _
    pointOnStroke)
public Stroke NearestPoint(
    Point point,
    out float pointOnStroke
)
public:
Stroke^ NearestPoint(
    Point point, 
    [OutAttribute] float% pointOnStroke
)
public Stroke NearestPoint(
    Point point,
    /** @attribute OutAttribute */ /** @ref */float pointOnStroke
)
public function NearestPoint(
    point : Point, 
    pointOnStroke : float
) : Stroke

參數

傳回值

型別:Microsoft.Ink.Stroke
傳回 Stroke,其中包含最接近 Ink 中指定 point 的 Point (英文)。如果多個筆劃包含的 Point 與已知 Point (英文) 有相同的距離,這個結果的值就是任意值。

備註

pointOnStroke 參數是以浮點數定義的,因為 Stroke 物件的點可以落在兩個實體座標點之間。舉例來說,值 1.5 表示點落在第一個和第二個筆劃封包之間的中間處。使用這個值,可以藉由呼叫 Split 方法來分割 Stroke 物件,或可以將值向上捨入或向下捨入以索引 Stroke 物件中的封包

範例

在這個範例中,最接近筆墨控制項中心之 Stroke 物件的色彩會變更為紅色。此外會建立新的 Stroke 物件,從所探索到最接近筆墨控制項中心的 Stroke 物件延伸到筆墨控制項中心。

Dim inkControl As Control = mInkOverlay.AttachedControl
' get the center of the ink control
Dim centerPt As Point = New Point(inkControl.Width / 2, inkControl.Height / 2)
Using g As Graphics = inkControl.CreateGraphics()
    ' convert center point to ink space coordinates
    mInkOverlay.Renderer.PixelToInkSpace(g, centerPt)
End Using
' get the nearest stroke
Dim pointOnStroke As Single
Dim nStroke As Stroke = mInkOverlay.Ink.NearestPoint(centerPt, pointOnStroke)
' nStroke will be null if there aren't any strokes
If Not IsNothing(nStroke) Then
    ' change the color of the nearest stroke to red
    nStroke.DrawingAttributes.Color = Color.Red
    Dim ptIdx As Integer = CType(Math.Round(pointOnStroke, MidpointRounding.ToEven), Integer)
    ' create points from the center to the nearest point on the stroke
    Dim connectPts() As Point = _
            { _
                centerPt, _
                nStroke.GetPoint(ptIdx) _
            }
    ' create the stroke
    mInkOverlay.Ink.CreateStroke(connectPts)
    inkControl.Invalidate()
End If
Control inkControl = mInkOverlay.AttachedControl;
// get the center of the ink control
Point centerPt = new Point(inkControl.Width / 2, inkControl.Height / 2);
using (Graphics g = inkControl.CreateGraphics())
{
    // convert center point to ink space coordinates
    mInkOverlay.Renderer.PixelToInkSpace(g, ref centerPt);
}

// get the nearest stroke
float pointOnStroke;
Stroke nStroke = mInkOverlay.Ink.NearestPoint(centerPt, out pointOnStroke);

// nStroke will be null if there aren't any strokes
if (nStroke != null)
{
    // change the color of the nearest stroke to red
    nStroke.DrawingAttributes.Color = Color.Red;
    int ptIdx = (int)Math.Round(pointOnStroke, MidpointRounding.ToEven);
    // create points from the center to the nearest point on the stroke
    Point[] connectPts = new Point[2] 
    {
        centerPt, 
        nStroke.GetPoint(ptIdx)
    };
    // create the stroke
    mInkOverlay.Ink.CreateStroke(connectPts);
    inkControl.Invalidate();
}

平台

Windows Vista

.NET Framework 和 .NET Compact Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱 .NET Framework 系統需求

版本資訊

.NET Framework

支援版本:3.0

請參閱

參考

Ink 類別

Ink 成員

NearestPoint 多載

Microsoft.Ink 命名空間

Stroke

Stroke.Split