Example #1
0
        //private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        //{

        //}

        void ToListBox()
        {
            for (; i < TextToListBox.Length; i++)
            {
                //
                SP.Add(new StackPanel());
                Ava.Add(new Image());
                Msg.Add(new TextBlock());
                time.Add(new System.Windows.Controls.Label());
                int LI = TextToListBox[i].LastIndexOf("&");
                int IO = TextToListBox[i].IndexOf("&");

                // System.Windows.MessageBox.Show(Convert.ToString(TextToListBox[i].Length), TextToListBox[i][TextToListBox[i].Length-1]+"a");
                string GetTime = TextToListBox[i].Substring(LI + 1, TextToListBox[i].Length - (LI + 1));//время и дата

                bool   hasFile;
                string GetId;
                if (TextToListBox[i].IndexOf("|") == -1)
                {
                    hasFile = false;
                }
                else
                {
                    hasFile = true;
                }

                string extend = null;
                Guid   StrmID = new Guid();
                if (!hasFile)
                {
                    GetId = TextToListBox[i].Substring(0, IO);
                }
                else
                {
                    GetId = TextToListBox[i].Substring(TextToListBox[i].IndexOf("|") + 1, IO - (TextToListBox[i].IndexOf("|") + 1));
                    //-узнаем расширение, чтобы если этот файл окажется изображением, музыкой или видео прогрузить соответствующую пиктограмму
                    extend = TextToListBox[i].Substring(TextToListBox[i].IndexOf("&"), TextToListBox[i].LastIndexOf("&") - TextToListBox[i].IndexOf("|"));
                    extend = extend.Substring(extend.LastIndexOf("."), (extend.LastIndexOf("&")) - (extend.LastIndexOf(".")));
                    StrmID = new Guid(TextToListBox[i].Substring(0, TextToListBox[i].IndexOf("|")));
                    //
                }
                time[i].Content  = GetTime;
                TextToListBox[i] = TextToListBox[i].Remove(TextToListBox[i].LastIndexOf('&'));
                TextToListBox[i] = TextToListBox[i].Substring(TextToListBox[i].IndexOf('&') + 1);//идентификатор-аватарка

                Msg[i].Text = " " + TextToListBox[i];
                try
                {
                    image1.Add(new BitmapImage(new Uri(sql.GetData(Convert.ToInt32(GetId))[5])));
                }
                catch (Exception)
                {
                    image1.Add(new BitmapImage(new Uri(@"file:///D:/Desktop/WPFMessanger/WpfApplication3/Resources/Empty.png")));
                }
                Ava[i].Source = image1[i];

                SP[i].Orientation   = System.Windows.Controls.Orientation.Horizontal;
                Ava[i].Height       = Double.NaN;
                Ava[i].Width        = 50;
                Msg[i].Width        = 260;
                Msg[i].Height       = Double.NaN;
                Msg[i].TextWrapping = TextWrapping.Wrap;
                //time[i].Foreground = new SolidColorBrush(Colors.Red);
                time[i].Foreground = Brushes.Gray;
                //time[i].HorizontalAlignment = Left;
                //time[i].Color = "grey";
                //


                SP[i].Children.Add(Ava[i]);
                if (hasFile == true)
                {
                    FilePic.Add(new Image());
                    MediaTimer.Add(new System.Windows.Forms.Timer());

                    if (extend == ".jpg" || extend == ".png" || extend == ".bmp" || extend == ".jepg")
                    {
                        try
                        {
                            System.IO.MemoryStream stream = new System.IO.MemoryStream(sql.GetPicture(StrmID));
                            var image = new BitmapImage();
                            image.BeginInit();
                            image.StreamSource = stream;
                            image.EndInit();
                            FilePic[j].Source = image;
                        }
                        catch (System.ArgumentNullException)
                        {
                            FilePic[j].Source = new BitmapImage(new Uri(@"file:///D:/Desktop/WPFMessanger/WpfApplication3/Resources/Null.png"));
                        }

                        //GIF не работает :с
                        //FilePic[i].SetAnimatedSource(img, image);
                        //ImageBehavior.SetRepeatBehavior(img, new RepeatBehavior(0));
                        //ImageBehavior.SetRepeatBehavior(img, RepeatBehavior.Forever);
                        FilePic[j].Height = Double.NaN;
                        FilePic[j].Width  = 260;
                        Msg[i].Text       = " ";
                        Msg[i].Width      = 0;
                        FilePic[j].Margin = new Thickness(10, 0, 0, 0);
                    }
                    else if (extend == ".mp3" || extend == ".m4a" || extend == ".aud" || extend == ".aif" || extend == ".amr" || extend == ".wma" || extend == ".wav" || extend == ".mid")
                    {
                        try
                        {
                            MediaTimer[mediaIndex].Interval = 400;
                            MediaTimer[mediaIndex].Tick    += timer_Tick;
                            TimerState.Add(false);
                            state.Add(false);

                            media.Add(new MediaElement());
                            PauseResume.Add(new System.Windows.Controls.Button());
                            Restart.Add(new System.Windows.Controls.Button());
                            rewind.Add(new Slider());
                            mediaPanel.Add(new StackPanel());

                            media[mediaIndex].MediaOpened += new RoutedEventHandler(media_MediaOpened);
                            //media[mediaIndex].MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(media_MediaFailed);


                            System.IO.MemoryStream stream = new System.IO.MemoryStream(sql.GetPicture(StrmID));
                            FilePic[j].Source = new BitmapImage(new Uri("Resources/music.png", UriKind.RelativeOrAbsolute));;

                            System.IO.File.WriteAllBytes("temp/temp" + mediaIndex + extend, sql.GetPicture(StrmID));
                            media[mediaIndex].Source           = (new Uri(AppDomain.CurrentDomain.BaseDirectory + "temp/temp" + mediaIndex + extend));
                            media[mediaIndex].LoadedBehavior   = MediaState.Manual;
                            media[mediaIndex].UnloadedBehavior = MediaState.Close;
                            media[mediaIndex].Play();
                            media[mediaIndex].Stop();
                            // media.Pause();


                            media[mediaIndex].Name          = "Media_" + mediaIndex;
                            rewind[mediaIndex].Name         = "Slider_" + mediaIndex;
                            Restart[mediaIndex].Name        = "Restart_" + mediaIndex;
                            PauseResume[mediaIndex].Name    = "Pause_" + mediaIndex;
                            Restart[mediaIndex].Content     = "Restart";
                            PauseResume[mediaIndex].Content = "Pause";
                            Restart[mediaIndex].Click      += new RoutedEventHandler(ToRestart);
                            PauseResume[mediaIndex].Click  += new RoutedEventHandler(ToResumePause);

                            //Thumb thumb = this.GetTemplateChild(rewind[mediaIndex].Name) as Thumb;
                            //thumb.DragStarted += new DragStartedEventHandler(sld_DragStarted);
                            //thumb.DragCompleted += new DragCompletedEventHandler(sld_DragCompleted);
                            //rewind[mediaIndex].OnThumbDragStarted += new RoutedEventHandler(sld_DragStarted);
                            //rewind[mediaIndex].DragLeave += new System.Windows.RoutedEventHandler(sld_DragCompleted);
                            //rewind[mediaIndex].ValueChanged += new RoutedPropertyChangedEventHandler<double>(sld_ValueChanged);

                            media[mediaIndex].MediaOpened += new RoutedEventHandler(media_MediaOpened);

                            mediaPanel[mediaIndex].Children.Add(media[mediaIndex]);
                            mediaPanel[mediaIndex].Children.Add(rewind[mediaIndex]);
                            mediaPanel[mediaIndex].Children.Add(PauseResume[mediaIndex]);
                            mediaPanel[mediaIndex].Children.Add(Restart[mediaIndex]);


                            FilePic[j].Height = Double.NaN;
                            FilePic[j].Width  = 0;
                            Msg[i].Text       = " ";
                            Msg[i].Width      = 0;

                            //media.Width = 250;
                            //media.Height = Double.NaN;
                            mediaPanel[mediaIndex].Orientation = System.Windows.Controls.Orientation.Vertical;
                            PauseResume[mediaIndex].Width      = 50;
                            PauseResume[mediaIndex].Height     = Double.NaN;
                            Restart[mediaIndex].Width          = 50;
                            Restart[mediaIndex].Height         = Double.NaN;
                            mediaPanel[mediaIndex].Width       = 260;
                            mediaPanel[mediaIndex].Height      = Double.NaN;
                            mediaPanel[mediaIndex].Margin      = new Thickness(10, 0, 0, 0);
                            rewind[mediaIndex].Width           = 260;


                            FilePic[j].Height = 40;
                            FilePic[j].Width  = Double.NaN;
                            Msg[i].Width      = 220;
                            FilePic[j].Margin = new Thickness(10, 0, 0, 0);

                            //System.IO.MemoryStream stream = new System.IO.MemoryStream(sql.GetPicture(StrmID));
                            media[mediaIndex].Width      = 0;
                            rewind[mediaIndex].IsEnabled = false;

                            SP[i].Children.Add(mediaPanel[mediaIndex]);
                            mediaIndex++;
                        }
                        catch (System.ArgumentNullException)
                        {
                            FilePic[j].Source = new BitmapImage(new Uri(@"file:///D:/Desktop/WPFMessanger/WpfApplication3/Resources/Null.png"));
                            FilePic[j].Height = Double.NaN;
                            FilePic[j].Width  = 260;
                            Msg[i].Text       = " ";
                            Msg[i].Width      = 0;
                            FilePic[j].Margin = new Thickness(10, 0, 0, 0);
                        }
                    }
                    else if (extend == ".mp4" || extend == ".3gp" || extend == ".3g2" || extend == ".asf" || extend == ".asx" || extend == ".avi" || extend == ".flv" || extend == ".mpg" || extend == ".webm" || extend == ".wmv" || extend == ".gif")
                    {
                        MediaTimer[mediaIndex].Interval = 400;
                        MediaTimer[mediaIndex].Tick    += timer_Tick;
                        TimerState.Add(false);
                        state.Add(false);

                        media.Add(new MediaElement());
                        PauseResume.Add(new System.Windows.Controls.Button());
                        Restart.Add(new System.Windows.Controls.Button());
                        rewind.Add(new Slider());
                        mediaPanel.Add(new StackPanel());
                        try
                        {
                            //media[mediaIndex].MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(media_MediaFailed);

                            //System.IO.MemoryStream stream = new System.IO.MemoryStream(sql.GetPicture(StrmID));



                            System.IO.File.WriteAllBytes("temp/temp" + mediaIndex + extend, sql.GetPicture(StrmID));
                            //var mediaPlayer = new MediaPlayer();
                            //mediaPlayer.Open(new Uri(@"\\Desktop - 3e0rm1s\mssqlserver\ChatNetwork\SendFile\odessa.mp4", UriKind.RelativeOrAbsolute));
                            //mediaPlayer.Play();
                            media[mediaIndex].Source           = (new Uri(AppDomain.CurrentDomain.BaseDirectory + "temp/temp" + mediaIndex + extend));
                            media[mediaIndex].LoadedBehavior   = MediaState.Manual;
                            media[mediaIndex].UnloadedBehavior = MediaState.Close;
                            media[mediaIndex].ScrubbingEnabled = true;
                            media[mediaIndex].Play();
                            media[mediaIndex].Stop();
                            //media.Pause();4


                            media[mediaIndex].Name          = "Media_" + mediaIndex;
                            rewind[mediaIndex].Name         = "Slider_" + mediaIndex;
                            Restart[mediaIndex].Name        = "Restart_" + mediaIndex;
                            PauseResume[mediaIndex].Name    = "Pause_" + mediaIndex;
                            Restart[mediaIndex].Content     = "Restart";
                            PauseResume[mediaIndex].Content = "Pause";
                            Restart[mediaIndex].Click      += new RoutedEventHandler(ToRestart);
                            PauseResume[mediaIndex].Click  += new RoutedEventHandler(ToResumePause);

                            //rewind[mediaIndex].DragEnter += new System.Windows.DragEventHandler(sld_DragStarted);
                            //rewind[mediaIndex].DragLeave += new System.Windows.DragEventHandler(sld_DragCompleted);
                            //rewind[mediaIndex].ValueChanged += new RoutedPropertyChangedEventHandler<double>(sld_ValueChanged);

                            media[mediaIndex].MediaOpened += new RoutedEventHandler(media_MediaOpened);

                            mediaPanel[mediaIndex].Children.Add(media[mediaIndex]);
                            mediaPanel[mediaIndex].Children.Add(rewind[mediaIndex]);
                            mediaPanel[mediaIndex].Children.Add(PauseResume[mediaIndex]);
                            mediaPanel[mediaIndex].Children.Add(Restart[mediaIndex]);


                            FilePic[j].Height = Double.NaN;
                            FilePic[j].Width  = 0;
                            Msg[i].Text       = " ";
                            Msg[i].Width      = 0;

                            //media.Width = 250;
                            //media.Height = Double.NaN;
                            mediaPanel[mediaIndex].Orientation = System.Windows.Controls.Orientation.Vertical;
                            PauseResume[mediaIndex].Width      = 50;
                            PauseResume[mediaIndex].Height     = Double.NaN;
                            Restart[mediaIndex].Width          = 50;
                            Restart[mediaIndex].Height         = Double.NaN;
                            media[mediaIndex].Width            = 260;
                            media[mediaIndex].Height           = Double.NaN;
                            mediaPanel[mediaIndex].Width       = 260;
                            mediaPanel[mediaIndex].Height      = Double.NaN;
                            mediaPanel[mediaIndex].Margin      = new Thickness(10, 0, 0, 0);
                            rewind[mediaIndex].Width           = 260;
                            rewind[mediaIndex].IsEnabled       = false;

                            SP[i].Children.Add(mediaPanel[mediaIndex]);//media
                            mediaIndex++;
                        }
                        catch (System.ArgumentNullException)
                        {
                            FilePic[j].Source = new BitmapImage(new Uri(@"file:///D:/Desktop/WPFMessanger/WpfApplication3/Resources/Null.png"));
                            FilePic[j].Height = Double.NaN;
                            FilePic[j].Width  = 260;
                            Msg[i].Text       = " ";
                            Msg[i].Width      = 0;
                            FilePic[j].Margin = new Thickness(10, 0, 0, 0);
                        }
                    }

                    else
                    {
                        FilePic[j].Source = new BitmapImage(new Uri("Resources/file.png", UriKind.RelativeOrAbsolute));
                        FilePic[j].Height = 40;
                        FilePic[j].Width  = Double.NaN;
                        Msg[i].Width      = 210;
                        FilePic[j].Margin = new Thickness(10, 0, 0, 0);
                    }
                    SP[i].Children.Add(FilePic[j]);

                    j++;
                }
                SP[i].Children.Add(Msg[i]);
                SP[i].Children.Add(time[i]);
                LBDialog.Items.Add(SP[i]);

                //
                //LBDialog.Items.Add(TextToListBox[i]);



                //LBDialog.SelectedIndex = LBDialog.Items.Count - 1;//Проскролить в конец
                //LBDialog.ScrollIntoView(LBDialog.SelectedItem);
                LBDialog.ScrollIntoView(LBDialog.Items[LBDialog.Items.Count - 1]);
            }
        }
Example #2
0
        //private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        //{

        //}

        void ToListBox()
        {
            int j = 0;

            for (int i = 0; i < TextToListBox.Length; i++)
            {
                //
                SP.Add(new StackPanel());
                Ava.Add(new Image());
                Msg.Add(new TextBlock());
                time.Add(new System.Windows.Controls.Label());
                int LI = TextToListBox[i].LastIndexOf("&");
                int IO = TextToListBox[i].IndexOf("&");

                // System.Windows.MessageBox.Show(Convert.ToString(TextToListBox[i].Length), TextToListBox[i][TextToListBox[i].Length-1]+"a");
                string GetTime = TextToListBox[i].Substring(LI + 1, TextToListBox[i].Length - (LI + 1));//время и дата

                bool   hasFile;
                string GetId;
                if (TextToListBox[i].IndexOf("|") == -1)
                {
                    hasFile = false;
                }
                else
                {
                    hasFile = true;
                }

                if (!hasFile)
                {
                    GetId = TextToListBox[i].Substring(0, IO);
                }
                else
                {
                    GetId = TextToListBox[i].Substring(TextToListBox[i].IndexOf("|") + 1, IO - (TextToListBox[i].IndexOf("|") + 1));
                }
                //GetId = TextToListBox[i].Substring(0,TextToListBox[i].IndexOf("|"));


                time[i].Content  = GetTime;                                                       //DateTime.Now;
                TextToListBox[i] = TextToListBox[i].Remove(TextToListBox[i].LastIndexOf('&'));
                TextToListBox[i] = TextToListBox[i].Substring(TextToListBox[i].IndexOf('&') + 1); //идентификатор-аватарка

                Msg[i].Text = " " + TextToListBox[i];
                image1.Add(new BitmapImage(new Uri(sql.GetData(Convert.ToInt32(GetId))[5])));
                Ava[i].Source = image1[i];

                //


                //
                SP[i].Orientation   = System.Windows.Controls.Orientation.Horizontal;
                Ava[i].Height       = Double.NaN;
                Ava[i].Width        = 50;
                Ava[i].Margin       = new Thickness(0, 0, 0, 0);
                Msg[i].Width        = 250;
                Msg[i].Height       = Double.NaN;
                Msg[i].TextWrapping = TextWrapping.Wrap;
                //time[i].Foreground = new SolidColorBrush(Colors.Red);
                time[i].Foreground = Brushes.Gray;
                //time[i].HorizontalAlignment = Left;
                //time[i].Color = "grey";


                SP[i].Children.Add(Ava[i]);
                if (hasFile == true)
                {
                    FilePic.Add(new Image());
                    FilePic[j].Source = new BitmapImage(new Uri("Resources/file.png", UriKind.RelativeOrAbsolute));
                    FilePic[j].Height = 40;
                    FilePic[j].Width  = Double.NaN;
                    FilePic[j].Margin = new Thickness(0, 0, 0, 0);
                    SP[i].Children.Add(FilePic[j]);
                    j++;
                }
                SP[i].Children.Add(Msg[i]);
                SP[i].Children.Add(time[i]);
                LBDialog.Items.Add(SP[i]);

                //
                //LBDialog.Items.Add(TextToListBox[i]);



                //LBDialog.SelectedIndex = LBDialog.Items.Count - 1;//Проскролить в конец
                //LBDialog.ScrollIntoView(LBDialog.SelectedItem);
                LBDialog.ScrollIntoView(LBDialog.Items[LBDialog.Items.Count - 1]);
            }
        }