Exemplo n.º 1
0
        protected override void OnDrawItem(UI.DrawItemEventArgs args)
        {
            args.DrawBackground();
            UserListItem userListItem = args.Item as UserListItem;

            Graphics g = args.Graphics;

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            Rectangle rect = userListItem.Bounds;

            //头像
            int x = rect.X + MARGIN;
            int y = rect.Y + (rect.Height - ICON_WIDTH) / 2;

            g.DrawImage(ProfilePhotoPool.GetPhoto(userListItem.User.ID), x, y, ICON_WIDTH, ICON_WIDTH);

            //在线状态
            Brush brush = new SolidBrush(Color.Gray);

            if (userListItem.User.Status == UserStatus.Online)
            {
                brush = new SolidBrush(Color.Green);
            }
            else if (userListItem.User.Status == UserStatus.Offline)
            {
                brush = new SolidBrush(Color.Gray);
            }
            else if (userListItem.User.Status == UserStatus.Busy)
            {
                brush = new SolidBrush(Color.Crimson);
            }
            g.FillEllipse(brush, x + ICON_WIDTH - 8, y + ICON_WIDTH - 8, 10, 10);

            g.DrawEllipse(new Pen(Color.White, 1.0f), x + ICON_WIDTH - 8, y + ICON_WIDTH - 8, 11, 11);

            //昵称
            x += ICON_WIDTH + MARGIN;

            Rectangle commandRect = new Rectangle(new Point(x, y), new Size(rect.Width - ICON_WIDTH - 3 * MARGIN, rect.Height - MARGIN));

            TextRenderer.DrawText(g, userListItem.User.NickName,
                                  args.Font, commandRect, Color.FromArgb(0, 0, 0), TextFormatFlags.Left | TextFormatFlags.Top);

            //IP
            string ip = "";

            if (userListItem.User.IP != null)
            {
                ip = userListItem.User.IP.ToString();
            }
            TextRenderer.DrawText(g, ip,
                                  args.Font, commandRect, Color.FromArgb(150, 150, 150), TextFormatFlags.Right | TextFormatFlags.Top);

            if (userListItem.UnreadMessageCount != 0)
            {
                TextRenderer.DrawText(g, userListItem.UnreadMessageCount.ToString(),
                                      args.Font, rect, Color.Red, TextFormatFlags.Right | TextFormatFlags.Bottom);
            }
        }
Exemplo n.º 2
0
        private void FormLogin_Load(object sender, EventArgs e)
        {
            List <NCIInfo> nciInfos = NCIInfo.GetNICInfo(NCIType.Physical | NCIType.Wireless);
            NCIInfo        nciInfo  = nciInfos.Find(new Predicate <NCIInfo>((item) =>
            {
                if (item.MAC == LanConfig.Instance.MAC)
                {
                    return(true);
                }
                return(false);
            }));

            if (nciInfo == null)
            {
                if (nciInfos.Count >= 1)
                {
                    nciInfo = nciInfos[0];
                    LanConfig.Instance.MAC = nciInfo.MAC;
                }
            }

            pictureBox.Image = ProfilePhotoPool.GetPhoto(LanConfig.Instance.MAC);
            labelLogin.Text  = LanConfig.Instance.NickName;
            labelNIC.Text    = nciInfo.Name;
        }
Exemplo n.º 3
0
        private void InitUserListBox(SynchronizationContext context, string filterNickName)
        {
            //task线程调用更新主界面
            context.Post((state) =>
            {
                pictureBoxFace.Image = ProfilePhotoPool.GetPhoto(_user.ID);
                labelName.Text       = _user.NickName;

                UpdateUserStatus();

                userListBox.OwnerUser = _user;

                List <UserListItem> ulItems = new List <UserListItem>();

                foreach (LanUser user in _user.Contacters)
                {
                    if (user.ID == _user.ID)
                    {
                        //忽视自己
                        continue;
                    }
                    if (user.NickName.Contains(filterNickName))
                    {
                        ulItems.Add(new UserListItem(user));
                    }
                }
                userListBox.Items.Clear();
                userListBox.Items.AddRange(ulItems);
            }, null);
        }
Exemplo n.º 4
0
        private void pictureBoxFace_Click(object sender, EventArgs e)
        {
            FormConfig fc = new FormConfig();

            fc.User = _user;
            fc.ShowDialog(this);

            //更新头像等一系列
            pictureBoxFace.Image = ProfilePhotoPool.GetPhoto(_user.ID);
            labelName.Text       = _user.NickName;
        }
Exemplo n.º 5
0
        private void SendUpdateStatePacket(UpdateState state, IPAddress remote, int port, string mac)
        {
            //发送自己的信息,让发送方也更新
            UdpPacket packetRsp = new UdpPacket();

            packetRsp.Address = remote;
            packetRsp.Port    = port;
            packetRsp.ToMAC   = mac;
            packetRsp.FromMAC = this.MAC;
            packetRsp.Command = UdpPacket.CMD_STATE;

            UdpPacketStateExtend entryExtend = new UdpPacketStateExtend();

            if ((state & LanIM.UpdateState.Photo) != 0)
            {
                packetRsp.Command       |= UdpPacket.CMD_OPTION_STATE_PROFILE_PHOTO;
                entryExtend.ProfilePhoto = ProfilePhotoPool.GetPhoto(this.ID, false);
            }
            if ((state & LanIM.UpdateState.NickName) != 0)
            {
                packetRsp.Command   |= UdpPacket.CMD_OPTION_STATE_NICKNAME;
                entryExtend.NickName = this.NickName;
            }
            if ((state & LanIM.UpdateState.PublicKey) != 0)
            {
                packetRsp.Command    |= UdpPacket.CMD_OPTION_STATE_PUBKEY;
                entryExtend.PublicKey = _client.SecurityKeys.Public;
            }
            if ((state & LanIM.UpdateState.Status) != 0)
            {
                packetRsp.Command |= UdpPacket.CMD_OPTION_STATE_STATUS;
                entryExtend.Status = this.Status;
            }

            packetRsp.Extend = entryExtend;

            _client.Send(packetRsp);
        }
Exemplo n.º 6
0
        //发送上线
        public void Login()
        {
            foreach (IPAddress broadcastAddr in this.BroadcastAddress)
            {
                //先发送一个空操作,以防失败
                SendEmptyPacket(broadcastAddr);
                Thread.Sleep(100);

                UdpPacket packet = new UdpPacket();
                packet.Address = broadcastAddr;
                packet.Port    = this.Port;
                packet.ToMAC   = string.Empty;
                packet.Command = UdpPacket.CMD_ENTRY |
                                 UdpPacket.CMD_OPTION_STATE_NICKNAME |
                                 //UdpPacket.CMD_OPTION_STATE_PROFILE_PHOTO | //TODO 应该更新所有的,但是跨网的时候好像发送的byte太长,不能正常发送暂且去掉头像
                                 UdpPacket.CMD_OPTION_STATE_PUBKEY |
                                 UdpPacket.CMD_OPTION_STATE_STATUS;
                packet.FromMAC = this.MAC;

                UdpPacketStateExtend extend = new UdpPacketStateExtend();
                extend.PublicKey    = _client.SecurityKeys.Public;//送公钥出去
                extend.NickName     = this.NickName;
                extend.ProfilePhoto = ProfilePhotoPool.GetPhoto(this.ID, false);
                extend.Status       = this.Status;

                packet.Extend = extend;
                _client.Send(packet);
                Thread.Sleep(100);
            }

            //10分钟以后更新头像
            WaitTimer.Start(1 * 60 * 10 * 1000,
                            new TimerCallback((obj) =>
            {
                UpdateState(LanIM.UpdateState.Photo);
            }
                                              ), null);
        }
Exemplo n.º 7
0
        protected override void OnDrawItem(UI.DrawItemEventArgs args)
        {
            MessageListItem item = args.Item as MessageListItem;
            Message         m    = item.Message;

            args.DrawBackground();

            Graphics g = args.Graphics;

            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            Rectangle rect = item.Bounds;

            Rectangle drawingObjRect;
            Point     pos = PointToClient(MousePosition);

            foreach (DrawingObject dobj in item.DrawingObjects)
            {
                drawingObjRect = dobj.Offset(rect.X, rect.Y);

                switch (dobj.Type)
                {
                //头像
                case DrawingObjectType.ProfilePhoto:
                {
                    g.DrawImage(ProfilePhotoPool.GetPhoto(item.User.ID), drawingObjRect);

                    if (drawingObjRect.Contains(pos))
                    {
                        g.DrawRectangle(IMAGE_BORDER_PEN_FOCUS, drawingObjRect.X, drawingObjRect.Y, drawingObjRect.Width, drawingObjRect.Height);
                    }
                    break;
                }

                //时间, 昵称,状态
                case DrawingObjectType.Title:
                {
                    string text = item.User.NickName + "  " + m.Time.ToString("yyyy/MM/dd HH:mm:ss");
                    if (!m.Flag)
                    {
                        text += "  传送失败";
                    }
                    TextRenderer.DrawText(g, text,
                                          args.Font, new Point((int)drawingObjRect.X, (int)drawingObjRect.Y)
                                          , m.Flag ? SUCCESS_TITLE_COLOR : ERROR_TITLE_COLOR, TextFormatFlags.Left);
                    break;
                }

                //文本消息
                case DrawingObjectType.TextBlock:
                {
                    TextBlockObj tb       = dobj.Tag as TextBlockObj;
                    StringPart   sp       = tb.StringPart;
                    int          selStart = Math.Min(tb.SelectionStart, tb.SelectionEnd);
                    int          selEnd   = Math.Max(tb.SelectionStart, tb.SelectionEnd);

                    string str1 = selStart == -1 ? sp.String : sp.String.Substring(0, selStart);
                    string str2 = selStart == -1 ? "" : sp.String.Substring(selStart, tb.SelectionLength);
                    string str3 = selEnd == -1 ? "" : sp.String.Substring(selEnd + 1);

                    //选择前的文本
                    if (!string.IsNullOrEmpty(str1))
                    {
                        TextRenderer.DrawText(g, str1, sp.Font, new Point((int)drawingObjRect.X, (int)drawingObjRect.Y), args.ForeColor,
                                              StringMeasurer.TextFormatFlags);
                    }

                    //选择中文本
                    if (!string.IsNullOrEmpty(str2))
                    {
                        int w1 = StringMeasurer.Width(g, sp.Font, str1, sp.TextFormatFlags);
                        int w2 = StringMeasurer.Width(g, sp.Font, str2, sp.TextFormatFlags);
                        drawingObjRect.X    += w1;
                        drawingObjRect.Width = w2;

                        g.FillRectangle(BrushPool.GetBrush(TEXT_SELECTION_BACK_COLOR), drawingObjRect);
                        TextRenderer.DrawText(g, str2, sp.Font, new Point((int)drawingObjRect.X, (int)drawingObjRect.Y),
                                              TEXT_SELECTION_FORE_COLOR, StringMeasurer.TextFormatFlags);

                        //选择后的文本
                        if (!string.IsNullOrEmpty(str3))
                        {
                            drawingObjRect.X    += w2;
                            drawingObjRect.Width = dobj.Width - w1 - w2;
                            TextRenderer.DrawText(g, str3, sp.Font, new Point((int)drawingObjRect.X, (int)drawingObjRect.Y), args.ForeColor,
                                                  StringMeasurer.TextFormatFlags);
                        }
                    }
                    break;
                }

                case DrawingObjectType.Image:
                {
                    Image image = (m as ImageMessage).Image;
                    g.DrawImage(image, drawingObjRect);

                    if (drawingObjRect.Contains(pos))
                    {
                        g.DrawRectangle(IMAGE_BORDER_PEN_FOCUS, drawingObjRect.X, drawingObjRect.Y, drawingObjRect.Width, drawingObjRect.Height);
                    }
                    else
                    {
                        g.DrawRectangle(IMAGE_BORDER_PEN, drawingObjRect.X, drawingObjRect.Y, drawingObjRect.Width, drawingObjRect.Height);
                    }
                    break;
                }

                case DrawingObjectType.File:
                {
                    FileMessage fm = m as FileMessage;

                    //底背景
                    g.FillRectangle(Brushes.White, drawingObjRect);
                    if (drawingObjRect.Contains(pos))
                    {
                        g.DrawRectangle(IMAGE_BORDER_PEN_FOCUS, drawingObjRect.X, drawingObjRect.Y, drawingObjRect.Width, drawingObjRect.Height);
                    }
                    else
                    {
                        g.DrawRectangle(IMAGE_BORDER_PEN, drawingObjRect.X, drawingObjRect.Y, drawingObjRect.Width, drawingObjRect.Height);
                    }

                    //文件名,文件大小,ICON
                    Image image       = FileIconPool.GetIcon(fm.OriginFilePath);
                    int   imageWidth  = 64;
                    int   imageHeight = 64 * image.Height / image.Width;

                    Rectangle imageRect    = new Rectangle(drawingObjRect.X + FILE_SEND_WIDTH - imageWidth - MARGIN, drawingObjRect.Y + MARGIN, imageWidth, FILE_SEND_HEIGHT - Font.Height - MARGIN * 3);
                    Rectangle fileNameRect = new Rectangle(drawingObjRect.X + MARGIN, drawingObjRect.Y + MARGIN, FILE_SEND_WIDTH - imageWidth - MARGIN * 3, FILE_SEND_HEIGHT - Font.Height - MARGIN * 3);
                    Rectangle fontRect     = new Rectangle(drawingObjRect.X + MARGIN, drawingObjRect.Y + FILE_SEND_HEIGHT - Font.Height - MARGIN, FILE_SEND_WIDTH - MARGIN * 2, Font.Height);
                    Rectangle pieRect      = new Rectangle(drawingObjRect.X + (FILE_SEND_WIDTH - PROGRESS_PIE_D) / 2, drawingObjRect.Y + (FILE_SEND_HEIGHT - PROGRESS_PIE_D) / 2, PROGRESS_PIE_D, PROGRESS_PIE_D);
                    Rectangle processRect  = new Rectangle(drawingObjRect.X, drawingObjRect.Y + (FILE_SEND_HEIGHT - Font.Height * 2) / 2, FILE_SEND_WIDTH, Font.Height * 2);

                    //当文件名超过显示部分时,中间用省略号表示
                    string str = fm.FileName;
                    SizeF  s   = g.MeasureString(fm.FileName, Font, new System.Drawing.SizeF((float)fileNameRect.Width, (float)fileNameRect.Height), StringFormat.GenericDefault, out int count, out int lines);
                    if (s.Height >= fileNameRect.Height & str.Length >= count)
                    {
                        //由于会出现半行的情况,直接写死了所取的字符串数目
                        str = str.Substring(0, 36) + "・・・" + str.Substring(str.Length - 4);
                    }
                    g.DrawImage(image, imageRect.X, imageRect.Y, imageWidth, imageHeight);
                    g.DrawString(str, args.Font, Brushes.Black, fileNameRect);
                    TextRenderer.DrawText(g, LanFile.HumanReadbleLen(fm.FileLength), args.Font, fontRect, Color.FromArgb(0, 0, 0), TextFormatFlags.Left);

                    if (item.State == MessageState.Receiving ||
                        item.State == MessageState.Sending)
                    {
                        //接受进度
                        g.FillRectangle(MESSAGELIST_PROGRESS_BACKGROUND_BRUSH, drawingObjRect);

                        g.DrawPie(PROGRESS_PEN, pieRect, 0, 360);
                        g.FillPie(MESSAGELIST_PROGRESS_FILLED_BRUSH, pieRect, 0, 360 * item.Progress / 100);

                        //TODO 显示剩余时间,传输速度
                        TextRenderer.DrawText(g, item.Progress + "%" + "\r\n" + LanFile.HumanReadbleLen(item.FileTransportedLength) + "/" + LanFile.HumanReadbleLen(fm.FileLength),
                                              args.Font, processRect, Color.FromArgb(0, 0, 0), TextFormatFlags.HorizontalCenter);
                    }
                    break;
                }
                }
            }
        }