Originariamente inviato da fu510n
Comunque sia l'idea di un controllo personalizzabile e bindabile è la cosa migliore da fare.
ok mi sto muovendo in questa direzione.. quindi sto portando il codice relativo alle postazioni dentro la classe postazione.
Sto trovando qualche difficoltà a portare dentro il codice relativo allo spostamento della postazione..
ti posto quello che ho fatto finora:
Codice PHP:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Collections;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
Image imgEstintore = Image.FromFile("img/estintore.JPG");
Image background = Image.FromFile("img/background.jpg");
Panel Mappa;
public static int WIDTH = 800;
public static int HEIGHT = 550;
public Form1()
{
InitializeComponent();
Text = "Postazioni antincendio";
Size = new Size(WIDTH, HEIGHT + 40);
MaximizeBox = false;
//FormBorderStyle = FormBorderStyle.FixedSingle;
CenterToScreen();
Mappa = new System.Windows.Forms.Panel();
Mappa.Location = new System.Drawing.Point(0, 0);
Mappa.Size = new System.Drawing.Size(ClientSize.Width, ClientSize.Height - 40);
Mappa.BackgroundImage = background;
Mappa.BackgroundImageLayout = ImageLayout.Stretch;
Mappa.AllowDrop = true;
Mappa.DragDrop += new DragEventHandler(Mappa_DragDrop);
Mappa.DragEnter += new DragEventHandler(Mappa_DragEnter);
Mappa.MouseDown += new MouseEventHandler(Mappa_MouseDown);
Controls.Add(Mappa);
}
private void Mappa_DragDrop(object sender, DragEventArgs e)
{
Postazione DraggingObject = (Postazione)e.Data.GetData(typeof(Postazione));
Console.WriteLine(DraggingObject.Name);
DraggingObject.Location = Mappa.PointToClient(new Point(e.X - (DraggingObject.Size.Width / 2), e.Y - (DraggingObject.Height / 2)));
DraggingObject = null;
e.Effect = DragDropEffects.None;
}
private void Mappa_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void aggiungiPostazioneToolStripMenuItem_Click(object sender, EventArgs e)
{
Mappa.SuspendLayout();
int x = contextMenuAggiungi.Left;
int y = contextMenuAggiungi.Top;
Postazione currPost = new Postazione(x, y, Mappa);
Mappa.ResumeLayout();
}
private void Mappa_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
ContextMenuStrip = contextMenuAggiungi;
}
}
}
public partial class Postazione : UserControl
{
ContextMenuStrip contextMenuRimuovi;
ToolStripMenuItem rimuoviPostazioneToolStripMenuItem;
Postazione LastContextSender;
Image imgEstintore = Image.FromFile("img/estintore.JPG");
public Postazione(int x, int y, Panel container)
{
Location = container.PointToClient(new Point(x - imgEstintore.Size.Width / 2, y - imgEstintore.Size.Height / 2));
Size = new Size(imgEstintore.Size.Width + 2, imgEstintore.Size.Height + 2);
BorderStyle = BorderStyle.FixedSingle;
contextMenuRimuovi = new ContextMenuStrip();
rimuoviPostazioneToolStripMenuItem = new ToolStripMenuItem();
contextMenuRimuovi.Items.AddRange(new System.Windows.Forms.ToolStripItem[]
{rimuoviPostazioneToolStripMenuItem});
contextMenuRimuovi.Name = "contextMenuRimuovi";
contextMenuRimuovi.Click += new System.EventHandler(this.contextMenuRimuovi_Click);
rimuoviPostazioneToolStripMenuItem.Name = "rimuoviPostazioneToolStripMenuItem";
rimuoviPostazioneToolStripMenuItem.Text = "Rimuovi postazione";
ContextMenuStrip = contextMenuRimuovi;
MouseDown += new MouseEventHandler(DragMouseDown);
container.Controls.Add(this);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics graphics = e.Graphics;
graphics.DrawImageUnscaled(imgEstintore, 0, 0);
}
private void DragMouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
((Postazione)sender).DoDragDrop(sender, DragDropEffects.Move);
}
else if (e.Button == MouseButtons.Right)
{
LastContextSender = (Postazione)sender;
}
}
private void contextMenuRimuovi_Click(object sender, EventArgs e)
{
LastContextSender.Dispose();
}
}
}