この記事は翻訳者によって翻訳されたものです。 記事の文章にポインターを重ねると、原文のテキストが表示されます。
訳文
原文
このトピックはまだ評価されていません - このトピックを評価する

Control.AllowDrop プロパティ

ユーザーがコントロールにドラッグしたデータを、そのコントロールが受け入れることができるかどうかを示す値を取得または設定します。

名前空間:  System.Windows.Forms
アセンブリ:  System.Windows.Forms (System.Windows.Forms.dll 内)
public virtual bool AllowDrop { get; set; }

プロパティ値

型: System.Boolean
コントロールでドラッグ アンド ドロップ操作を実行できる場合は true。それ以外の場合は false 既定値は、false です。

継承時の注意

派生クラスで AllowDrop プロパティをオーバーライドする場合は、基本クラスの AllowDrop プロパティを使用して、基本の実装を拡張します。 それ以外の場合は、すべての実装を提供する必要があります。 AllowDrop プロパティの get アクセサーと set アクセサーの両方をオーバーライドする必要はありません。必要に応じて 1 つのアクセサーだけをオーバーライドできます。

ユーザーがイメージまたはイメージ ファイルをフォームにドラッグできるようにし、ドロップされた場所にそのイメージを表示するコード例を次に示します。 OnPaint メソッドは、フォームが描画されるたびにイメージを再描画するためオーバーライドされます。されない場合、イメージは次回の再描画までそのままの状態で残ります。 DragEnter イベント処理メソッドでは、フォームにドラッグされているデータの種類を判別し、適切なフィードバックを提供します。 DragDrop イベント処理メソッドでは、Image をデータから作成できる場合に、フォーム上にイメージを表示します。 DragEventArgs.X および DragEventArgs.Y の値は画面座標であるため、この例では PointToClient メソッドを使用して値をクライアント座標に変換しています。


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

サポート対象: 4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile

サポート対象: 4、3.5 SP1

Windows 7, Windows Vista SP1 以降, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core はサポート対象外), Windows Server 2008 R2 (SP1 以降で Server Core をサポート), Windows Server 2003 SP2

.NET Framework では、各プラットフォームのすべてのバージョンはサポートしていません。 サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
この情報は役に立ちましたか。
(残り 1500 文字)
コミュニティ コンテンツ 追加
注釈 FAQ