Export (0) Print
Expand All
Expand Minimize

Control.PointToClient Method

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

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Function PointToClient ( _
	p As Point _
) As Point
'Usage
Dim instance As Control 
Dim p As Point 
Dim returnValue As Point 

returnValue = instance.PointToClient(p)

Parameters

p
Type: System.Drawing.Point

The screen coordinate Point to convert.

Return Value

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

The following code example enables the user to drag an image or image file onto the form, and have it be displayed at the point on it is dropped. The OnPaint method is overridden to repaint the image each time the form is painted; otherwise the image would only persist until the next repainting. The DragEnter event-handling method determines the type of data being dragged into the form and provides the appropriate feedback. The DragDrop event-handling method displays the image on the form, if an Image can be created from the data. Because the DragEventArgs.X and DragEventArgs.Y values are screen coordinates, the example uses the PointToClient method to convert them to client coordinates.

Private picture As Image
Private pictureLocation As Point

Public Sub New()
   ' Enable drag-and-drop operations. 
   Me.AllowDrop = True 
End Sub 

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
   MyBase.OnPaint(e)

   ' If there is an image and it has a location,  
   ' paint it when the Form is repainted. 
   If (Me.picture IsNot Nothing) And _
     Not (Me.pictureLocation.Equals(Point.Empty)) Then
      e.Graphics.DrawImage(Me.picture, Me.pictureLocation)
   End If 
End Sub 

Private Sub Form1_DragDrop(ByVal sender As Object, _
  ByVal e As DragEventArgs) Handles MyBase.DragDrop
   ' Handle FileDrop 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 'picture' variable. 
         Me.picture = Image.FromFile(files(0))
         ' Set the picture location equal to the drop point. 
         Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
      Catch ex As Exception
         MessageBox.Show(ex.Message)
         Return 
      End Try 
   End If 

   ' Handle Bitmap data. 
   If e.Data.GetDataPresent(DataFormats.Bitmap) Then 
      Try 
         ' Create an Image and assign it to the picture variable. 
         Me.picture = CType(e.Data.GetData(DataFormats.Bitmap), Image)
         ' Set the picture location equal to the drop point. 
         Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
      Catch ex As Exception
         MessageBox.Show(ex.Message)
         Return 
      End Try 
   End If 

   ' Force the form to be redrawn with the image. 
   Me.Invalidate()
End Sub 

Private Sub Form1_DragEnter(ByVal sender As Object, _
  ByVal e As DragEventArgs) Handles MyBase.DragEnter
   ' If the data is a file or a bitmap, display the copy cursor. 
   If e.Data.GetDataPresent(DataFormats.Bitmap) _
      Or e.Data.GetDataPresent(DataFormats.FileDrop) Then
      e.Effect = DragDropEffects.Copy
   Else
      e.Effect = DragDropEffects.None
   End If 
End Sub

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft