PDA

View Full Version : اضافه کردن چندین پنل در حالت runtime و امکان حرکت دادن آنها روی فرم بوسیله ماوس



bahar_engineer
یک شنبه 15 آذر 1388, 18:12 عصر
سلام

می خوام روی صفحه بتونم در حالت runtime با کلیک روی بعضی از picturebox ها پنل اضافه کنم ..

مثلا چند picturebox توی فرم دارم و هر کدام عکس یکی از قطعات مدار را دارند. حالا می خوام روی هر کدوم که کلیک می کنم یه پنل به فرم اضافه بشه و من بتونم اونو حرکت بدم

این کدها رو نوشتم اما در runtime عمل نمی کنه چون نمی تونم اینطوری event های پنل رو set کنم



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ContrloCircuit
{
public partial class Form1 : Form
{
protected Point loc;
private PictureBox selectpic;
//Start point of the line.
private Point _startPoint = Point.Empty;
//End point of the line.
private Point _endPoint = Point.Empty;
// protected Panel panel1 = new Panel();

public Form1()
{
InitializeComponent();
Image img = Image.FromFile(@"E:dog.jpg");
this.picBox.AllowDrop = true;
this.picBox.DragDrop += new System.Windows.Forms.DragEventHandler(this.picBox_ DragDrop);
this.picBox.DragEnter += new System.Windows.Forms.DragEventHandler(this.picBox_ DragEnter);
}

private void pictureBox1_Click(object sender, EventArgs e)
{
// data mydata = new data();
// picBox.Controls.Add(mydata.MyPanel);
// mydata.MyPanel.BackgroundImage = pictureBox1.Image;
// mydata.MyPanel.Height = pictureBox1.Height;
// mydata.MyPanel.Width = pictureBox1.Width;
panel1.BackgroundImage = pictureBox1.Image;
panel1.AllowDrop = true;
}

private void pictureBox3_Click(object sender, EventArgs e)
{
// Panel panel1 = new Panel();
// this.Controls.Add(panel1);
// panel1.BackgroundImage = pictureBox3.Image;
}

private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
//target control will accept data here
// PictureBox destination = (PictureBox)sender;
// destination.Image = (Bitmap)e.Data.GetData(typeof(Bitmap));
}

private void picBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}

private void picBox_DragDrop(object sender, DragEventArgs e)
{
// PictureBox picbox = (PictureBox)sender;
// Graphics g = picbox.CreateGraphics();
// g.DrawImage((Image)e.Data.GetData(DataFormats.Bitm ap), myloc);
// label1.Text = myloc.ToString();
PictureBox destination = (PictureBox)sender;
destination.Image = (Bitmap)e.Data.GetData(typeof(Bitmap));
}

private void pictureBox3_MouseDown(object sender, MouseEventArgs e)
{
// PictureBox mypic = (PictureBox)sender;
// mypic.DoDragDrop(mypic.Image, DragDropEffects.Copy);
}

private void panel1_MouseDown(object sender, MouseEventArgs e)
{
Panel mypic = (Panel)sender;
mypic.DoDragDrop(mypic.BackgroundImage, DragDropEffects.Move);
panel1.Location = loc;
}

private void picBox_DragOver(object sender, DragEventArgs e)
{
// myloc.X = e.X;
// myloc.Y = e.Y;
}

private void panel1_DragOver(object sender, DragEventArgs e)
{
// myloc.X = e.X;
// myloc.Y = e.Y;
panel1.Location = loc;
}


private void timer1_Tick(object sender, EventArgs e)
{
loc = Cursor.Position;
label1.Text = loc.ToString();
}

private void panel1_DragEnter(object sender, DragEventArgs e)
{
panel1.Location = loc;
}

private void button1_Click(object sender, EventArgs e)
{
//Initial the start point
this.picBox.MouseDown += new MouseEventHandler(picBox_MouseDown);
//Update the end point and fire the paint event.
this.picBox.MouseMove += new MouseEventHandler(picBox_MouseMove);
//Paint line on picture box to help user adjust the end point.
this.picBox.Paint += new PaintEventHandler(picBox_Paint);
//Paint line to image here. Reinitial the states.
this.picBox.MouseUp += new MouseEventHandler(picBox_MouseUp);
}

private void picBox_Paint(object sender, PaintEventArgs e)
{
if (_startPoint != Point.Empty && _endPoint != Point.Empty && _startPoint != _endPoint)
{
//Draw line.
e.Graphics.DrawLine(Pens.Red, _startPoint, _endPoint);
}
}

private void picBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//Initial the start point of the line.
_startPoint = e.Location;
}
}

private void picBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//Update the end point.
_endPoint = e.Location;
//Force to refresh UI.
this.picBox.Refresh();
}
}

private void picBox_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_endPoint = e.Location;
PaintLineOnImage();
_startPoint = Point.Empty;
_endPoint = Point.Empty;
}
}

private void PaintLineOnImage()
{
if (_startPoint != Point.Empty && _endPoint != Point.Empty && _startPoint != _endPoint)
{
//Get graphics from image.
Graphics g = Graphics.FromImage(picBox.Image);
//Draw line on image.
g.DrawLine(Pens.Red, _startPoint, _endPoint);
g.Dispose();
//Force to refresh UI.
this.picBox.Refresh();
}
}

}
}


این یه سری کدهای رسم خط هم داره

لطفا کمک کنید... اگه یه پنل بذارم روی صفحه مشکلی نیست اما اگه با همون اسم در حالت اجرا اضافه کنم پنل حرکت نمی کنه

bahar_engineer
دوشنبه 16 آذر 1388, 13:07 عصر
جهت بالا اومدن تاپیک