Add MenuItem to Menu

image_pdfimage_print


   
 


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AppWindowsSample" Height="200" Width="300" 
    xmlns:my="clr-namespace:System;assembly=mscorlib">
  <StackPanel>
    <Menu Name="menu1" >
      <MenuItem Header="Window" x:Name="windowMenu">
        <MenuItem Header="SubmenuOpened event" />
      </MenuItem>
    </Menu>
    <Button Margin="10" Name="newWindowButton" Height="23" VerticalAlignment="Top">New Window</Button>
    <StatusBar Grid.Row="2" Name="statusBar1" >
      <TextBlock x:Name="statusText">Status</TextBlock>
    </StatusBar>
    <Button Height="23" Margin="10" Name="shutdownButton" VerticalAlignment="Top" >Shutdown</Button>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
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;
using System.Windows.Controls.Primitives;
using System.Diagnostics;
using System.ComponentModel;


namespace WpfApplication1 {
  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();

      DataContext = Application.Current.Windows;
      statusText.Text = Application.Current.ShutdownMode.ToString();

      newWindowButton.Click += new RoutedEventHandler(newWindowButton_Click);
      shutdownButton.Click += new RoutedEventHandler(shutdownButton_Click);
      windowMenu.SubmenuOpened += new RoutedEventHandler(windowMenu_SubmenuOpened);
      Closing += Window1_Closing;
    }

    public void windowMenu_SubmenuOpened(object sender, RoutedEventArgs e) {
      windowMenu.Items.Clear();
      foreach( Window window in Application.Current.Windows ) {
        MenuItem item = new MenuItem();
        item.Header = window.Title;
        item.Click += windowMenuItem_Click;
        item.Tag = window;
        item.IsChecked = window.IsActive;
        windowMenu.Items.Add(item);
      }
    }

    public void windowMenuItem_Click(object sender, RoutedEventArgs e) {
      Window window = (Window)((MenuItem)sender).Tag;
      window.Activate();
    }

    public void newWindowButton_Click(object sender, RoutedEventArgs e) {
      Window window = new Window();
      window.Title = "Window " + DateTime.Now;
      window.Width = 200;
      window.Height = 100;
      window.Show();
      statusText.Text = Application.Current.ShutdownMode.ToString();
    }

    void shutdownButton_Click(object sender, RoutedEventArgs e) {
      Application.Current.Shutdown();
    }

    void Window1_Closing(object sender, CancelEventArgs e) {
      if( MessageBox.Show("Do you really want to shut down?", "Shutting Down", MessageBoxButton.YesNo) == MessageBoxResult.No ) {
        e.Cancel = true;
      }
    }


  }
}

   
     


MenuItem opened event

image_pdfimage_print


   
 


<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AppWindowsSample" Height="200" Width="300" 
    xmlns:my="clr-namespace:System;assembly=mscorlib">
  <StackPanel>
    <Menu Name="menu1" >
      <MenuItem Header="Window" x:Name="windowMenu">
        <MenuItem Header="SubmenuOpened event" />
      </MenuItem>
    </Menu>
    <Button Margin="10" Name="newWindowButton" Height="23" VerticalAlignment="Top">New Window</Button>
    <StatusBar Grid.Row="2" Name="statusBar1" >
      <TextBlock x:Name="statusText">Status</TextBlock>
    </StatusBar>
    <Button Height="23" Margin="10" Name="shutdownButton" VerticalAlignment="Top" >Shutdown</Button>
  </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
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;
using System.Windows.Controls.Primitives;
using System.Diagnostics;
using System.ComponentModel;


namespace WpfApplication1 {
  public partial class Window1 : System.Windows.Window {

    public Window1() {
      InitializeComponent();

      DataContext = Application.Current.Windows;
      statusText.Text = Application.Current.ShutdownMode.ToString();

      newWindowButton.Click += new RoutedEventHandler(newWindowButton_Click);
      shutdownButton.Click += new RoutedEventHandler(shutdownButton_Click);
      windowMenu.SubmenuOpened += new RoutedEventHandler(windowMenu_SubmenuOpened);
      Closing += Window1_Closing;
    }

    public void windowMenu_SubmenuOpened(object sender, RoutedEventArgs e) {
      windowMenu.Items.Clear();
      foreach( Window window in Application.Current.Windows ) {
        MenuItem item = new MenuItem();
        item.Header = window.Title;
        item.Click += windowMenuItem_Click;
        item.Tag = window;
        item.IsChecked = window.IsActive;
        windowMenu.Items.Add(item);
      }
    }

    public void windowMenuItem_Click(object sender, RoutedEventArgs e) {
      Window window = (Window)((MenuItem)sender).Tag;
      window.Activate();
    }

    public void newWindowButton_Click(object sender, RoutedEventArgs e) {
      Window window = new Window();
      window.Title = "Window " + DateTime.Now;
      window.Width = 200;
      window.Height = 100;
      window.Show();
      statusText.Text = Application.Current.ShutdownMode.ToString();
    }

    void shutdownButton_Click(object sender, RoutedEventArgs e) {
      Application.Current.Shutdown();
    }

    void Window1_Closing(object sender, CancelEventArgs e) {
      if( MessageBox.Show("Do you really want to shut down?", "Shutting Down", MessageBoxButton.YesNo) == MessageBoxResult.No ) {
        e.Cancel = true;
      }
    }


  }
}

   
     


Sound And Video Playback in Code

image_pdfimage_print


   
  

<Window x:Class="SoundAndVideo.CodePlayback"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="CodePlayback" Height="300" Width="300"
    xmlns:local="clr-namespace:SoundAndVideo">
  <Window.Resources>
    <local:TimeSpanConverter x:Key="TimeSpanConverter"></local:TimeSpanConverter>
  </Window.Resources>
    <StackPanel Orientation="Horizontal">      
      <MediaElement Name="media" LoadedBehavior="Manual" UnloadedBehavior="Close" Source="test.mp3" MediaOpened="media_MediaOpened"></MediaElement>

      <Button Click="cmdPlay_Click">Play</Button>
      <Button Click="cmdStop_Click">Stop</Button>
      <Button Click="cmdPause_Click">Pause</Button>      
      <TextBlock>Volume:</TextBlock>
      <Slider Minimum="0" Maximum="1" Value="{Binding ElementName=media, Path=Volume, Mode=TwoWay}"></Slider>
      <TextBlock>Balance: </TextBlock>
      <Slider Minimum="-1" Maximum="1" Value="{Binding ElementName=media, Path=Balance, Mode=TwoWay}"></Slider>
      <TextBlock>Speed: </TextBlock>
      <Slider Minimum="0" Maximum="2" Value="{Binding ElementName=media, Path=SpeedRatio}"></Slider>
      <TextBlock>Seek To: </TextBlock>
      <Slider Minimum="0" Name="sliderPosition" TickPlacement="BottomRight" TickFrequency="1"
               ValueChanged="sliderPosition_ValueChanged"></Slider>
      <TextBlock Name="lblTime"></TextBlock>
  </StackPanel>

</Window>

//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
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;
using System.Windows.Media.Animation;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
namespace SoundAndVideo
{
    public partial class CodePlayback : System.Windows.Window
    {
        public CodePlayback()
        {
            InitializeComponent();
        }
        private void sliderSpeed_ValueChanged(object sender, RoutedEventArgs e)
        {
             media.SpeedRatio = ((Slider)sender).Value;
        }

        private void cmdPlay_Click(object sender, RoutedEventArgs e)
        {
            media.Play();
        }
        private void cmdPause_Click(object sender, RoutedEventArgs e)
        {
            media.Pause();
        }
        private void cmdStop_Click(object sender, RoutedEventArgs e)
        {            
            media.Stop();
            media.SpeedRatio = 1;
        }
        private void media_MediaOpened(object sender, RoutedEventArgs e)
        {
            sliderPosition.Maximum = media.NaturalDuration.TimeSpan.TotalSeconds;
        }
        private void sliderPosition_ValueChanged(object sender, RoutedEventArgs e)
        {
            media.Pause();
            media.Position = TimeSpan.FromSeconds(sliderPosition.Value);
            media.Play();
        }
    }
    public class TimeSpanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
        {
            try
            {
                TimeSpan time = (TimeSpan)value;
                return time.TotalSeconds;
            }
            catch
            {
                return 0;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
        {
            double seconds = (double)value;
            return TimeSpan.FromSeconds(seconds);
        }
    }

}

   
    
     


Play with MediaPlayer

image_pdfimage_print


   
  
<Window x:Class="SoundAndVideo.SoundPlayerTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SoundPlayerTest" Height="300" Width="300" Closed="window_Closed">
    <StackPanel>
          <Button Click="cmdPlayWithMediaPlayer_Click">Play with MediaPlayer</Button>
    </StackPanel>
</Window>

//File:Window.xaml.cs
using System;
using System.Collections.Generic;
using System.Text;
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;
using System.Media;
using System.ComponentModel;
using System.IO;

namespace SoundAndVideo
{
    public partial class SoundPlayerTest : System.Windows.Window
    {
        MediaPlayer player = new MediaPlayer();

        public SoundPlayerTest()
        {
            InitializeComponent();
        }
        private void cmdPlayWithMediaPlayer_Click(object sender, RoutedEventArgs e)
        {            
            player.Open(new Uri("test.mp3", UriKind.Relative));
            player.Play();
        }

        private void window_Closed(object sender, EventArgs e)
        {
            player.Close();
        }
    }
}

   
    
     


Play Audio Asynchronously

image_pdfimage_print


   
  
<Window x:Class="SoundAndVideo.SoundPlayerTest"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="SoundPlayerTest" Height="300" Width="300" Closed="window_Closed">
    <StackPanel>
       <Button Click="cmdPlayAudioAsync_Click">Play Audio Asynchronously</Button>
    </StackPanel>
</Window>
//File:Window.xaml.cs

using System;
using System.Collections.Generic;
using System.Text;
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;
using System.Media;
using System.ComponentModel;
using System.IO;

namespace SoundAndVideo
{
    public partial class SoundPlayerTest : System.Windows.Window
    {
        MediaPlayer player = new MediaPlayer();

        public SoundPlayerTest()
        {
            InitializeComponent();
        }
        private void cmdPlayAudioAsync_Click(object sender, RoutedEventArgs e)
        {
            SoundPlayer player = new SoundPlayer();
            player.SoundLocation = "test.wav";
            player.Load();
            player.Play();
        }

        private void window_Closed(object sender, EventArgs e)
        {
            player.Close();
        }
    }
}

   
    
     


Play System Sounds

image_pdfimage_print


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF" Height="120" Width="300">
    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="{x:Type Button}">
                <EventSetter Event="Click" Handler="Button_Click" />
            </Style>
        </StackPanel.Resources>
        <Button Content="Asterisk" Name="btnAsterisk" />
        <Button Content="Beep" Name="btnBeep" />
        <Button Content="Exclamation" Name="btnExclamation" />
        <Button Content="Hand" Name="btnHand" />
        <Button Content="Question" Name="btnQuestion" />
    </StackPanel>
</Window>
//File:Window.xaml.cs

using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;

            if (btn != null)
            {
                switch (btn.Content.ToString())
                {
                    case "Asterisk":
                        System.Media.SystemSounds.Asterisk.Play();
                        break;
                    case "Beep":
                        System.Media.SystemSounds.Beep.Play();
                        break;
                    case "Exclamation":
                        System.Media.SystemSounds.Exclamation.Play();
                        break;
                    case "Hand":
                        System.Media.SystemSounds.Hand.Play();
                        break;
                    case "Question":
                        System.Media.SystemSounds.Question.Play();
                        break;
                    default:
                        string msg = "Sound not implemented: " + btn.Content;
                        MessageBox.Show(msg);
                        break;
                }
            }
        }
    }
}

   
    
     


File menu

image_pdfimage_print


   
    


<Window x:Class="ControlDemos.menu"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="ControlDemos" Height="300" Width="300">
    <Grid>
      <Menu Width="30" Margin="10, 10, 5, 5" HorizontalAlignment="Left" Background="White">
        <MenuItem Header="_File">
          <MenuItem Header="_New" IsCheckable="true"/>
          <MenuItem Header="_Open" IsCheckable="true"/>
          <MenuItem Header="_Close" IsCheckable="true"/>
          <Separator/>
          <MenuItem Header="Open Previous">
            <MenuItem Header="Word Documents" />
            <MenuItem Header="Source Code" >
              <MenuItem Header="C# Files" />
            </MenuItem>
          </MenuItem>
          <Separator/>
          <MenuItem Header="E_xit">
          </MenuItem>
        </MenuItem>
      </Menu>

  </Grid>
</Window>