ITransformProvider.Move(Double, Double) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
移動控制器。
public:
void Move(double x, double y);
public void Move(double x, double y);
abstract member Move : double * double -> unit
Public Sub Move (x As Double, y As Double)
參數
- x
- Double
控制器左側的絕對螢幕座標。
- y
- Double
控制桿頂部的絕對螢幕座標。
例外狀況
如果該 CanMove 性質是假的。
範例
以下範例展示了一種可移動自訂控制項的可能實作。
/// <summary>
/// Moves the provider to the specified position.
/// </summary>
/// <param name="x">The specified X screen coordinate.</param>
/// <param name="y">The specified Y screen coordinate</param>
void ITransformProvider.Move(double x, double y)
{
int leftInt = (int)x;
int topInt = (int)y;
if (!((ITransformProvider)this).CanMove)
throw new InvalidOperationException(
"Operation cannot be performed.");
// Move should never be allowed to place a control outside the
// bounds of its container; the control should always be accessible
// using the keyboard or mouse.
// Use the bounds of the parent window to limit the placement
// of the custom control.
int maxLeft = 10;
int maxTop = 10;
int maxRight =
this.customControl.formWidth - this.customControl.Width - 10;
int maxBottom =
this.customControl.formHeight - this.customControl.Height - 10;
if (leftInt < maxLeft)
leftInt = 0;
if (topInt < maxTop)
topInt = 0;
if (leftInt > maxRight)
leftInt = maxRight;
if (topInt > maxBottom)
topInt = maxBottom;
// Invoke control method on separate thread to avoid clashing with UI.
// Use anonymous method for simplicity.
this.customControl.Invoke(new MethodInvoker(delegate ()
{
this.customControl.Left = leftInt;
this.customControl.Top = topInt;
}));
}
''' <summary>
''' Moves the provider to the specified position.
''' </summary>
''' <param name="x">The specified X screen coordinate.</param>
''' <param name="y">The specified Y screen coordinate</param>
Private Sub Move(ByVal x As Double, ByVal y As Double) Implements ITransformProvider.Move
Dim leftInt As Integer = CInt(x)
Dim topInt As Integer = CInt(y)
If Not(CType(Me, ITransformProvider)).CanMove Then
Throw New InvalidOperationException("Operation cannot be performed.")
End If
' Move should never be allowed to place a control outside the
' bounds of its container; the control should always be accessible
' using the keyboard or mouse.
' Use the bounds of the parent window to limit the placement
' of the custom control.
Dim maxLeft As Integer = 10
Dim maxTop As Integer = 10
Dim maxRight As Integer = Me.customControl.formWidth - Me.customControl.Width - 10
Dim maxBottom As Integer = Me.customControl.formHeight - Me.customControl.Height - 10
If leftInt < maxLeft Then
leftInt = 0
End If
If topInt < maxTop Then
topInt = 0
End If
If leftInt > maxRight Then
leftInt = maxRight
End If
If topInt > maxBottom Then
topInt = maxBottom
End If
' Invoke control method on separate thread to avoid clashing with UI.
' Use anonymous method for simplicity.
Me.customControl.Invoke(New MethodInvoker(Sub()
Me.customControl.Left = leftInt
Me.customControl.Top = topInt
End Sub))
End Sub
備註
物件無法被移動、調整大小或旋轉,使其產生的螢幕位置完全超出容器座標,且鍵盤或滑鼠無法存取。 例如,當頂層視窗完全移出畫面外,或子物件移出容器視窗的邊界時。 在這些情況下,物件會盡量靠近請求的螢幕座標,而頂部或左側的座標會被覆寫,以使其位於容器的邊界內。