Share via


Shape.PointToClient Method

Computes the location of the specified screen point into client coordinates.

Namespace:  Microsoft.VisualBasic.PowerPacks
Assembly:  Microsoft.VisualBasic.PowerPacks.Vs (in Microsoft.VisualBasic.PowerPacks.Vs.dll)

Syntax

'Declaration
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

Parameters

Return Value

Type: System.Drawing.Point
A Point that represents the converted Point, p, in client coordinates.

Remarks

The PointToClient method can be used to convert a value such as a DragEventArgs that returns screen coordinates into the client coordinates of a form.

Examples

The following example demonstrates how to use the PointToClient method to move a RectangleShape when an image file is dropped onto it. The PointToClient method moves the RectangleShape relative to the client form. For example, if the drop location is 10 pixels down and 10 pixels to the right of the top-left corner of the rectangle, the rectangle will be moved to a location 10 pixels down and 10 pixels to the right of the top-left corner of the form.

This example requires that you have a RectangleShape control named RectangleShape1 on a form and that the AllowDrop property of the form is set to 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 Security

See Also

Reference

Shape Class

Microsoft.VisualBasic.PowerPacks Namespace

Other Resources

How to: Draw Lines with the LineShape Control (Visual Studio)

How to: Draw Shapes with the OvalShape and RectangleShape Controls (Visual Studio)

Introduction to the Line and Shape Controls (Visual Studio)