by Kunal Chowdhury via Kunal's Blog on 6/18/2010 1:24:00 PM
Microsoft released Windows 7 last year which has lots of functionalities including a nice UI. Among those features one of the most user friendly feature is Pin/Unpin application to Taskbar. You can develop WPF or Windows Forms applications in which you can implement this feature. But do you know that this is also possible in Silverlight? Yes, Silverlight 4 has the power to talk with any other application using the COM API. You can read one of my earlier article [Silverlight 4: Interoperability with Excel using the COM Object]. In this Article I will show you how we can talk with the Windows 7 Taskbar. Read the complete Article and provide your feedbacks or suggestions.
• Windows 7 Operating System (must to run the application) • Visual Studio 2010 • Silverlight 4 Tools for Visual Studio 2010
<UserControl x:Class="Silverlight4.Interoperability.Taskbar.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Border BorderBrush="Black" Height="177" Width="600"> <Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="31" /> <RowDefinition /> </Grid.RowDefinitions> <Border BorderBrush="Black" CornerRadius="10,10,0,0"> <Border.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="Black" /> <GradientStop Color="Black" Offset="1" /> </LinearGradientBrush> </Border.Background> </Border> <Border BorderBrush="Black" CornerRadius="10,10,0,0" MouseLeftButtonDown="Border_MouseLeftButtonDown" Margin="2,2,2,0"> <Border.Background> <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> <GradientStop Color="White" /> <GradientStop Color="Transparent" Offset="0.69" /> </LinearGradientBrush> </Border.Background> <Border x:Name="brdClose" BorderBrush="#FFE9D710" BorderThickness="1" HorizontalAlignment="Right" MouseLeftButtonDown="brdClose_MouseLeftButtonDown" Margin="0,5,8,6" Width="22" Height="18" Background="#FF700C0C"> <TextBlock TextWrapping="Wrap" Text="X" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="13.333" Foreground="White" IsHitTestVisible="False" /> </Border> </Border> <Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" /> <TextBlock HorizontalAlignment="Left" Margin="9,7,0,8" TextWrapping="Wrap" Text="Windows 7 Taskbar Demo" IsHitTestVisible="False" d:LayoutOverrides="Height" FontWeight="Bold" FontSize="13.333" Foreground="White" /> <TextBox x:Name="txtWordpad" Height="22" Margin="8,8,91,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" IsReadOnly="True" Background="White" /> <Button x:Name="btnWordpad" Content="Pin/Unpin" Height="25" Click="btnWordpad_Click" Grid.Row="1" Width="80" Margin="0,6,6,0" HorizontalAlignment="Right" VerticalAlignment="Top" d:LayoutOverrides="Width, Height" Foreground="White" /> <TextBox x:Name="txtPaint" Height="22" Margin="8,36,91,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" IsReadOnly="True" Background="White" /> <Button x:Name="btnPaint" Content="Pin/Unpin" Height="25" Click="btnPaint_Click" Grid.Row="1" Width="80" Margin="0,34,6,0" HorizontalAlignment="Right" VerticalAlignment="Top" d:LayoutOverrides="Width, Height" Foreground="White" /> <TextBox x:Name="txtCalculator" Margin="8,66,91,58" Grid.Row="1" TextWrapping="Wrap" IsReadOnly="True" Background="White" /> <Button x:Name="btnCalculator" Content="Pin/Unpin" Height="25" Click="btnCalculator_Click" Grid.Row="1" Width="80" Margin="0,64,6,57" HorizontalAlignment="Right" Foreground="White" d:LayoutOverrides="Width" /> <TextBlock x:Name="txbMessage" Margin="8,0,8,22" Grid.Row="1" TextWrapping="Wrap" FontWeight="Bold" Foreground="Red" VerticalAlignment="Bottom" TextAlignment="Center" FontSize="12" /> </Grid> </Border> </UserControl>
GetFolderPath()
AutomationFactory
NameSpace
commonPrograms
commonPrograms.Self.Path
private string GetFolderPath() { using (dynamic shellApplication = AutomationFactory.CreateObject("Shell.Application")) { // get the common programs folder using the namespace dynamic commonPrograms = shellApplication.NameSpace(23); // commonPrograms.Self.Path will return => // "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs" string folderPath = string.Format("{0}\\{1}", commonPrograms.Self.Path, "Accessories"); return folderPath; } }
void MainPage_Loaded(object sender, RoutedEventArgs e) { txtWordpad.Text = string.Format(@"{0}\{1}.lnk", GetFolderPath(), "Wordpad"); txtPaint.Text = string.Format(@"{0}\{1}.lnk", GetFolderPath(), "Paint"); txtCalculator.Text = string.Format(@"{0}\{1}.lnk", GetFolderPath(), "Calculator"); }
private void PinToTaskbar(string fileName) { // create the shell application object using (dynamic shellApplication = AutomationFactory.CreateObject("Shell.Application")) { // get the accessories directory dynamic directory = shellApplication.NameSpace(GetFolderPath()); // get the full application path dynamic link = directory.ParseName(fileName + ".lnk"); dynamic verbs = link.Verbs(); for (int i = 0; i < verbs.Count(); i++) { dynamic verb = verbs.Item(i); string verbName = verb.Name.Replace(@"&", string.Empty).ToLower(); if (verbName.Equals("pin to taskbar")) { verb.DoIt(); ShowMessage(fileName + " has been pinned to your taskbar."); } else if (verbName.Equals("unpin from taskbar")) { verb.DoIt(); ShowMessage(fileName + " has been unpinned from your taskbar."); } } } }
Verbs()
dynamic verb = verbs.Item(i); string verbName = verb.Name.Replace(@"&", string.Empty).ToLower();
verb.DoIt()
Original Post: Silverlight 4: Interoperability with Windows 7 Taskbar using COM
The content of the postings is owned by the respective author. Silverlight Feeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on Silverlight Feeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.