C# 高仿腾讯QQ (实现QQ好友列表

您所在的位置:网站首页 怎么分组qq好友 C# 高仿腾讯QQ (实现QQ好友列表

C# 高仿腾讯QQ (实现QQ好友列表

2024-07-17 00:53| 来源: 网络整理| 查看: 265

    C#实现QQ好友列表,一直是我网上百搜却不得其解的控件,几乎找不到相关的信息,而找到的也不是我想要的那种。不过,曾在CSDN上看过到过网上一位大牛用C++实现QQ好友列表这种效果,当然,对于我这个C++基本没学过的小程序员来说,读他的代码却有点像看天书.....不过还好,C++代码看不懂,但从他的文章中却给我了一个实现QQ好友列表的思路-基于ListBox绘法

    之前曾想过用TreeView 、ListView来绘,但归根结底还是因为技术不行而流产,因为找不到合适的方法来重写。        ListBox呢,本身有几个好优势       1、可以用OnMeasureItem方法来实现内部子项的不等高,而我们正好可以利用这点来区分我们QQ好友列表,好友分类与好友之间的区别,不过也有缺陷......

    2、就是不能像QQ好友列表那样分类可以收缩、展开,所以我就用了一个折中的办法,在分类展开时把好友用ListBox的Add()追加与Insert()插入方法而添加进来,反之刚用Remove()与 RemoveAt()方法,把好友移出去....

    好了,思路有了方法也有了,那么唯一的麻烦就是QQ的好友列表他有QQ分类与QQ好友之分,而QQ好友项内又含有头像、网名、备注名、说明、是否开通邮箱、是否带视频、是否开通手机QQ等(其实我们做时没必要这些功能,但在这里还是说一下,因为我也只是为大家提供一种参考思路)

    3、ListBox本身呢它是没有这些项的,它只能添加string字符串,但为了达到我们想要的效果,所以我的方法是,扩展ListBox的Itme的项,让他的功能强大起来,当然这个扩展方法是我在CSwindow程序之窗上看到某位大牛实现带图标的ListBox控件而得来的,这个网站很不错,如果大家想学自定义控件的方法可以去《CSwindow程序之窗》上看看,相信你会收获很多.....

    好了解决这几个问题的方法、思想如果弄清了,我们就可以开始敲代码了......

如:下面代码做的QQ好友列表并不是完整版,完整版可看这个在这里只是为大家演示下,如果有兴趣可以下载下面的源码对比着当做参考一下,如果有不明白可以进 NET技术交流1群:57218890(已满) NET技术交流2群:57219423 群,里面热心的人很多,群共享学习资源也很多,呵呵废话不说,下面贴源

QQListBox.cs文件

  //作者:阿龙(Along) //QQ号:646494711 //QQ群1:57218890 //QQ群2:57219423 //网站:http://www.8timer.com //博客:http://www.cnblogs.com/Along729/ //声明:未经作者许可,任何人不得发布出售该源码,请尊重别人的劳动成果,谢谢大家支持 using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.Drawing;using System.Drawing.Drawing2D;using System.ComponentModel;using System.Xml;

namespace QQListBoxDemo{ /// /// Description of QQListBox. /// public partial class QQListBox : ListBox { public QQListBoxItem mouseitem; public string _xmlpath="QQdate.xml"; private QQListBoxItemCollection _items;

public QQListBox() : base() { _items = new QQListBoxItemCollection(this); base.DrawMode = DrawMode.OwnerDrawVariable; InitializeComponent(); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true);// 双缓冲 this.SetStyle(ControlStyles.ResizeRedraw, true);//调整大小时重绘 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景. this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);// 双缓冲 this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); //this.BackColor=Color.Transparent; } [Localizable(true)] [MergableProperty(false)] [DesignerSerializationVisibility( DesignerSerializationVisibility.Content)]

public string Xmlpath { get { return _xmlpath; } set {_xmlpath=value; } }

public new QQListBoxItemCollection Items { get { return _items; } }

internal ListBox.ObjectCollection OldItems { get { return base.Items; } }

protected override void OnPaint(PaintEventArgs e) { //Bitmap bg=new Bitmap(this.ClientRectangle.Width,this.ClientRectangle.Height); //Graphics g =Graphics.FromImage(bg); Graphics g=e.Graphics; #region 绘制选中项 //判断是否有选中项 if (this.Focused && this.SelectedItem != null) { //得到选中项的区域 Rectangle bounds = this.GetItemRectangle(this.SelectedIndex); //判断选中项是否为分类 if (Items[this.SelectedIndex].IsClass) { //g.FillRectangle(new SolidBrush(Color.FromArgb(230,238,241)),bounds); //Items[this.SelectedIndex].DrawImage(global::QQListBox.Properties.Resources.MainPanel_FolderNode_collapseTexture, new Rectangle(bounds.X+3,bounds.Y+6,12,12)); g.DrawString(Items[this.SelectedIndex].Title, new Font("微软雅黑", 9), new SolidBrush(Color.Black), bounds.Left + 15, bounds.Top + 4); } else { g.FillRectangle(new SolidBrush(Color.FromArgb(252,233,161)),bounds); }

}

#endregion

//循环绘制每项 for (int i = 0; i 0 ) //this.Items.Remove ( this.SelectedItems[0] ); } protected override void OnSelectedIndexChanged(EventArgs e) { base.OnSelectedIndexChanged(e); QQListBoxItem item = Items[this.SelectedIndex]; //MessageBox.Show(this.SelectedIndex.ToString()); if (item.IsClass == true && item.IsExpand == true) { for (int i = this._items.Count - 1; i >= 0; i--) { if (this._items[i].Classid == item.Classid && this._items[i] != item) { this._items.RemoveAt(i); } } item.IsExpand = false; } else if (item.IsClass == true) { item.IsExpand = true; try { XmlDocument doc = new XmlDocument(); doc.Load(_xmlpath); XmlNodeList Qd = doc.DocumentElement.ChildNodes[item.Classid].ChildNodes; int i = 1; foreach (XmlNode Qi in Qd) { this._items.Insert( this.SelectedIndex+i, new QQListBoxItem( item.Classid, int.Parse(Qi.Attributes["qq"].Value), Qi.Attributes["title"].Value, Qi.Attributes["remarks"].Value, Qi.Attributes["text"].Value, new Bitmap(Qi.Attributes["image"].Value), true, true, true)); i++;

}

}

catch (Exception A) { Console.WriteLine("Exception: {0}", A.ToString()); }

}

this.Invalidate(); } protected override void OnDoubleClick(EventArgs e) { base.OnDoubleClick(e); QQListBoxItem item = Items[this.SelectedIndex]; // MessageBox.Show(this.SelectedIndex.ToString()); } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); for(int i = 0; i


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3