Control.PointToClient(Point) 메서드

정의

특정 화면 지점의 위치를 클라이언트 좌표로 계산합니다.

C#
public System.Drawing.Point PointToClient (System.Drawing.Point p);

매개 변수

p
Point

변환할 화면 좌표 Point입니다.

반환

변환된 PointPoint를 클라이언트 좌표로 나타내는 p입니다.

예제

다음 코드 예제에서는 사용자가 이미지 또는 이미지 파일을 폼으로 끌어 놓을 때 표시되도록 합니다. 메서드는 OnPaint 양식이 그려질 때마다 이미지를 다시 칠하도록 재정의됩니다. 그렇지 않으면 이미지가 다음 다시 칠할 때까지만 유지됩니다. 이벤트 처리 메서드는 DragEnter 폼으로 끌 데이터의 형식을 결정하고 적절한 피드백을 제공합니다. DragDrop 데이터에서 를 만들 수 있는 경우 Image 이벤트 처리 메서드는 양식에 이미지를 표시합니다. DragEventArgs.XDragEventArgs.Y 값은 화면 좌표이므로 예제에서는 메서드를 PointToClient 사용하여 클라이언트 좌표로 변환합니다.

C#
private Image picture;
private Point pictureLocation;

public Form1()
{
   // Enable drag-and-drop operations and 
   // add handlers for DragEnter and DragDrop.
   this.AllowDrop = true;
   this.DragDrop += new DragEventHandler(this.Form1_DragDrop);
   this.DragEnter += new DragEventHandler(this.Form1_DragEnter);
}

protected override void OnPaint(PaintEventArgs e)
{
   // If there is an image and it has a location, 
   // paint it when the Form is repainted.
   base.OnPaint(e);
   if(this.picture != null && this.pictureLocation != Point.Empty)
   {
      e.Graphics.DrawImage(this.picture, this.pictureLocation);
   }
}

private void Form1_DragDrop(object sender, DragEventArgs e)
{
   // Handle FileDrop 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 picture variable.
         this.picture = Image.FromFile(files[0]);
         // Set the picture location equal to the drop point.
         this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.Message);
         return;
      }
   }

   // Handle Bitmap data.
   if(e.Data.GetDataPresent(DataFormats.Bitmap) )
   {
      try
      {
         // Create an Image and assign it to the picture variable.
         this.picture = (Image)e.Data.GetData(DataFormats.Bitmap);
         // Set the picture location equal to the drop point.
         this.pictureLocation = this.PointToClient(new Point(e.X, e.Y) );
      }
      catch(Exception ex)
      {
         MessageBox.Show(ex.Message);
         return;
      }
   }
   // Force the form to be redrawn with the image.
   this.Invalidate();
}

private void Form1_DragEnter(object sender, DragEventArgs e)
{
   // If the data is a file or a bitmap, display the copy cursor.
   if (e.Data.GetDataPresent(DataFormats.Bitmap) || 
      e.Data.GetDataPresent(DataFormats.FileDrop) ) 
   {
      e.Effect = DragDropEffects.Copy;
   }
   else
   {
      e.Effect = DragDropEffects.None;
   }
}

적용 대상

제품 버전
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

추가 정보