![]() ![]() This.DragDrop += new DragEventHandler(this.Form1_DragDrop) If ( e->Data->GetDataPresent( DataFormats::Bitmap ) || e->Data->GetDataPresent( DataFormats::FileDrop ) ) If the data is a file or a bitmap, display the copy cursor. Void Form1_DragEnter( Object^ /*sender*/, DragEventArgs^ e ) Force the form to be redrawn with the image. This->picture = dynamic_cast(e->Data->GetData( DataFormats::Bitmap )) Create an Image and assign it to the picture variable. If ( e->Data->GetDataPresent( DataFormats::Bitmap ) ) This->pictureLocation = this->PointToClient( Point(e->X,e->Y) ) Set the picture location equal to the drop point. This->picture = Image::FromFile( files ) Assign the first image to the picture variable. case the user has selected multiple files.Īrray^files = (array^)e->Data->GetData( DataFormats::FileDrop ) Assign the file names to a String* array, in If ( e->Data->GetDataPresent( DataFormats::FileDrop ) ) Void Form1_DragDrop( Object^ /*sender*/, DragEventArgs^ e ) If ( this->picture != nullptr & this->pictureLocation != Point::Empty )Į->Graphics->DrawImage( this->picture, this->pictureLocation ) If there is an image and it has a location, Virtual void OnPaint( PaintEventArgs^ e ) override This->DragEnter += gcnew DragEventHandler( this, &Form1::Form1_DragEnter ) This->DragDrop += gcnew DragEventHandler( this, &Form1::Form1_DragDrop ) add handlers for DragEnter and DragDrop. Because the DragEventArgs.X and DragEventArgs.Y values are screen coordinates, the example uses the PointToClient method to convert them to client coordinates. The DragDrop event-handling method displays the image on the form, if an Image can be created from the data. The DragEnter event-handling method determines the type of data being dragged into the form and provides the appropriate feedback. 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 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. Returns PointĪ Point that represents the converted Point, p, in client coordinates.
0 Comments
Leave a Reply. |