共用方式為


Shape.PointToClient 方法

將指定的螢幕點的位置計算為工作區座標 (Client Coordinate)。

命名空間:  Microsoft.VisualBasic.PowerPacks
組件:  Microsoft.VisualBasic.PowerPacks.Vs (在 Microsoft.VisualBasic.PowerPacks.Vs.dll 中)

語法

'宣告
Public Function PointToClient ( _
    position As Point _
) As Point
public Point PointToClient(
    Point position
)
public:
Point PointToClient(
    Point position
)
member PointToClient : 
        position:Point -> Point
public function PointToClient(
    position : Point
) : Point

參數

  • position
    類型:Point

傳回值

類型:Point
Point,以工作區座標表示的轉換後 Point (p)。

備註

PointToClient方法可以用來將值轉換為DragEventArgs傳回螢幕座標插入用戶端表單的座標。

範例

下列範例示範如何使用PointToClient方法來移動RectangleShape當映像檔放到它。 PointToClient方法移RectangleShape相對於用戶端表單。 比方說,如果置放位置是 10 個像素和右邊的矩形左上角的 10 個像素,矩形將移往下的位置 10 個像素和右邊的表單的左上角的 10 個像素。

此範例中您需要RectangleShape控制項上的表單,名為 RectangleShape1AllowDrop表單的屬性設定為true。

Private Sub Form1_DragDrop(
    ByVal sender As Object, 
    ByVal e As System.Windows.Forms.DragEventArgs
  ) Handles Me.DragDrop

    ' Determine whether the drop is within the rectangle. 
    If RectangleShape1.HitTest(e.X, e.Y) = True Then 
        ' Handle file data. 
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then 
            ' Assign the file names to a string array, in  
            ' case the user has selected multiple files. 
            Dim files As String() = 
              CType(e.Data.GetData(DataFormats.FileDrop), String())
            Try 
                ' Assign the first image to the BackGroundImage 
                ' property.
                RectangleShape1.BackgroundImage = 
                  Image.FromFile(files(0))
                ' Set the rectangle location relative to the form.
                RectangleShape1.Location = 
                  RectangleShape1.PointToClient(New Point(e.X, e.Y))
            Catch ex As Exception
                MessageBox.Show(ex.Message)
                Return 
            End Try 
        End If 
    End If 
End Sub 
Private Sub Form1_DragEnter(
    ByVal sender As Object, 
    ByVal e As DragEventArgs
  ) Handles MyBase.DragEnter

    ' If the data is a file, display the copy cursor. 
    If e.Data.GetDataPresent(DataFormats.FileDrop) Then
        e.Effect = DragDropEffects.Copy
    Else
        e.Effect = DragDropEffects.None
    End If 
End Sub
private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
    // Determine whether the drop is within the rectangle. 
    if (rectangleShape1.HitTest(e.X, e.Y)==true)
        // Handle file data.
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
            // Assign the file names to a string array, in  
            // case the user has selected multiple files.
        {
            string[] files = (string[]) e.Data.GetData(DataFormats.FileDrop);
            try
            {
                // Assign the first image to the BackGroundImage 
                // property.
                rectangleShape1.BackgroundImage = Image.FromFile(files[0]);
                // Set the rectangle location relative to the form.
                rectangleShape1.Location = 
                    rectangleShape1.PointToClient(new Point(e.X, e.Y));
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
    }
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
    // If the data is a file, display the copy cursor. 
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        e.Effect = DragDropEffects.Copy;
    }
    else
    {
        e.Effect = DragDropEffects.None;
    }
}

.NET Framework 安全性

請參閱

參考

Shape 類別

Microsoft.VisualBasic.PowerPacks 命名空間

其他資源

如何:使用 LineShape 控制項繪製線條 (Visual Studio)

如何:使用 OvalShape 和 RectangleShape 控制項繪製圖案 (Visual Studio)

Line 和 Shape 控制項簡介 (Visual Studio)