Click here to Skip to main content
12,633,764 members (32,230 online)
Click here to Skip to main content
Add your own
alternative version

Stats

6.5K views
Posted

PictureBox Image Exchange by Drag and Drop (just using mouse event)

, 30 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Many drag and drop effects can be simply done by mouse event.

Introduction

This tip discusses how to exchange the images of two PictureBoxes by dragging one on the other.

Using the Code

Open Visual Studio 2008 Windows Form with two PictureBoxes created.

???2

Image^ im;
Point PicLoc;
int ox,oy;
bool md;
private: System::Void pictureBox1_MouseDown
	(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
                 PicLoc=this->pictureBox1->Location;
                 im=pictureBox2->Image;
                 this->pictureBox1->BringToFront();
                 ox=e->X;
                 oy=e->Y;
                 md=true;
             }
private: System::Void pictureBox1_MouseUp
	(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
                 md=false;
                 int p1=e->X+pictureBox1->Location.X;
                 int p2=e->Y+pictureBox1->Location.Y;
                 if(pictureBox2->Location.X<p1 && 
			p1<pictureBox2->Location.X+pictureBox2->Size.Width){
                     if(pictureBox2->Location.Y<p2 && 
			p2<pictureBox2->Location.Y+pictureBox2->Size.Height){
                         this->pictureBox2->Image=this->pictureBox1->Image;
                         this->pictureBox1->Image=im;
                     }
                 }
                 this->pictureBox1->Location=PicLoc;
             }
    private: System::Void pictureBox1_MouseMove
	(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e) {
                 if(md){
                     this->pictureBox1->Left +=e->X-ox;
                     this->pictureBox1->Top +=e->Y-oy;
                 }
             }

Effect

Drag PictureBox1 on to PictureBox2:

???

The images have been exchanged. PictureBox1 goes back to the initial position when mouse is up.

???3

History

  • Version 1: 28/4/2012

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Martin Mok
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161208.2 | Last Updated 30 Apr 2012
Article Copyright 2012 by Martin Mok
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid