我在实现同一treeview中拖动节点时,希望鼠标在移动的时候,拖动的节点的图片以及文字都能够跟随鼠标移动,我上网查了一段时间,并且在论坛里发帖子,希望能得到这方面的例子,但都没有实现,最后,我只能自己却寻找一些特别的方法来实现这种效果。下面这些代码是我实现这种效果的简单代码,希望能给各位有所帮助,如果你有更好的方法,也希望你对我多多指教。
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ImageList imageList1;
public Form1()
{
InitializeComponent();
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.treeView1 = new System.Windows.Forms.TreeView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.SuspendLayout();
//
// treeView1
//
this.treeView1.AllowDrop = true;
this.treeView1.Font = new System.Drawing.Font("宋体", 12F);
this.treeView1.ImageList = this.imageList1;
this.treeView1.Location = new System.Drawing.Point(8, 8);
this.treeView1.Name = "treeView1";
this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
new System.Windows.Forms.TreeNode("This is a very simple example", 1, 1),
new System.Windows.Forms.TreeNode("but i dont know another way to do such", 2, 2),
new System.Windows.Forms.TreeNode("so I think I can realise this a ticky way", 3, 3),
new System.Windows.Forms.TreeNode("yes if this is helpful for you ,that is my glad ", 4, 4),
new System.Windows.Forms.TreeNode("ok that is all,have a good day", 0, 0)});
this.treeView1.Size = new System.Drawing.Size(360, 264);
this.treeView1.TabIndex = 0;
this.treeView1.DragOver += new System.Windows.Forms.DragEventHandler(this.treeView1_DragOver);
this.treeView1.DragEnter += new System.Windows.Forms.DragEventHandler(this.treeView1_DragEnter);
this.treeView1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseMove);
this.treeView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.treeView1_ItemDrag);
this.treeView1.DragLeave += new System.EventHandler(this.treeView1_DragLeave);
this.treeView1.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeView1_DragDrop);
//
// imageList1
//
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(376, 277);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
ImageForm form;
private void treeView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
TreeNode dragNode = (TreeNode)e.Item;
form = new ImageForm((Bitmap)imageList1.Images[dragNode.ImageIndex],dragNode.Text);
form.Load +=new EventHandler(form_Load);
form.TopLevel = false;
form.Visible = true;
this.Controls.Add(form);
form.BringToFront();
treeView1.DoDragDrop(e.Item,DragDropEffects.Move);
}
private void treeView1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent(typeof(TreeNode)))
{
e.Effect = DragDropEffects.Move;
}
}
private void treeView1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
TreeView tv = sender as TreeView;
TreeNode dragNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
form.Visible = true;
form.Location = new Point(pt.X + 18,pt.Y + 6);
form.BringToFront();
TreeNode overNode = ((TreeView)sender).GetNodeAt(pt);
treeView1.SelectedNode = overNode;
if(overNode == null || dragNode.Equals(overNode))
{
e.Effect = DragDropEffects.None;
}
else
{
e.Effect = DragDropEffects.Move;
}
}
private void treeView1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if(e.Effect == DragDropEffects.Move)
{
TreeView tv = sender as TreeView;
TreeNode dragNode = (TreeNode)e.Data.GetData(typeof(TreeNode));
Point pt = ((TreeView)sender).PointToClient(new Point(e.X, e.Y));
TreeNode destinationNode = ((TreeView)sender).GetNodeAt(pt);
if(destinationNode.Parent == null)
{
treeView1.Nodes.Insert(destinationNode.Index,(TreeNode)dragNode.Clone());
}
else
{
destinationNode.Parent.Nodes.Insert(destinationNode.Index,(TreeNode)dragNode.Clone());
}
if(dragNode.Parent == null)
{
treeView1.Nodes.Remove(dragNode);
}
else
{
dragNode.Parent.Nodes.Remove(dragNode);
}
}
}
private void treeView1_DragLeave(object sender, System.EventArgs e)
{
form.Visible = false;
}
private void treeView1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(form != null)
{
this.Controls.Remove(form);
form = null;
}
}
}
下面是我实现的动态加载的窗体的源码
public class ImageForm : System.Windows.Forms.Form
{
private string text = "";
private Bitmap bitmap;
public ImageForm(Bitmap bitmap,string text)
{
this.text = text;
this.bitmap = bitmap;
InitializeComponent();
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 56);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form2";
this.Text = "Form2";
this.Load += new System.EventHandler(this.Form2_Load);
}
#endregion
private void Form2_Load(object sender, System.EventArgs e)
{
Rectangle rect = this.ClientRectangle;
Bitmap b = new Bitmap(this.Width,this.Height);
using(Graphics g = Graphics.FromImage(b))
{
g.FillRectangle(SystemBrushes.Window,rect);
Rectangle bmpRect = new Rectangle(0,0,bitmap.Width,bitmap.Height);
g.DrawImage(bitmap,bmpRect);
Rectangle textRect = new Rectangle(bitmap.Width + 4,0,rect.Width - bitmap.Width - 4,rect.Height);
g.DrawString(text,this.Font,SystemBrushes.ControlDarkDark,textRect);
Region r = GetRegion(b,SystemColors.Window);
this.BackgroundImage = b;
this.Region = r;
}
}
#region get the region about a image
private Region GetRegion(Bitmap _img, Color color)
{
Color _matchColor=Color.FromArgb(color.R,color.G,color.B);
System.Drawing.Region rgn= new Region();
rgn.MakeEmpty();
Rectangle rc=new Rectangle(0,0,0,0);
bool inimage=false;
for(int y=0; y<_img.Height;y++)
{
for(int x=0;x<_img.Width;x++)
{
if(!inimage)
{
if(_img.GetPixel(x,y)!=_matchColor)
{
inimage=true;
rc.X=x;
rc.Y=y;
rc.Height=1;
}
}
else
{
if(_img.GetPixel(x,y)==_matchColor)
{
inimage=false;
rc.Width=x-rc.X;
rgn.Union(rc);
}
}
}
if(inimage)
{
inimage=false;
rc.Width=_img.Width-rc.X;
rgn.Union(rc);
}
}
return rgn;
}
#endregion
}
感谢bobpowell,我在他的网站上发现了getregion这个方法,解决了我的难题,你可以从
http://www.bobpowell.net/region_from_bitmap.htm看到有关这一方法的文章。