Citat:
Ursprungligen postat av zman
Man kan bara ha en bild i en picturebox eller? Är det själva pictureboxen man ska flytta runt då? Har satt DragDrop event på picturebox2.. men jag kanske är fel ute
En bild per box. Det finns ett exempel under DoDragDrop i hjälpen som kan vara värt att kolla in. Jag gjorde ett litet test utifrån hjälpen nedan med två boxar som man kan flytta runt inuti en Form. Ett rent kludd men alltid är det något som kan hjälpa. Det är även i C++/CLI men det borde inte vara svårt att inse vad som händer.
Kod:
#pragma once
namespace TestaDrop
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
this->pictureBox1->Image = gcnew Bitmap("c:\\test.bmp");
this->pictureBox2->Image = gcnew Bitmap("c:\\test.bmp");
this->currentBox = nullptr;
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components) {
delete components;
}
}
private:
System::Windows::Forms::PictureBox^ pictureBox1;
System::Windows::Forms::PictureBox^ pictureBox2;
System::Windows::Forms::PictureBox^ currentBox;
Rectangle dragBoxFromMouseDown;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox2))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Location = System::Drawing::Point(420, 95);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(200, 163);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
this->pictureBox1->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::PictureBox_MouseDown);
this->pictureBox1->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::PictureBox_MouseMove);
this->pictureBox1->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::PictureBox_MouseUp);
//
// pictureBox2
//
this->pictureBox2->Location = System::Drawing::Point(92, 116);
this->pictureBox2->Name = L"pictureBox2";
this->pictureBox2->Size = System::Drawing::Size(178, 121);
this->pictureBox2->TabIndex = 1;
this->pictureBox2->TabStop = false;
this->pictureBox2->MouseDown += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::PictureBox_MouseDown);
this->pictureBox2->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::PictureBox_MouseMove);
this->pictureBox2->MouseUp += gcnew System::Windows::Forms::MouseEventHandler(this, &Form1::PictureBox_MouseUp);
//
// Form1
//
this->AllowDrop = true;
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(657, 427);
this->Controls->Add(this->pictureBox2);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->DragDrop += gcnew System::Windows::Forms::DragEventHandler(this, &Form1::Form1_DragDrop);
this->DragOver += gcnew System::Windows::Forms::DragEventHandler(this, &Form1::Form1_DragOver);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox2))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
System::Void PictureBox_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
{
try {
this->currentBox = safe_cast<PictureBox^>(sender);
}
catch (...) { return; }
// Remember the point where the mouse down occurred. The DragSize indicates
// the size that the mouse can move before a drag event should be started.
System::Drawing::Size dragSize = SystemInformation::DragSize;
// Create a rectangle using the DragSize, with the mouse position being
// at the center of the rectangle.
dragBoxFromMouseDown = Rectangle(Point(e->X - (dragSize.Width / 2),e->Y - (dragSize.Height / 2)),dragSize);
}
System::Void PictureBox_MouseMove(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
{
if ( (e->Button & System::Windows::Forms::MouseButtons::Left) == System::Windows::Forms::MouseButtons::Left ) {
// If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMouseDown != Rectangle::Empty && !dragBoxFromMouseDown.Contains( e->X, e->Y ) )
{
// Proceed with the drag-and-drop, passing in the list item.
DragDropEffects dropEffect = this->DoDragDrop( this->pictureBox1, static_cast<DragDropEffects>(DragDropEffects::All | DragDropEffects::Link) );
}
}
}
System::Void PictureBox_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e)
{
// Reset the drag rectangle when the mouse button is raised.
dragBoxFromMouseDown = Rectangle::Empty;
this->currentBox = nullptr;
}
System::Void Form1_DragDrop(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e)
{
if (this->currentBox) {
this->currentBox->Location = Point(e->X - this->Location.X, e->Y - this->Location.Y);
}
}
System::Void Form1_DragOver(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e)
{
e->Effect = DragDropEffects::Move;
}
};
}
Det som du vill göra är dock att skapa bilderna dynamisk antar jag när du läser in nya bilder och då behöver du förmodligen lägga in dem i nån slags container för att hålla ordning på dem..