2007-02-28, 16:13
  #1
Medlem
Jag ska ha ett fnster med ett antal bilder i. Och dessa bilder ska jag kunna flytta runt i fnstret genom drag n drop-metoden. Har ingen aning om var jag ska brja.. Vilka controllers behver jag anvnda? Behver ingen jttedetaljerad (tror jag) frklaring.. utan mest var jag ska brja och vilka controllers jag ska anvnda..
Citera
2007-02-28, 16:35
  #2
Medlem
RoscoTanners avatar
Citat:
Ursprungligen postat av zman
Jag ska ha ett fnster med ett antal bilder i. Och dessa bilder ska jag kunna flytta runt i fnstret genom drag n drop-metoden. Har ingen aning om var jag ska brja.. Vilka controllers behver jag anvnda? Behver ingen jttedetaljerad (tror jag) frklaring.. utan mest var jag ska brja och vilka controllers jag ska anvnda..

Det gr skert att gra p flera olika stt. Utan srskilt mycket tanke bakom s kanske man kan gra s hr:

*Skapa en klass (ImageClass) som du rver frn UserControl som du dessutom lgger en PictureBox p som fyller ut hela kontrollen.

*Fr varje ny bild som lses in lggs en ny sdan klassinstans till och bilden lses in i en Bitmap som sedan lggs till PictureBox:ens Image property.

*Skapa nn slags smart container-klass som hller reda p dina ImageClass-objekt och som skter hit-testing, drag-and-drop, redraw, och dylik funktionalitet.

*Anvnd klassen ovan i godtycklig Form eller Control fr att visa bilder.

Bara en ide som skert kan justeras till det bttre.
Citera
2007-02-28, 17:18
  #3
Medlem
Sane?s avatar
Jag skulle nog bara kra med en tom form och lyssna p dess mousedown/up/move och paint events.
Nr man trycker ner musknappen matchar du platsen mot nn positionslista fr bilderna och uppdaterar positionen nr musen rr sig.
I paint ritar du upp bilderna direkt p fomens bakgrund (funktion fr att rita bilder finns inbyggt i graphics som hnger med i painteventargs (ellervaddomheter)).

Inte srskilt objektorienterat men det r nog snabbaste sttet att lsa problemet
Citera
2007-02-28, 19:19
  #4
Medlem
Man kan bara ha en bild i en picturebox eller? r det sjlva pictureboxen man ska flytta runt d? Har satt DragDrop event p picturebox2.. men jag kanske r fel ute
Citera
2007-03-01, 00:18
  #5
Medlem
RoscoTanners avatar
Citat:
Ursprungligen postat av zman
Man kan bara ha en bild i en picturebox eller? r det sjlva 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 hjlpen som kan vara vrt att kolla in. Jag gjorde ett litet test utifrn hjlpen nedan med tv boxar som man kan flytta runt inuti en Form. Ett rent kludd men alltid r det ngot som kan hjlpa. Det r ven i C++/CLI men det borde inte vara svrt att inse vad som hnder.

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 gra r dock att skapa bilderna dynamisk antar jag nr du lser in nya bilder och d behver du frmodligen lgga in dem i nn slags container fr att hlla ordning p dem..
Citera
2007-03-01, 12:41
  #6
Medlem
Cisco9s avatar
Ta en titt p http://www.codeproject.com/csharp/dandtutorial.asp. The code project har oftast artiklar och tutorials om det mesta inom olika sprk.
Citera

Skapa ett konto eller logga in för att kommentera

Du måste vara medlem för att kunna kommentera

Skapa ett konto

Det är enkelt att registrera ett nytt konto

Bli medlem

Logga in

Har du redan ett konto? Logga in här

Logga in