C#, WPF – Windows 7 Thumbnail Toolbar

During learning to develop myself a gadget for Windows 7, I would like to use some features of Windows 7 for my application. One of these features is enabling thumbnail of our application on toolbar as following

You can see in image above that I have a custom beautiful thumbnail with 4 buttons to navigate through the image gallery. To apply this feature in our application, I need a library called Microsoft API Code Pack http://code.msdn.microsoft.com/WindowsAPICodePack . In download package you can find a lot of examples relevant to many interesting features of Windows 7. The example below is extracted from this package but I made some modifications so that it works better.

1. Create a WPF application, add a StackPanel and 2 children: Image and ListBox control. The list box item template was also changed to contain an Image and a TextBlock. The TextBlock is set by the name of the image.

2. Add reference to Microsoft.WindowsAPICodePack.dll and Microsoft.WindowsAPICodePack.Shell.dll.
3. Test if I am running on Windows 7

public Window1()
{
	if (!TaskbarManager.IsPlatformSupported)
	{
		MessageBox.Show("This demo runs only on Windows 7");
		Application.Current.Shutdown();
	}
	InitializeComponent();
}

4. Add images to list box through data binding. The data was taken from ViewModel.

<ListBox x:Name="ImageList" ItemsSource="{Binding AllImages}" ItemTemplate="{DynamicResource ItemImageTemplate}" ItemsPanel="{DynamicResource PanelImageTemplate}" Height="167" />

5. Add 4 thumbnail toolbar button when window loads. These buttons will be used for navigating our gallery in thumbnail as shown in image above.

private void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
	// TODO: Add event handler implementation here.
	m_btnToolbarFist = new ThumbnailToolbarButton(Properties.Resources.FirstImage, "First Image");
	m_btnToolbarFist.Enabled = true;
	m_btnToolbarFist.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(m_btnToolbarFist_Click);

	m_btnToolbarPrev = new ThumbnailToolbarButton(Properties.Resources.PreviousImage, "Previous Image");
	m_btnToolbarPrev.Enabled = true;
	m_btnToolbarPrev.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(m_btnToolbarPrev_Click);

	m_btnToolbarNext = new ThumbnailToolbarButton(Properties.Resources.NextImage, "Next Image");
	m_btnToolbarNext.Enabled = true;
	m_btnToolbarNext.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(m_btnToolbarNext_Click);

	m_btnToolbarLast = new ThumbnailToolbarButton(Properties.Resources.LastImage, "Last Image");
	m_btnToolbarLast.Enabled = true;
	m_btnToolbarLast.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(m_btnToolbarLast_Click);

	TaskbarManager.Instance.ThumbnailToolbars.AddButtons(new WindowInteropHelper(this).Handle, new ThumbnailToolbarButton[] { m_btnToolbarFist, m_btnToolbarPrev, m_btnToolbarNext, m_btnToolbarLast });
}

6. Add some code lines for navigating images in list. For example, the code handle to navigate to the last image in list.

void m_btnToolbarLast_Click(object sender, ThumbnailButtonClickedEventArgs e)
{
	ImageList.SelectedIndex = ImageList.Items.Count - 1;
	ImageList.Focus();
	if (ImageList.SelectedItem != null)
		ImageList.ScrollIntoView(ImageList.SelectedItem);
}

7. I would like to use the current selected image as our thumbnail. Therefore let’s take a snapshot of Image control and set it to taskbar through SetThumbnailcClip function.

void PictureBox_LayoutUpdated(object sender, EventArgs e)
{
	Vector vtPict = VisualTreeHelper.GetOffset(PictureBox);
	TaskbarManager.Instance.TabbedThumbnail.SetThumbnailClip(new WindowInteropHelper(this).Handle, new System.Drawing.Rectangle((int)vtPict.X, (int)vtPict.Y, (int)PictureBox.RenderSize.Width, (int)PictureBox.RenderSize.Height));
}

What I am still concerning is try to build the program exactly according to MVVM pattern. The complete source code you can download “WPF Win7 Thumbnail Demo

C# – Adjust master volume in Vista and Windows 7

There are a lot of changes in Vista and Windows 7 in comparison with Windows XP, especially in programming field. Many code snippet which works perfectly, does not run under Vista/Windows 7 anymore, for example adjusting master volume. If you have already worked with master volume in Windows XP, maybe you use MixerControl for accessing system volume with following API.
– mixerClose
– mixerGetControlDetailsA
– mixerGetDevCapsA
– mixerGetID
– mixerGetLineControlsA
– mixerGetLineInfoA
– mixerGetNumDevs
– mixerMessage
– mixerOpen
– mixerSetControlDetails

Under Vista/Windows 7, we can not use this API anymore because volumes have been individualized in Vista which means for the most part an application changing the waveOut volume has no affect on other applications. In Vista/Windows 7, we must use another APIs which is COM based.
– MMDevice API: The Windows Multimedia Device (MMDevice) API enables audio clients to discover audio endpoint devices, determine their capabilities, and create driver instances for those devices.
– WASAPI: The Windows Audio Session API (WASAPI) enables client applications to manage the flow of audio data between the application and an audio endpoint device.
– DeviceTopology API: The DeviceTopology API provides client applications with the ability to traverse the functional hardware topologies of audio rendering and capture devices. Through the interfaces and methods in the DeviceTopology API, clients can discover the functional subunits (for example, volume control) that lie along the data paths that lead to and from audio endpoint devices.
– The EndpointVolume API enables specialized clients to control and monitor the volume levels of audio endpoint devices.

Ray M. wrote a great article about how to use these APIs for adjusting system volume on CodeProject http://www.codeproject.com/KB/vista/CoreAudio.aspx . For more details you can read at that article. I just tested the available codes if they really work and stored both of methods into one project so that I can use later. The complete backup code you can download here “Adjust System Volume”.

UPDATE 04.03.2010
There are problems at synchronizing between volume and slider. No idea how to solve it!

Windows – Desktopography 2009 Win7 theme

The most interesting feature in Windows 7 is changing the desktop wallpaper after given time. With this feature we don’t have a boring wallpaper all time and I like it very much. I can create myself a theme and add many wallpapers that I like and let themselves changed after 15 minutes. And every time when I minimize all of working windows and I’ll see an other wallpaper, that’s just a … small change but anyway it makes my computer more exciting than before. Microsoft offers a lot of themes too. You can find them at following link http://windows.microsoft.com/en-US/windows/downloads/personalize but none of them pleases me.
I have tried a lot of theme (my own one and the others) but what I like at most is theme with Desktopography wallpapers which I choose from the gallery of Desktopography. They are really amazing wallpapers and even can be the best wallpapers of 2009. Therefore I would like to share my theme pack, it is pretty large, about 50 MB but I think it’s worth to download and install as your win7 theme. My own pack is larger but I delete some of wallpapers before sharing because the size is so large. It’s about 80 MB at original size.

Desktopography 2009

You can download it at following link
Mirror 1: http://www.megaupload.com/?d=K033U3E0
Mirror 2: http://ugotfile.com/file/791246/Desktopography2009.themepack
I really respect what the authors of this wallpapers have done. Not only how they design and use color but also their imagination. How can they draw such scenes in their brains? My grandfather always tell me that I should read more fictional novel to improve my imagination. He says that science is good but without imagination human can make any inventive step. I think my grandfather is right because a lot of scientific projects are trying to make some imaginative things to be real.