分享
 
 
 

在同一treeview中拖放节点并实现动态图像跟随

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

我在实现同一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看到有关这一方法的文章。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有