Vinnaren i pepparkakshustävlingen!
2007-02-28, 16:13
  #1
Medlem
Jag ska ha ett fönster med ett antal bilder i. Och dessa bilder ska jag kunna flytta runt i fönstret genom drag n drop-metoden. Har ingen aning om var jag ska börja.. Vilka controllers behöver jag använda? Behöver ingen jättedetaljerad (tror jag) förklaring.. utan mest var jag ska börja och vilka controllers jag ska använda..
Citera
2007-02-28, 16:35
  #2
Medlem
RoscoTanners avatar
Citat:
Ursprungligen postat av zman
Jag ska ha ett fönster med ett antal bilder i. Och dessa bilder ska jag kunna flytta runt i fönstret genom drag n drop-metoden. Har ingen aning om var jag ska börja.. Vilka controllers behöver jag använda? Behöver ingen jättedetaljerad (tror jag) förklaring.. utan mest var jag ska börja och vilka controllers jag ska använda..

Det går säkert att göra på flera olika sätt. Utan särskilt mycket tanke bakom så kanske man kan göra så här:

*Skapa en klass (ImageClass) som du ärver från UserControl som du dessutom lägger en PictureBox på som fyller ut hela kontrollen.

*För varje ny bild som läses in läggs en ny sådan klassinstans till och bilden läses in i en Bitmap som sedan läggs till PictureBox:ens Image property.

*Skapa nån slags smart container-klass som håller reda på dina ImageClass-objekt och som sköter hit-testing, drag-and-drop, redraw, och dylik funktionalitet.

*Använd klassen ovan i godtycklig Form eller Control för att visa bilder.

Bara en ide som säkert kan justeras till det bättre.
Citera
2007-02-28, 17:18
  #3
Medlem
Sane?s avatar
Jag skulle nog bara köra med en tom form och lyssna på dess mousedown/up/move och paint events.
När man trycker ner musknappen matchar du platsen mot nån positionslista för bilderna och uppdaterar positionen när musen rör sig.
I paint ritar du upp bilderna direkt på fomens bakgrund (funktion för att rita bilder finns inbyggt i graphics som hänger med i painteventargs (ellervaddomheter)).

Inte särskilt objektorienterat men det är nog snabbaste sättet att lösa problemet
Citera
2007-02-28, 19:19
  #4
Medlem
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
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 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..
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 språk.
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