Advertisement
Lauda

Drag & drop clusterfuck

May 13th, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using HCI_Projekat.klase;
  10. using HCI_Projekat.klase.handlers;
  11.  
  12. namespace HCI_Projekat.forme
  13. {
  14.     public partial class frmMapa : Form
  15.     {
  16.         private TreeNode _draggedIndexItem;
  17.         private Point _screenOffset;
  18.         private Rectangle _mouseDownSelectionWindow;
  19.         private int x, y;
  20.         private Image selImg;
  21.         Point position;
  22.  
  23.         public frmMapa()
  24.         {
  25.             InitializeComponent();
  26.             populateTrewView();
  27.             tvIcon.ExpandAll();
  28.  
  29.             pMap.AllowDrop = true;
  30.  
  31.             _mouseDownSelectionWindow = Rectangle.Empty;
  32.         }
  33.  
  34.         private void frmMapa_Load(object sender, EventArgs e)
  35.         {
  36.             List<IkoniceKoord> icon = Ikonice.getInstance().getIkonice();
  37.  
  38.             foreach (IkoniceKoord ikItr in icon)
  39.             {
  40.                 PictureBox pb = new PictureBox();
  41.                 CustomToolTip ctt = new CustomToolTip();
  42.  
  43.                 pb.Location = new Point(ikItr.x, ikItr.y);
  44.                 pb.Image = ikItr.icon;
  45.                 pb.BackColor = Color.Transparent;
  46.                 pb.Tag = pb.Image;
  47.                 pb.Size = new Size(ikItr.icon.Width, ikItr.icon.Height);
  48.                 ctt.SetToolTip(pb, ikItr.tTip);
  49.           //    MessageBox.Show(ikItr.x + " | " + ikItr.y + " | " + ikItr.icon.ToString());
  50.                 pMap.Controls.Add(pb);
  51.             }
  52.         }
  53.  
  54.         private void populateTrewView()
  55.         {
  56.             List<ZivotinjskeVrste> zv = Zivotinje.getInstance().getZivotinjskeVrste();
  57.             ImageList imgList = new ImageList();
  58.             TreeNode root = tvIcon.Nodes.Add("Životinje");
  59.             TreeNode items = root;
  60.  
  61.             if (Zivotinje.getInstance().getZivotinjskeVrste() != null)
  62.             {
  63.             /*    foreach (ZivotinjskeVrste zvItr in zv)
  64.                 {
  65.                     imgList.Images.Add(zvItr.Ikona);
  66.                     items = root.Nodes.Add(zvItr.Ime);
  67.                     items.ImageIndex = 0;
  68.                     items.SelectedImageIndex = 0;
  69.                 } */
  70.  
  71.                 for (int i = 0; i < Zivotinje.getInstance().getZivotinjskeVrste().Count; i++)
  72.                 {
  73.                     items = root.Nodes.Add(Zivotinje.getInstance().getZivotinjskeVrste()[i].Ime);
  74.                     imgList.Images.Add(Zivotinje.getInstance().getZivotinjskeVrste()[i].Ikona);
  75.                     items.ImageIndex = i;
  76.                     items.SelectedImageIndex = i;
  77.                     items.Tag = Zivotinje.getInstance().getZivotinjskeVrste()[i];
  78.                 }
  79.                 tvIcon.ImageList = imgList;
  80.             }
  81.         }
  82.  
  83.         private void tvIcon_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
  84.         {
  85.             e.Cancel = true;
  86.         }
  87.  
  88.         private void tvIcon_MouseDown(object sender, MouseEventArgs e)
  89.         {
  90.            _draggedIndexItem = tvIcon.GetNodeAt(e.Location);
  91.  
  92.             if (_draggedIndexItem != null)
  93.             {
  94.                 tvIcon.SelectedNode = _draggedIndexItem;
  95.                 if (_draggedIndexItem.GetNodeCount(true) == 0)
  96.                 {
  97.                     Size dragSize = SystemInformation.DragSize;
  98.                     _mouseDownSelectionWindow = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
  99.                   //  MessageBox.Show(_mouseDownSelectionWindow.ToString());
  100.                     int idx = _draggedIndexItem.ImageIndex;
  101.                     selImg = tvIcon.ImageList.Images[idx];
  102.                 }
  103.                 else
  104.                 {
  105.                     _mouseDownSelectionWindow = Rectangle.Empty;
  106.                 }
  107.             }
  108.         }
  109.  
  110.         private void tvIcon_MouseMove(object sender, MouseEventArgs e)
  111.         {
  112.             if (e.Button == MouseButtons.Left)
  113.             {
  114.                 if ((_mouseDownSelectionWindow != Rectangle.Empty) && (!_mouseDownSelectionWindow.Contains(e.X, e.Y)))
  115.                 {
  116.                     _screenOffset = SystemInformation.WorkingArea.Location;
  117.                     DragDropEffects dropEffect = this.tvIcon.DoDragDrop(_draggedIndexItem, DragDropEffects.Copy);
  118.                 }
  119.  
  120.             }
  121.         }
  122.  
  123.         private void pMap_DragEnter(object sender, DragEventArgs e)
  124.         {
  125.             Type testType = new TreeNode().GetType();
  126.  
  127.             if ((e.Data.GetDataPresent(testType)))
  128.                 e.Effect = DragDropEffects.Copy;
  129.             else
  130.                 e.Effect = DragDropEffects.None;
  131.         }
  132.  
  133.         private void pMap_DragDrop(object sender, DragEventArgs e)
  134.         {
  135.             Type testType = new TreeNode().GetType();
  136.             TreeNode draggedItem;
  137.             _mouseDownSelectionWindow = Rectangle.Empty;
  138.  
  139.             if (e.Data.GetDataPresent(testType))
  140.             {
  141.                 draggedItem = (TreeNode)e.Data.GetData(testType);
  142.          //       draggedItem.ToolTipText = "Prevuceno!";
  143.           //      draggedItem.ForeColor = Color.Gray;
  144.  
  145.                 position = Cursor.Position; // Pozicija kursora na ekranu
  146.                 Point controlRelatedCoords = this.pMap.PointToClient(position); // Pozicija kursora relativna panelu...
  147.  
  148.                 // Obrada:
  149.                 CustomToolTip ctt = new CustomToolTip();
  150.  
  151.                 ZivotinjskeVrste zv = (ZivotinjskeVrste)draggedItem.Tag;
  152.                 PictureBox pb = new PictureBox();
  153.                 pb.Location = new Point(controlRelatedCoords.X, controlRelatedCoords.Y);
  154.                 pb.Image = selImg;
  155.                 pb.Size = new Size(selImg.Width, selImg.Height);;
  156.                 pb.BackColor = Color.Transparent;
  157.                 pb.Tag = draggedItem.Tag;
  158.                 pMap.Controls.Add(pb);
  159.                 Button btn = new Button();
  160.                 pb.Tag = pb.Image; // Hackfix of le year
  161.                 String tt = "Oznaka: " + zv.Oznaka + Environment.NewLine + "Ime: " + zv.Ime + Environment.NewLine + "Tip: " + zv.Tip + Environment.NewLine + "Status ugrozenosti: " + zv.StatusUgrozenosti + Environment.NewLine + "Opasna za ljude: " + zv.OpasnaZaLjude  + Environment.NewLine + "IUCN lista: " + zv.IUCN + Environment.NewLine + "Zivi u naseljenom mjestu: " + zv.NaseljenoMjesto + Environment.NewLine + "Turisticki status: " + zv.TuristickiStatus + Environment.NewLine + "Prihod od turizma: " + zv.Prihod + "Datum otkrivanja: " + zv.DatumOtkrivanja + Environment.NewLine + "Opis: " + zv.Opis;
  162.                 ctt.SetToolTip(pb, tt);
  163.                 // Snimanje u kolekciju!
  164.                 IkoniceKoord ik = new IkoniceKoord();
  165.                 ik.x = controlRelatedCoords.X;
  166.                 ik.y = controlRelatedCoords.Y;
  167.                 ik.icon = selImg;
  168.                 ik.tTip = tt;
  169.  
  170.                 Ikonice.getInstance().getIkonice().Add(ik);
  171.                 Ikonice.getInstance().SaveFile();
  172.                
  173.             }
  174.  
  175.         }
  176.  
  177.         private void button1_Click(object sender, EventArgs e)
  178.         {
  179.             this.DialogResult = DialogResult.OK;
  180.             this.Close();
  181.         }
  182.     }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement