Shape.PointToClient Method (Point)

 

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)

public Point PointToClient(
	Point position
)

Parameters

p

The screen coordinate Point to convert.

Return Value

Type: System.Drawing.Point

A Point that represents the converted Point, p, in client coordinates.

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.

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 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;
    }
}
Return to top
Show: