xxx

来源:互联网 发布:中国好吃的泡面知乎 编辑:程序博客网 时间:2024/05/19 14:19
if (bytesFromClient[0] == l[0] && bytesFromClient[1] == l[1] && bytesFromClient[2] == l[2] && bytesFromClient[3] == l[3])
                        {
                            byte[] i = new byte[1024];
                            Array.Copy(bytesFromClient, 4, i, 0, 1024);
                            string a = Encoding.UTF8.GetString(i);
                            a = a.Substring(0, a.IndexOf("\0"));
                            string target = a.Substring(0,a.IndexOf("$"));
                            Socket targetSocket = null;
                            if (clientList.TryGetValue(target, out targetSocket))
                            {

                                byte[] b = new byte[4096];
                                byte[] j = Encoding.UTF8.GetBytes(name+"$"+a.Substring(a.IndexOf("$")+1));
                                Array.Copy(BitConverter.GetBytes(-4), b, 4);
                                Array.Copy(j, 0, b, 4, j.Length);
                                targetSocket.Send(b);
                            }

                        }

try{
                    if(clientSocket!=null&&clientSocket.Connected)
                    {
                        Int32 len = clientSocket.Receive(bytesFrom);
                        if(len>-1)
                        {
                            dataFromClient = Encoding.UTF8.GetString(bytesFrom,0,len);
                            Int32 sublen = dataFromClient.LastIndexOf("$");
                            if(sublen>-1)
                            {
                                dataFromClient = dataFromClient.Substring(0,sublen);
                                if(!clientList.ContainsKey(dataFromClient))
                                {

                                    clientList.Add(dataFromClient,clientSocket);
                                    Console.WriteLine(dataFromClient+"加入弹幕");
                                    BroadCast.sentList(clientList);
                                    HandleClass client = new HandleClass();
                                    client.StartClient(clientSocket,dataFromClient,clientList);
                                }
                            }
                        }
                    }
                }

public static void sentList(Dictionary<string, Socket> clientList)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string k in clientList.Keys)
            {
                sb.Append(k + "$");
            }

            byte[] b = new byte[4096];
            byte[] i = Encoding.UTF8.GetBytes(sb.ToString());
            Array.Copy(BitConverter.GetBytes(-3), b, 4);
            Array.Copy(i, 0, b, 4, i.Length);

            foreach (var item in clientList)
            {
                Socket brdcastSocket = (Socket)item.Value;
                try
                {
                    Byte[] bytesFromClient = new Byte[4096];
                    brdcastSocket.Send(b);
                }
                catch (Exception e)
                {
                    brdcastSocket.Close();
                    brdcastSocket = null;
                    Console.WriteLine(e.ToString());
                    continue;
                }
            }
        }


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Threading;

namespace chunClient.Chun.Animation
{
    public class TalkAnimation
    {
        private static LinkedList<Border> list = new LinkedList<Border>();
        private System.Windows.Controls.Canvas canvas;
        private string input = string.Empty;
        private double Width;
        private Border border;
        private BitmapImage bi;
        DispatcherTimer t2 = new DispatcherTimer();
        private BitmapImage mbi;
        System.Windows.Controls.Label l;

        public TalkAnimation(BitmapImage bi, Canvas canvas, Border input, string p1, double p2)
        {
            // TODO: Complete member initialization
            this.bi = bi;
            this.border = input;
            this.canvas = canvas;
            this.input = p1;
            this.Width = p2;
            this.canvas.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new de(init));
        }

        public TalkAnimation(BitmapImage mbi, Canvas canvas, Border border, string p1, double p2, int p3)
        {
            // TODO: Complete member initialization
            this.mbi = mbi;
            this.canvas = canvas;
            this.border = border;
            this.input = p1;
            this.Width = p2;
            this.canvas.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new de(init1));
        }

        private void init1()
        {
            try
            {
                Border b = new Border();
                b.BorderBrush = new SolidColorBrush(Colors.Black);
                b.BorderThickness = new Thickness(8, 8, 8, 8);
                b.CornerRadius = new CornerRadius(13);
                b.Opacity = 0.5;
                l = new Chun.Label.MyLabel(new Chun.Message.Message(input, 1)).Label;
                double location = Canvas.GetTop(this.border) - 45;
                if (list.Count != 0)
                {
                    Border k = list.Last<Border>();
                    foreach (Border z in list)
                    {
                        double temp = Canvas.GetTop(z) - 40;
                        DoubleAnimation da = new DoubleAnimation(Canvas.GetTop(z), temp, TimeSpan.FromSeconds(0.2));
                        Storyboard storyboard = new Storyboard();
                        Storyboard.SetTarget(da, z);
                        Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.TopProperty));
                        storyboard.Children.Add(da);
                        storyboard.Begin();

                    }
                }
                jj1(location);
                b.Child = l;
                Canvas.SetTop(b, location);
                Canvas.SetRight(b, this.Width - Canvas.GetLeft(this.border) - 389);
                this.canvas.Children.Add(b);
                list.AddLast(b);
                DispatcherTimer t1 = new DispatcherTimer();
                t1.Interval = TimeSpan.FromSeconds(3);
                t1.Tick += delegate
                {
                    this.canvas.Children.Remove(b);
                    list.Remove(b);
                    t1.Stop();
                    t1 = null;
                };
                t1.Start();
                b.MouseEnter += delegate
                {
                    Canvas.SetRight(b, this.Width - Canvas.GetLeft(this.border) - 389 + 20);
                    t2.Stop();
                    t1.Stop();

                };
                b.MouseLeave += delegate
                {
                    Canvas.SetRight(b, this.Width - Canvas.GetLeft(this.border) - 389);
                    t2.Start();
                    t1.Start();
                };

            }
            catch (Exception e)
            {
                MessageBox.Show("重启下试试^_^");
            }
        }

        private void jj1(double location)
        {
             Border b1 = new Border();
            b1.BorderBrush = new SolidColorBrush(Colors.Black);
            b1.BorderThickness = new Thickness(8, 8, 8, 8);
            b1.CornerRadius = new CornerRadius(13);
            b1.Opacity = 0.5;
            Image i = new Image();
            i.Source = mbi;
            i.Width = 40;
            i.Height = 40;
            b1.Child = i;
            Canvas.SetTop(b1, location);
            Canvas.SetRight(b1,this.Width - Canvas.GetLeft(this.border) - 389 - 60);
            this.canvas.Children.Add(b1);
            list.AddLast(b1);
            t2.Interval = TimeSpan.FromSeconds(3);
            t2.Tick += delegate
            {
                this.canvas.Children.Remove(b1);
                list.Remove(b1);
                t2.Stop();
                t2 = null;
            };
            t2.Start();
           
        }
        
        
        private void init()
        {
            try
            {
                Border b = new Border();
                b.BorderBrush = new SolidColorBrush(Colors.Black);
                b.BorderThickness = new Thickness(8, 8, 8, 8);
                b.CornerRadius = new CornerRadius(13);
                b.Opacity = 0.5;
                l = new Chun.Label.MyLabel(new Chun.Message.Message(input,1)).Label;
                double location = Canvas.GetTop(this.border) - 45;
                if(list.Count!=0)
                {
                    Border k = list.Last<Border>();
                    foreach(Border z in list)
                    {
                        double temp = Canvas.GetTop(z)-40;
                        DoubleAnimation da = new DoubleAnimation(Canvas.GetTop(z), temp, TimeSpan.FromSeconds(0.2));
                        Storyboard storyboard = new Storyboard();
                        Storyboard.SetTarget(da, z);
                        Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.TopProperty));
                        storyboard.Children.Add(da);
                        storyboard.Begin();
                        
                    }
                }
                jj(location);
                b.Child = l;
                Canvas.SetTop(b, location);
                Canvas.SetLeft(b, Canvas.GetLeft(this.border));
                this.canvas.Children.Add(b);
                list.AddLast(b);
                DispatcherTimer t1 = new DispatcherTimer();
                t1.Interval = TimeSpan.FromSeconds(3);
                t1.Tick += delegate
                {
                    this.canvas.Children.Remove(b);
                    list.Remove(b);
                    t1.Stop();
                    t1 = null;
                };
                t1.Start();
                b.MouseEnter += delegate
                {
                    Canvas.SetLeft(b, Canvas.GetLeft(this.border) + 20);
                    t2.Stop();
                    t1.Stop();
                    
                };
                b.MouseLeave += delegate
                {
                    Canvas.SetLeft(b, Canvas.GetLeft(this.border));
                    t2.Start();
                    t1.Start();
                };
                
            }
            catch (Exception e)
            {
                MessageBox.Show("重启下试试^_^");
            }



        }

        private void jj(double location)
        {
            Border b1 = new Border();
            b1.BorderBrush = new SolidColorBrush(Colors.Black);
            b1.BorderThickness = new Thickness(8, 8, 8, 8);
            b1.CornerRadius = new CornerRadius(13);
            b1.Opacity = 0.5;
            Image i = new Image();
            i.Source = bi;
            i.Width = 40;
            i.Height = 40;
            b1.Child = i;
            Canvas.SetTop(b1, location);
            Canvas.SetLeft(b1, Canvas.GetLeft(this.border)-60);
            this.canvas.Children.Add(b1);
            list.AddLast(b1);
            t2.Interval = TimeSpan.FromSeconds(3);
            t2.Tick += delegate
            {
                this.canvas.Children.Remove(b1);
                list.Remove(b1);
                t2.Stop();
                t2 = null;
            };
            t2.Start();
           
        }

     

    }
}

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;

namespace chunClient.Chun.Client
{
    public class Client
    {
        private static Socket clientSocket = null;
        private static Boolean isListen = true;
        private System.Windows.Controls.Canvas canvas;
        private double Width;
        private MainWindow mainWindow;
        private Border border;
        public static Dictionary<string,TalkWindow> talks=new Dictionary<string,TalkWindow>();
        public Client(System.Windows.Controls.Canvas canvas, Border input, double p2, MainWindow mainWindow)
        {
            this.border = input;
            this.canvas = canvas;
            this.mainWindow = mainWindow;
            this.Width = p2;
            try
            {
                if (clientSocket == null || !clientSocket.Connected)
                {
                    clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    clientSocket.BeginConnect(IPAddress.Parse("192.168.22.198"), 8888, (a) =>
                    {
                        if (a.IsCompleted)
                        {
                            Byte[] bytesSend = new Byte[4096];
                            bytesSend = Encoding.UTF8.GetBytes(Dns.GetHostName()+"$");
                            if (clientSocket != null && clientSocket.Connected)
                            {
                                clientSocket.Send(bytesSend);
                                Thread th = new Thread(DataFromServer);
                                th.IsBackground = true;
                                th.Start();

                            }
                        }
                    }, null);
                }
            }

            catch (Exception e)
            {
                MessageBox.Show("..可能有问题1");
            }
        }

       
        public void sendMsg(string input)
        {
            Byte[] bytesSend = new Byte[4096];
            bytesSend = Encoding.UTF8.GetBytes(input);
            try
            {
                clientSocket.Send(bytesSend);
            }
            catch(Exception e)
            {
                MessageBox.Show("发不出去");
            }
        }

        private void DataFromServer()
        {
            isListen = true;
            try
            {
                while(isListen)
                {
                    Byte[] bytesFrom = new Byte[4096];
                    byte[] j = BitConverter.GetBytes(-1);
                    byte[] k = BitConverter.GetBytes(-2);
                    byte[] h = BitConverter.GetBytes(-3);
                    byte[] l = BitConverter.GetBytes(-4);
                    Int32 len = clientSocket.Receive(bytesFrom);
                    if (bytesFrom[0] == j[0] && bytesFrom[1] == j[1] && bytesFrom[2] == j[2] && bytesFrom[3] == j[3])
                    {
                        Thread th = new Thread(receiveFile);
                        th.Start(bytesFrom);
                        th.Join();
                    }
                    else if (bytesFrom[0] == k[0] && bytesFrom[1] == k[1] && bytesFrom[2] == k[2] && bytesFrom[3] == k[3])
                    {
                        byte[] i = new byte[1024];
                        Array.Copy(bytesFrom, 4, i, 0, 1024);
                        string a = Encoding.UTF8.GetString(i);
                        a = a.Substring(0, a.IndexOf("\0"));
                        Int32 b = Int32.Parse(a);
                        this.mainWindow.interval = b*1000;
                    }
                    else if (bytesFrom[0] == h[0] && bytesFrom[1] == h[1] && bytesFrom[2] == h[2] && bytesFrom[3] == h[3])
                    {
                        byte[] i = new byte[1024];
                        Array.Copy(bytesFrom, 4, i, 0, 1024);
                        string a = Encoding.UTF8.GetString(i);
                        a = a.Substring(0, a.IndexOf("\0"));
                        string[] kis = a.Split('$');
                        Context.setOnlineUsers(kis);
                    }
                    else if (bytesFrom[0] == l[0] && bytesFrom[1] == l[1] && bytesFrom[2] == l[2] && bytesFrom[3] == l[3])
                    {
                        byte[] i = new byte[1024];
                        Array.Copy(bytesFrom, 4, i, 0, 1024);
                        string a = Encoding.UTF8.GetString(i);
                        a = a.Substring(0, a.IndexOf("\0"));
                        string name = a.Substring(0, a.IndexOf("$"));
                        if (talks.ContainsKey(name))
                        {
                            talks[name].Dispatcher.BeginInvoke(new Action(()=>
                            {
                                talks[name].recieveMsg(a.Substring(a.IndexOf("$")+1));
                            }));
                        }
                        else
                        {
                            talks.Add(name, new TalkWindow(name,this));
                            talks[name].Dispatcher.BeginInvoke(new Action(() =>
                            {
                                talks[name].recieveMsg(a.Substring(a.IndexOf("$") + 1));
                            }));
                        }

                    }
                    else
                    {
                        string dataFromClientTmp = Encoding.UTF8.GetString(bytesFrom, 0, len);
                        new Chun.Animation.DanMuModeTwoAnimation(this.canvas,this.border, dataFromClientTmp, this.Width);
                    }
                }
            }
            catch (Exception e)
            {
                isListen = false;
                MessageBox.Show("..可能有问题2");
            }
        }

        private void receiveFile(object obj)
        {
            byte[] bytesFrom = (byte[])obj;
            byte[] fLength = new byte[8] { bytesFrom[4], bytesFrom[5], bytesFrom[6], bytesFrom[7], bytesFrom[8], bytesFrom[9], bytesFrom[10], bytesFrom[11] };
            byte[] filename = new byte[4096];
            Array.Copy(bytesFrom,12,filename,0,bytesFrom.Length-12);
            int length = BitConverter.ToInt32(fLength,0);
            string fileName = Encoding.UTF8.GetString(filename);
            fileName = fileName.Substring(0,fileName.IndexOf("\0\0") );
            fileName = fileName.Substring(fileName.LastIndexOf("\\")+1);

            FileStream fs = new FileStream("D:\\" + fileName, FileMode.Create);
            MemoryStream ms = new MemoryStream();
            try
            {
                int bytesRead = 0;
                long count = 0;
                byte[] buffer = new byte[8192];

                while (count < length)
                {
                    bytesRead = clientSocket.Receive(buffer, buffer.Length, 0);
                    ms.Write(buffer, 0, bytesRead);
                    count += bytesRead;
                }
                fs.Write(ms.ToArray(), 0, ms.ToArray().Length);
            }
            finally
            {
                fs.Close();
                ms.Close();
            }

        }

        internal void sendSMsg(string p,string hostname)
        {
            Byte[] bytesSend = new Byte[4096];
            byte[] j = Encoding.UTF8.GetBytes(hostname + "$" + p);
            Array.Copy(BitConverter.GetBytes(-4), bytesSend, 4);
            Array.Copy(j, 0, bytesSend, 4, j.Length);
            try
            {
                clientSocket.Send(bytesSend);
            }
            catch (Exception e)
            {
                MessageBox.Show("发不出去");
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace chunClient
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private Chun.Client.Client client = null;
        
        Storyboard storyboard;
        public MainWindow()
        {
            InitializeComponent();

            jj.MouseEnter += jj_MouseEnter;
            jj.MouseLeave += jj_MouseLeave;
            foreach (UIElement uiEle in this.canvas.Children)
            {
                //WPF设计上的问题,Button.Clicked事件Supress掉了Mouse.MouseLeftButtonDown附加事件等.
                //不加这个Button、TextBox等无法拖动
                if (uiEle is Button || uiEle is TextBox)
                {
                    uiEle.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Element_MouseLeftButtonDown), true);
                    uiEle.AddHandler(Button.MouseMoveEvent, new MouseEventHandler(Element_MouseMove), true);
                    uiEle.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Element_MouseLeftButtonUp), true);
                    continue;
                }
                //
                uiEle.MouseMove += new MouseEventHandler(Element_MouseMove);
                uiEle.MouseLeftButtonDown += new MouseButtonEventHandler(Element_MouseLeftButtonDown);
                uiEle.MouseLeftButtonUp += new MouseButtonEventHandler(Element_MouseLeftButtonUp);
            }         
            this.Loaded += MainWindow_Loaded;
            this.input.Background = new SolidColorBrush(Colors.Transparent);
            this.input.BorderBrush = new SolidColorBrush(Colors.Violet);
            this.input.KeyDown += input_KeyDown;
            this.input.Opacity = 0.7;
            DoubleAnimation da = new DoubleAnimation(0, this.input.Height, TimeSpan.FromSeconds(0.15));
            storyboard = new Storyboard();
            Storyboard.SetTarget(da, this.input);
            Storyboard.SetTargetProperty(da, new PropertyPath(TextBox.HeightProperty));
            storyboard.Children.Add(da);
            client = new Chun.Client.Client(this.canvas,this.inputBorder, SystemParameters.FullPrimaryScreenWidth,this);
        }
        private Dictionary<Border,Label> ddd = new Dictionary<Border,Label>();

        Random r = new Random(6);

        double h = SystemParameters.FullPrimaryScreenHeight;
        double w = SystemParameters.FullPrimaryScreenWidth;
        Storyboard s1 = new Storyboard();
        int flag2 = 0;
        DispatcherTimer t = new DispatcherTimer();
        void jj_MouseLeave(object sender, MouseEventArgs e)
        {
            #region
            DoubleAnimation da;
            Storyboard s = new Storyboard();

            if (Canvas.GetLeft(jj) >= SystemParameters.FullPrimaryScreenWidth - 50)
            {
                da = new DoubleAnimation(Canvas.GetLeft(jj), SystemParameters.FullPrimaryScreenWidth, TimeSpan.FromSeconds(0.5));

                Storyboard.SetTarget(da, jj);
                Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.LeftProperty));
                s.Children.Add(da);
                s.Completed += delegate
                {
                    Canvas.SetLeft(jj, SystemParameters.FullPrimaryScreenWidth);
                    s.Stop();
                };
                s.Begin();

            }
            if (Canvas.GetLeft(jj) <= 0)
            {
                da = new DoubleAnimation(Canvas.GetLeft(jj), -50, TimeSpan.FromSeconds(0.5));

                Storyboard.SetTarget(da, jj);
                Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.LeftProperty));
                s.Children.Add(da);
                s.Completed += delegate
                {
                    Canvas.SetLeft(jj, -50);
                    s.Stop();
                };
                s.Begin();

            }
            if (Canvas.GetTop(jj) <= 0)
            {
                da = new DoubleAnimation(Canvas.GetTop(jj), -50, TimeSpan.FromSeconds(0.5));

                Storyboard.SetTarget(da, jj);
                Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.TopProperty));
                s.Children.Add(da);
                s.Completed += delegate
                {
                    Canvas.SetTop(jj, -50);
                    s.Stop();
                };
                s.Begin();

            }
            #endregion
            
            t.Interval = TimeSpan.FromMilliseconds(3 * 1000);
            t.Tick += delegate
            {
                foreach (Border b in ddd.Keys)
                {
                    Label l = ddd[b];
                    l.Visibility = System.Windows.Visibility.Hidden;
                    b.Visibility = System.Windows.Visibility.Hidden;
                }
                flag2 = 0;
                s1.Stop();
            };
            t.Start();
        }

        void jj_MouseEnter(object sender, MouseEventArgs e)
        {
            #region
            DoubleAnimation da;
            Storyboard s = new Storyboard();
            if (Canvas.GetLeft(jj) <= 0)
            {
                da = new DoubleAnimation(Canvas.GetLeft(jj), 0, TimeSpan.FromSeconds(0.5));

                Storyboard.SetTarget(da, jj);
                Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.LeftProperty));
                s.Children.Add(da);
                s.Completed += delegate
                {
                    Canvas.SetLeft(jj, 0);
                    s.Stop();
                };
                s.Begin();
            }
            if (Canvas.GetLeft(jj) >= SystemParameters.FullPrimaryScreenWidth - 50)
            {
                da = new DoubleAnimation(Canvas.GetLeft(jj), SystemParameters.FullPrimaryScreenWidth - 50, TimeSpan.FromSeconds(0.5));

                Storyboard.SetTarget(da, jj);
                Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.LeftProperty));
                s.Children.Add(da);
                s.Completed += delegate
                {
                    Canvas.SetLeft(jj, SystemParameters.FullPrimaryScreenWidth - 50);
                    s.Stop();
                };
                s.Begin();
            }
            if (Canvas.GetTop(jj) <= 0)
            {
                da = new DoubleAnimation(Canvas.GetTop(jj), 0, TimeSpan.FromSeconds(0.5));

                Storyboard.SetTarget(da, jj);
                Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.TopProperty));
                s.Children.Add(da);
                s.Completed += delegate
                {
                    Canvas.SetTop(jj, 0);
                    s.Stop();
                };
                s.Begin();

            }
            #endregion
            t.Stop();
            if (flag2 == 0)
            {
                flag2 = 1;
                if (Context.onlineUsers.Keys.Count.Equals(ddd.Count))
                {
                    foreach (Border b in ddd.Keys)
                    {
                        Label l = ddd[b];
                        l.Visibility = System.Windows.Visibility.Visible;
                        b.Visibility = System.Windows.Visibility.Visible;
                    }
                    goAni();
                    return;
                }
                foreach (Border b in ddd.Keys)
                {
                    Label l = ddd[b];
                    this.canvas.Children.Remove(l);
                    this.canvas.Children.Remove(b);

                }
                ddd = new Dictionary<Border, Label>();
                Dictionary<string, Dictionary<string, Uri>> dis = Context.onlineUsers;
                foreach (string hostname in dis.Keys)
                {
                    Dictionary<string, Uri> ccc = new Dictionary<string, Uri>();
                    string mininame = "";
                    BitmapImage bi = null;
                    if (Context.onlineUsers.TryGetValue(hostname, out ccc))
                    {
                        mininame = ccc.Keys.First();
                        Uri uri = ccc.Values.First();
                        bi = new BitmapImage(uri);

                    }
                    Border b = new Border();
                    b.BorderBrush = new SolidColorBrush(Colors.Black);
                    b.BorderThickness = new Thickness(8, 8, 8, 8);
                    b.CornerRadius = new CornerRadius(13);
                    b.Opacity = 0.5;
                    Image i = new Image();
                    i.Source = bi;
                    i.Width = 100;
                    i.Height = 100;
                    b.Child = i;

                    Label l = new Label();
                    l.Foreground = new SolidColorBrush(Colors.Pink);
                    l.Background = new SolidColorBrush(Colors.Transparent);
                    l.Content = mininame;
                    ddd.Add(b, l);
                    this.canvas.Children.Add(b);
                    this.canvas.Children.Add(l);
                    b.MouseLeftButtonDown += delegate
                    {
                        if (Chun.Client.Client.talks.ContainsKey(hostname))
                        {
                            Chun.Client.Client.talks[hostname].Visibility = System.Windows.Visibility.Visible;
                        }
                        else
                        {
                            Chun.Client.Client.talks.Add(hostname, new TalkWindow(hostname, client));
                            Chun.Client.Client.talks[hostname].Visibility = System.Windows.Visibility.Visible;
                        }
                    };
                }
                goAni();
            }
            else
            {
                
                
            }
            
        }

        private void goAni()
        {
            foreach(Border b in ddd.Keys)
            {
                Label l = ddd[b];

                double hei = (h-150) * r.NextDouble();
                double wid = (w-100) * r.NextDouble();
                DoubleAnimation da1 = new DoubleAnimation(Canvas.GetLeft(jj), wid , TimeSpan.FromSeconds(0.5));
                Storyboard.SetTarget(da1, b);
                Storyboard.SetTargetProperty(da1, new PropertyPath(Canvas.LeftProperty));
                DoubleAnimation da2 = new DoubleAnimation(Canvas.GetTop(jj), hei, TimeSpan.FromSeconds(0.5));
                Storyboard.SetTarget(da2, b);
                Storyboard.SetTargetProperty(da2, new PropertyPath(Canvas.TopProperty));
                DoubleAnimation da3 = new DoubleAnimation(Canvas.GetLeft(jj), wid, TimeSpan.FromSeconds(0.5));
                Storyboard.SetTarget(da3, l);
                Storyboard.SetTargetProperty(da3, new PropertyPath(Canvas.LeftProperty));
                DoubleAnimation da4 = new DoubleAnimation(Canvas.GetTop(jj), hei + 100, TimeSpan.FromSeconds(0.5));
                Storyboard.SetTarget(da4, l);
                Storyboard.SetTargetProperty(da4, new PropertyPath(Canvas.TopProperty));
                s1.Children.Add(da1);
                s1.Children.Add(da2);
                s1.Children.Add(da3);
                s1.Children.Add(da4);
                s1.Begin();

            }
            
            
        }

        

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            HotKey hotKey = new HotKey(this, HotKey.KeyFlags.MOD_SHIFT,System.Windows.Forms.Keys.Space);
            hotKey.OnHotKey += new HotKey.OnHotKeyEventHandler(hotKey_OnHotKey);

        }
        int flag = 0;
        void hotKey_OnHotKey()
        {
            if (flag == 0)
            {
                storyboard.Begin();
                this.Activate();
                this.input.Focus();
                inputBorder.BorderBrush = new SolidColorBrush(Colors.Black);
                input.Background = new SolidColorBrush(Colors.Orange);
                flag = 1;
                
            }
            else
            {
                inputBorder.BorderBrush = new SolidColorBrush(Colors.Transparent);
                this.input.Background = new SolidColorBrush(Colors.Transparent);
                flag = 0;
            }
        }
        int LastTime=0;
        string LastMessage=string.Empty;
        public int interval = 0 ;
        void input_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key==Key.RightCtrl)
            {
                this.input.Text = LastMessage;
            }
            if (e.Key == Key.Enter)
            {
                if (e.Timestamp - LastTime < interval)
                {
                    //MessageBox.Show(interval / 1000 + "秒间隔");
                    return;
                }
                LastTime = e.Timestamp;
                LastMessage = this.input.Text;
                if(String.IsNullOrWhiteSpace(LastMessage))
                {
                    return;
                }
                client.sendMsg(this.input.Text);
                this.input.Clear();
            }
        }

      

        bool isDragDropInEffect = false;
        Point pos = new Point();

        void Element_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragDropInEffect)
            {
                FrameworkElement currEle = sender as FrameworkElement;
                double xPos = e.GetPosition(null).X - pos.X + (double)currEle.GetValue(Canvas.LeftProperty);
                double yPos = e.GetPosition(null).Y - pos.Y + (double)currEle.GetValue(Canvas.TopProperty);
                currEle.SetValue(Canvas.LeftProperty, xPos);
                currEle.SetValue(Canvas.TopProperty, yPos);
                pos = e.GetPosition(null);
            }
        }

        void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            FrameworkElement fEle = sender as FrameworkElement;
            isDragDropInEffect = true;
            pos = e.GetPosition(null);
            fEle.CaptureMouse();
            fEle.Cursor = Cursors.Hand;
        }

        void Element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isDragDropInEffect)
            {
                FrameworkElement ele = sender as FrameworkElement;
                isDragDropInEffect = false;
                ele.ReleaseMouseCapture();
            }
            DoubleAnimation da;
            Storyboard s = new Storyboard();
            
            if (Canvas.GetLeft(jj) >= SystemParameters.FullPrimaryScreenWidth-50)
            {
                da = new DoubleAnimation(Canvas.GetLeft(jj), SystemParameters.FullPrimaryScreenWidth, TimeSpan.FromSeconds(0.5));
                
                Storyboard.SetTarget(da, jj);
                Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.LeftProperty));
                s.Children.Add(da);
                s.Completed += delegate
                {
                    Canvas.SetLeft(jj, SystemParameters.FullPrimaryScreenWidth);
                    s.Stop();
                };
                s.Begin();
                
            }
            if (Canvas.GetLeft(jj) <= 0)
            {
                da = new DoubleAnimation(Canvas.GetLeft(jj), -50, TimeSpan.FromSeconds(0.5));
              
                Storyboard.SetTarget(da, jj);
                Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.LeftProperty));
                s.Children.Add(da);
                s.Completed += delegate
                {
                    Canvas.SetLeft(jj, - 50);
                    s.Stop();
                };
                s.Begin();

            }
            if (Canvas.GetTop(jj) <= 0)
            {
                da = new DoubleAnimation(Canvas.GetTop(jj), -50, TimeSpan.FromSeconds(0.5));
                
                Storyboard.SetTarget(da, jj);
                Storyboard.SetTargetProperty(da, new PropertyPath(Canvas.TopProperty));
                s.Children.Add(da);
                s.Completed += delegate
                {
                    Canvas.SetTop(jj, -50);
                    s.Stop();
                };
                s.Begin();

            }

        }

       
    }
}



<Window x:Class="chunClient.TalkWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Background="#00FFFFFF"  AllowsTransparency="True" WindowStyle="None" WindowState="Maximized" ShowInTaskbar="True" Topmost="False" Width="436.657">

    <Canvas Name="canvas">
        <Border Name="inputBorder" BorderBrush="Transparent" BorderThickness="8,8,8,8" Canvas.Left="134" Opacity="0.7" Canvas.Top="462" CornerRadius="20" Height="63">
            <TextBox Name="input" Height="45"  Width="389" HorizontalAlignment="Center" VerticalAlignment="Bottom"  BorderThickness="0,0,0,0" FontSize="20" >
            </TextBox>
        </Border>

    </Canvas>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace chunClient
{
    /// <summary>
    /// TalkWindow.xaml 的交互逻辑
    /// </summary>
    public partial class TalkWindow : Window
    {
        public BitmapImage bi = null;
        public BitmapImage mbi = null;
        public Dictionary<string, Uri> ccc;
        private string hostname;
        public TalkWindow(string hostname, Chun.Client.Client client)
        {
            this.hostname = hostname;
            InitializeComponent();
            foreach (UIElement uiEle in this.canvas.Children)
            {
                //WPF设计上的问题,Button.Clicked事件Supress掉了Mouse.MouseLeftButtonDown附加事件等.
                //不加这个Button、TextBox等无法拖动
                if (uiEle is Button || uiEle is TextBox)
                {
                    uiEle.AddHandler(Button.MouseLeftButtonDownEvent, new MouseButtonEventHandler(Element_MouseLeftButtonDown), true);
                    uiEle.AddHandler(Button.MouseMoveEvent, new MouseEventHandler(Element_MouseMove), true);
                    uiEle.AddHandler(Button.MouseLeftButtonUpEvent, new MouseButtonEventHandler(Element_MouseLeftButtonUp), true);
                    continue;
                }
                //
                uiEle.MouseMove += new MouseEventHandler(Element_MouseMove);
                uiEle.MouseLeftButtonDown += new MouseButtonEventHandler(Element_MouseLeftButtonDown);
                uiEle.MouseLeftButtonUp += new MouseButtonEventHandler(Element_MouseLeftButtonUp);
            }         
            string mininame = hostname;
            this.client = client;
            Uri uri1 = Context.onlineUsers[Dns.GetHostName()].Values.First();
            mbi = new BitmapImage(uri1);
            if (Context.onlineUsers.TryGetValue(hostname, out ccc))
            {
                mininame = ccc.Keys.First();
                Uri uri = ccc.Values.First();
                bi = new BitmapImage(uri);

            }
            else
            {
                MessageBox.Show("该用户不在线");
            }
            this.Title = mininame;
            inputBorder.BorderBrush = new SolidColorBrush(Colors.Black);
            input.Background = new SolidColorBrush(Colors.Orange);
            this.input.KeyDown += input_KeyDown;
            this.input.Opacity = 0.7;
            
        }

      
        public void recieveMsg(string msg)
        {
            new Chun.Animation.TalkAnimation(bi,this.canvas, this.inputBorder, msg, this.Width);
        }
        int LastTime = 0;
        string LastMessage = string.Empty;
        private Chun.Client.Client client;
        void input_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.RightCtrl)
            {
                this.input.Text = LastMessage;
            }
            if (e.Key == Key.Enter)
            {
                
                LastTime = e.Timestamp;
                LastMessage = this.input.Text;
                if (String.IsNullOrWhiteSpace(LastMessage))
                {
                    return;
                }
                new Chun.Animation.TalkAnimation(mbi, this.canvas, this.inputBorder, this.input.Text, this.Width, 1);
                client.sendSMsg(this.input.Text, hostname);
               
                this.input.Clear();
            }
        }
        bool isDragDropInEffect = false;
        Point pos = new Point();

        void Element_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragDropInEffect)
            {
                FrameworkElement currEle = sender as FrameworkElement;
                double xPos = e.GetPosition(null).X - pos.X + (double)currEle.GetValue(Canvas.LeftProperty);
                double yPos = e.GetPosition(null).Y - pos.Y + (double)currEle.GetValue(Canvas.TopProperty);
                currEle.SetValue(Canvas.LeftProperty, xPos);
                currEle.SetValue(Canvas.TopProperty, yPos);
                pos = e.GetPosition(null);
            }
        }

        void Element_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            FrameworkElement fEle = sender as FrameworkElement;
            isDragDropInEffect = true;
            pos = e.GetPosition(null);
            fEle.CaptureMouse();
            fEle.Cursor = Cursors.Hand;
        }

        void Element_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isDragDropInEffect)
            {
                FrameworkElement ele = sender as FrameworkElement;
                isDragDropInEffect = false;
                ele.ReleaseMouseCapture();
            }
            

        }

    }
}



0 0
原创粉丝点击