Shape.PointToClient 方法

将指定屏幕点的位置计算成工作区坐标。

命名空间:  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

参数

返回值

类型:System.Drawing.Point
一个 Point,它表示转换后的 Point、p(以工作区坐标表示)。

备注

PointToClient 方法还可用于将返回屏幕坐标到窗体的工作区坐标的值例如 DragEventArgs

示例

,在图像文件拖放到其上时,下面的示例演示如何使用 PointToClient 方法移动 RectangleShape 。PointToClient 方法移动 RectangleShape 相对客户端窗体。例如,因此,如果放置位置 10 像素放置和 10 像素在矩形右边的左上角,矩形将向下移到位置 10 像素、 10 像素的窗体右边的左上角。

此示例要求您具有名为在窗体的 RectangleShape1 的一个 RectangleShape 控件,并且该窗体的 AllowDrop 属性设置为 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)