C#仿QQ聊天程序v1.0(IConnectionList接口实现)[易学网]
IConnectionList接口定义
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
/// 用户在线列表接口
/// </summary>
public interface IConnectionList
{
/// <summary>
/// 批量添加用户列表。
/// </summary>
/// <param name="connList"></param>
void AddConnection(IList connList);
/// <summary>
/// 添加一个用户连接
/// </summary>
/// <param name="conn"></param>
void AddConnection(ChatConnection conn);
/// <summary>
/// 在指定位置插入用户连接
/// </summary>
/// <param name="position"></param>
/// <param name="conn"></param>
void AddConnection(int position, ChatConnection conn);
/// <summary>
/// 删除一个用户连接
/// </summary>
/// <param name="account"></param>
void RemoveConnection(string account);
/// <summary>
/// 检查该用户是否在用户列表之中
/// </summary>
/// <param name="account"></param>
/// <returns></returns>
bool IsConnectionExists(string account);
/// <summary>
/// 设置指定用户状态
/// </summary>
/// <param name="account"></param>
/// <param name="state"></param>
void SetConnectionState(string account, ChatState state);
/// <summary>
/// 当用户接受到消息,用户图标自动闪烁(如QQ那样跳动)
/// </summary>
/// <param name="account"></param>
void FlashConnection(string account);
/// <summary>
/// 停止跳动
/// </summary>
/// <param name="account"></param>
void StopFlash(string account);
/// <summary>
/// 关闭所有用户
/// </summary>
void CloseAll();
void CloseAll(bool removeUser);
/// <summary>
/// 增加接收到的消息,加入到聊天记录文件。
/// </summary>
/// <param name="sender"></param>
/// <param name="message"></param>
/// <returns></returns>
int AddReceivedMessage(string sender, string message);
/// <summary>
/// 返回所有连接
/// </summary>
/// <returns></returns>
IList GetConnections();
/// <summary>
/// 返回选中的用户
/// </summary>
/// <returns></returns>
ChatConnection GetSelectedConnection();
/// <summary>
/// 返回指定的用户连接
/// </summary>
/// <param name="account"></param>
/// <returns></returns>
ChatConnection GetConnection(string account);
/// <summary>
/// 显示用户类型(按部门或显示所有用户)
/// </summary>
ShowUser ShowUserType { get; set; }
//易学原创作品,如转载请注明出处 by 易学网 www.vjsdn.net
}
IConnectionList接口实现
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
/// <summary>
/// 用户自定义控件,实现IConnectionList接口
/// 该用户控件显示所有在线用户。
/// </summary>
public partial class ucConnections : UserControl, IConnectionList
{
private ImageSize _ImageSize = ImageSize.Large; //显示图标样式(大图/小图)
private ArrayList _flashs = new ArrayList(); //闪烁图标队列
private Hashtable _receivedMsg = new Hashtable(); //接收到的消息
private IList _connections = new ArrayList();//在线用户
private bool _enableFlash = true;//启动/停止闪烁图标
private NavBarItemLink _hotLink = null;//当前选中的Item
private ShowUser _ShowUserType = ShowUser.All;//显示用户类型
//易学原创作品,如转载请注明出处 by 易学网 www.vjsdn.net
public ucConnections()
{
InitializeComponent();
}
private void ucConnections_Load(object sender, EventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
if (_enableFlash) timerFlash.Start();
}
//显示用户类型
public ShowUser ShowUserType { get { return _ShowUserType; } set { _ShowUserType = value; } }
public void RemoveConnection(string account)
{
try
{
NavBarItem item = GetNavBarItemByAccount(account);
if (item == null) return;
if (_flashs.IndexOf(item) >= 0) _flashs.Remove(item);
navBarControl.BeginUpdate();
navBarControl.ActiveGroup.ItemLinks.Remove(item);
navBarControl.Items.Remove(item);
navBarControl.EndUpdate();
ChatConnection conn = GetConnection(account);
if (conn != null) _connections.Remove(conn);
}
catch (Exception ex)
{
navBarControl.EndUpdate();
Msg.ShowError("RemoveConnection" + ex.Message);
}
}
//批量增加用户.注意BeginUpdate,EndUpdate.不能简化代码
public void AddConnection(IList connList)
{
try
{
navBarControl.BeginUpdate();
foreach (ChatConnection conn in connList)
{
NavBarItem item = GetNavBarItemByAccount(conn.Account);
if (item != null) //更新连线
(item.Tag as ChatConnection).Update(conn);
else //新增
this.AddConnection(conn);
}
}
finally
{
navBarControl.EndUpdate();
}
}
private NavBarGroup GetGroupByFactory(string factory)
{
foreach (NavBarGroup g in navBarControl.Groups)
if (g.Tag.ToString() == factory) return g;
return null;
}
public void AddConnection(int position, ChatConnection conn)
{
this.DoAddConnection(conn, position);
}
public void AddConnection(ChatConnection conn)
{
this.DoAddConnection(conn, -1);
}
//增加一个连接
public void DoAddConnection(ChatConnection conn, int position)
{
try
{
navBarControl.BeginUpdate();
NavBarGroup group;
if (_ShowUserType == ShowUser.ByGroup)
group = this.GetGroupByFactory(conn.FactoryCode);
else
group = navBarControl.ActiveGroup;
if (group == null) group = CreateGroup(conn.FactoryCode);
NavBarItem item = navBarControl.Items.Add();
item.Caption = conn.DisplayName + "(" + conn.FactoryCode + ")";
item.Tag = conn;
item.SmallImageIndex = 0;
if (position < 0)
group.ItemLinks.Add(item); //增加
else
group.ItemLinks.Insert(position, item); //插入
_connections.Add(conn);
}
catch (Exception ex)
{
Msg.ShowError("AddConnection" + ex.Message);
}
finally
{
navBarControl.EndUpdate();
}
}
private NavBarGroup CreateGroup(string factoryCode)
{
NavBarGroup group = navBarControl.Groups.Add();
group.ItemChanged += new EventHandler(OnGroup_ItemChanged);
group.Caption = "用户列表";
group.Tag = factoryCode;
group.LargeImageIndex = (_ShowUserType == ShowUser.All) ? 0 : 1; ;
return group;
}
private void OnGroup_ItemChanged(object sender, EventArgs e)
{
NavBarGroup group = sender as NavBarGroup;
if (_ShowUserType == ShowUser.ByGroup)
{
string caption = "用户列表 ({0}) 部门:{1}";
group.Caption = string.Format(caption, group.ItemLinks.Count, group.Tag);
}
else
{
string caption = "用户列表 ({0}) ";
group.Caption = string.Format(caption, group.ItemLinks.Count);
}
}
public int AddReceivedMessage(string sender, string message)
{
ChatConnection conn = GetConnection(sender);
if (conn != null)
return conn.ReceivedMessage.Add(message);
else
return -1;
}
public ChatConnection GetConnection(string account)
{
for (int i = 0; i <= _connections.Count - 1; i++)
{
ChatConnection conn = _connections[i] as ChatConnection;
if (conn.Account.ToUpper() == account.ToUpper()) return conn;
}
return null;
}
//当收到用户的信息,闪烁图标
public void FlashConnection(string account)
{
NavBarItem item = GetNavBarItemByAccount(account);
if (item == null) return;
if (_flashs.IndexOf(item) < 0) _flashs.Add(item);
}
//闪烁图标的时钟事件
void OnTimerTick(object sender, EventArgs e)
{
if (!_enableFlash) return;
try
{
for (int i = 0; i <= _flashs.Count - 1; i++)
{
if (i > _flashs.Count - 1) continue; //避免异地删除了_flashs中元素.
if (_flashs.Count == 0) continue;
navBarControl.BeginUpdate();
NavBarItem item = _flashs[i] as NavBarItem;
int step = GetFlashStep();
item.SmallImageIndex = step;
item.LargeImageIndex = step;
if (item.Appearance.ForeColor != Color.DodgerBlue)
item.Appearance.ForeColor = Color.DodgerBlue;
else
item.Appearance.ForeColor = Color.Black;
navBarControl.EndUpdate();
}
}
catch { navBarControl.EndUpdate(); }
}
//停止闪烁
public void StopFlash(string account)
{
NavBarItem item = GetNavBarItemByAccount(account);
if (_flashs.IndexOf(item) >= 0)
{
item.Appearance.ForeColor = Color.Black;
item.SmallImageIndex = 0;
item.LargeImageIndex = 0;
_flashs.Remove(item);
}
}
public void CloseAll(bool removeUser)
{
if (removeUser)
{
CloseAll();
}
else //设置状态为脱线
{
try
{
_flashs.Clear();
navBarControl.BeginUpdate();
foreach (NavBarItem item in navBarControl.Items)
{
item.SmallImageIndex = 1;
item.LargeImageIndex = 1;
}
navBarControl.EndUpdate();
}
catch (Exception ex)
{
navBarControl.EndUpdate();
}
}
}
public void CloseAll()
{
try
{
_flashs.Clear();
_connections.Clear();
navBarControl.BeginUpdate();
navBarControl.Items.Clear();
navBarControl.Groups.Clear();
navBarControl.EndUpdate();
}
catch (Exception ex)
{
navBarControl.EndUpdate();
Msg.Warning("CloseAll:" + ex.Message);
}
}
private NavBarItem GetNavBarItemByAccount(string account)
{
for (int i = 0; i <= navBarControl.Items.Count - 1; i++)
{
NavBarItem item = navBarControl.Items[i];
if (item.Tag == null) continue;
ChatConnection conn = item.Tag as ChatConnection;
if (conn.Account == account) return item;
}
return null;
}
public bool IsConnectionExists(string account)
{
return GetConnection(account) != null;
}
public ChatConnection GetSelectedConnection()
{
if (navBarControl.SelectedLink != null && navBarControl.SelectedLink.Item.Tag != null)
return navBarControl.SelectedLink.Item.Tag as ChatConnection;
else
return null;
}
public IList GetConnections()
{
return _connections;
}
public void SetConnectionState(string account, ChatState state)
{
try
{
NavBarItem item = GetNavBarItemByAccount(account);
if (item == null) return;
navBarControl.BeginUpdate();
ChatConnection conn = item.Tag as ChatConnection;
conn.ConnectionState = state;
if (state == ChatState.Hide || state == ChatState.Offline)
{
item.SmallImageIndex = 1;
item.LargeImageIndex = 1;
}
else if (state == ChatState.Online)
{
item.SmallImageIndex = 0;
item.LargeImageIndex = 0;
}
}
finally
{
navBarControl.EndUpdate();
}
}
//记录闪烁图标的当前序号
private int _CurrentFlagStep = 0;
private int GetFlashStep()
{
int[] steps = new int[] { 0, 4, 0, 3 }; //数字代表ImageIndex序号
_CurrentFlagStep += 1;
if (_CurrentFlagStep == 4) _CurrentFlagStep = 0;
return steps[_CurrentFlagStep];
}
//是否允许闪烁图标
[DefaultValue(true)]
public bool EnableFlash
{
get { return _enableFlash; }
set { _enableFlash = value; timerFlash.Enabled = value; }
}
//点击联系人触发的事件
private event OnConnectionClickHandler _OnConnectionClick;
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[Description("点击按钮触发事件")]
[Category("Events")]
public event OnConnectionClickHandler OnConnectionClick
{
add
{
_OnConnectionClick += value;
}
remove
{
_OnConnectionClick -= value;
}
}
//点击菜单触发的事件
private event ToolStripItemClickedEventHandler _OnMenuItemClick;
[EditorBrowsable(EditorBrowsableState.Always)]
[Browsable(true)]
[Description("点击菜单触发事件")]
[Category("Events")]
public event ToolStripItemClickedEventHandler OnMenuItemClick
{
add
{
_OnMenuItemClick += value;
}
remove
{
_OnMenuItemClick -= value;
}
}
private void navBarControl_LinkClicked(object sender, NavBarLinkEventArgs e)
{
if (_OnConnectionClick != null)
{
if (e.Link.Item.Tag != null && e.Link.Item.Tag is ChatConnection)
_OnConnectionClick(e.Link.Item, e.Link.Item.Tag as ChatConnection);
}
}
private void navBarControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
NavBarHitInfo info = navBarControl.CalcHitInfo(e.Location);
_hotLink = info.Link;
navBarControl.ActiveGroup.SelectedLink = info.Link;
}
}
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (_OnMenuItemClick != null) _OnMenuItemClick(sender, e);
}
}
//易学原创作品,如转载请注明出处 by 易学网 www.vjsdn.net
//点击用户触发的事件
public delegate void OnConnectionClickHandler(NavBarItem sender, ChatConnection conn);