Windows 7 programming: Taskbar. Part 9 – PeekBitmap.

Windows 7 +1

Earlier I wrote about possibility of the task panel of Windows 7 to change preview for a window. We said that in preview it is possible to display both a window part, and own image. At Windows 7 there is such pleasant feature, that if we will hover the mouse cursor on preview windows all other windows will disappear, and the selected window will be displayed. It looks as follows.

 

In this case the panel of problems Windows 7 allows us to set own behaviour too. In this case we have possibility to set contents of our form our own image. For example, we can write any useful text. In this case the work scenario can look as follows. The user in the panel of problems has an application which something does. It can observe its state on the basis of ProgressBar and OverlayIcon about which I wrote earlier. If this information has not enough, he can hover the mouse cursor at an icon of the application. In this case preview in which the additional information contains it will be displayed (about what I wrote too). The user can hover on window preview and in this case all windows will disappear and on the screen there will be only an selected window. In this window we can display even more information which is necessary for the user.

By default at such scenario at concealment of other windows window contents will be displayed. Such redefinition of the content of a window can be useful, if on the form the information is separated and at once is not clear that occurs in the application.

To implement the similar scenario in our application we will take advantage .NET Interop Sample Library. SetPeekBitmap method of a class-wrapper is intended for these purposes. When the user hovers on preview windows it is necessary to generate the image. For these purposes we will redefine method WndProc and we will catch event WM_DWMSENDICONICLIVEPREVIEWBITMAP. Just during this moment of time it is necessary to generate the image.

protected override void WndProc(ref Message m)


{


    if (m.Msg == WM_DWMSENDICONICLIVEPREVIEWBITMAP)


    {


        WindowsFormsExtensions.SetPeekBitmap(this, GeneratePeekBitmap(this, Images._111), true);


    }


 


    base.WndProc(ref m);


}

In this case we invoke a method which will generate necessary to us Bitmap. Generation of this image also does not represent any complexity. Generally we can copy in this Bitmap a picture of our window and over it to draw the information necessary to us. I will fill this area with a certain background and I will draw a icon of a state atop it.

private static Bitmap GeneratePeekBitmap(Form form, Image stateImage)


{


    var preview = new Bitmap(form.ClientSize.Width, form.ClientSize.Height);


 


    var g = Graphics.FromImage(preview);


 


    g.DrawImage(Images.background.GetThumbnailImage(form.ClientSize.Width, form.ClientSize.Height, null, IntPtr.Zero), 0, 0);


    


    if (stateImage != null)


    {


        Size thumbSize = new Size(100, 100);


        g.DrawImage(stateImage.GetThumbnailImage(thumbSize.Width, thumbSize.Height, null, IntPtr.Zero), form.ClientSize.Width / 2 - thumbSize.Width / 2, form.ClientSize.Height / 2 - thumbSize.Height / 2);


    }


 


    return preview;


}





Pay attention, that for correct display the sizes of this image should coincide with the size of the form. After that at us the application which looks as follows will turn out.



 



Actually, after that the image to is established whence it can be necessary. The main thing that this image also was generated at the moment of processing specified above event. For example, in the demonstration sample I also set this image in the timer. Thus I can hover the mouse cursor on preview and observe of that as the form changes at timer tick.



Besides, SetPeekBitmap method has a third parametre of logic type (boolean). Whether changing this parametre it is possible to specify it is necessary to remove a border of application. For example, if I set this parametre in false I will see the following result.





Sample application:



Windows 7 programming: Taskbar. Part 8 – IconicThumbnail.

Windows 7 +1

Recently I have told about how it is possible to influence on preview windows in the task panel of Windows 7. We displayed only that part of a window which is most important for the user in that time. Nevertheless in certain situations be much more useful displaying of other picture (an application state), instead of window parts.

For example, this possibility uses Windows Live Messanger. If we hover the mouse cursor to WLM icon it will display avatars of the current user in an preview window .

Windows Live Messanger

Let’s understand this possibility of the task panel of Windows 7. For implementation of similar behaviour in .NET Interop Sample Library there is SetIconicThumbnail method. With its help we can create easily and simply own preview for our window. However, before we will start it to use we should enable this behaviour by means of EnableCustomWindowPreview method. Otherwise at executing of SetIconicThumbnail method we will receive a corresponding exception. To make it it is possible directly in the designer of the form.

public Form1()


{


    InitializeComponent();


    WindowsFormsExtensions.EnableCustomWindowPreview(this);


}

It is necessary to tell, that the sizes of the image which we wish to display in preview are limited. It is logical, because would present that was, if preview could be the size 1024×768. These sizes are limited by value 200×120. The image can be less than this size. In this case the window preview also will be reduced. If the image more than these sizes the exception will be throwed.

In parametres of SetIconicThumbnail method the reference to the current form and the image (Bitmap) which need to be displayed is passed. Our goal – to generate this Bitmap. And in this case at us hands are completely untied – we can generate all that is necessary for us. It can be a window picture, with any additional text atop. It can be any own picture containing the information. It can be the text with a some statistics. All depends on your imagination.

I will generate the image displaying a state of application by the text and graphically, and also a small picture of a window in the demonstration application. For this purpose I will create Bitmap with necessary sizes to me and I will fill in with its background. After that I will draw there the image of the state and a window picture. Then I will write the text of the state on this image and I will transfer result in SetIconicThumbnail method.

private static void SetState(Form form, string stateText, Image stateImage)


{


    // blank image


    var preview = new Bitmap(200, 120);


    var g = Graphics.FromImage(preview);


 


    // fill background


    g.DrawImage(Images.background, 0, 0);


 


    // file image of state


    if (stateImage != null)


    {


        g.DrawImage(stateImage.GetThumbnailImage(100, 100, null, IntPtr.Zero), 100, 10);


    }


 


    // fill image of form


    g.DrawImage(GetFormImage(form, 50, 60), 10, 10);


 


    // draw text


    g.DrawString(stateText, new Font("Verdana", 18), Brushes.White, 10, 70);


 


    // setting thumbnail


    WindowsFormsExtensions.SetIconicThumbnail(form, preview);


}

We can see that the code has turned out laconic enough. Finally, we can see a window with the following preview.

That is interesting, our application can change itself this state in a background. Thus, if the mouse is on a icon the application and preview at present is displayed, image change in an emerging window occurs smoothly, without any by-effects. So, for example, I can create the timer in which handler through certain time intervals to change a state.

private readonly string[] _stateTexts = new[] { "Deleting..", "Wireless", "Security", "Stop", "Help", "O'kay", "Playing", "Login", "Warning", "Showcase", "Search", "Processing..", "Locked", "Error", "Refreshing.." };


private readonly Bitmap[] _stateImages = new[] { Images._104, Images._110, Images._111, Images._112, Images._113, Images._114, Images._120, Images._125, Images._129, Images._134, Images._17, Images._25, Images._41, Images._50, Images._52 };


private int _currentIndex = 0;


 


private void timer1_Tick(object sender, EventArgs e)


{


    if (_currentIndex + 1 >= _stateImages.Length)


    {


        _currentIndex = 0;


    }


    else


    {


        _currentIndex++;


    }


    SetState(this, _stateTexts[_currentIndex], _stateImages[_currentIndex]);


}





Preview windows of our application will dynamically change, and the user can see this state easily and conveniently



Sample application:



Windows 7 programming: Taskbar. Part 7 – ThumbnailClip.

Windows 7 +1

As we already saw, Windows 7 possesses convenient functionality for display preview of windows of applications. It is possible to look the reduced representation of a window for few seconds at mouse hover at a window icon in the task panel. It is very convenient when at the user openly considerable quantity of windows.

All contents of a window are displayed by default in these preview . However, for some appendices it would be much more convenient to show in preview not all contents of a window, but only its part. Such functionality also is provided for the task panel of Windows 7 and we can use it for the out applications. Let’s understand this possibility of the task panel of Windows 7.

As usually we will use for this purpose .NET Interop Sample Library. In wrappers for functions from system libraries of Windows here there is SetThumbnailClip method which will help us with implementation of the given functionality. By a call of the given method in parametres it is necessary to transfer a current form and the co-ordinates limiting area of a window.

private void Clip5_Click(object sender, EventArgs e)


{


    WindowsFormsExtensions.SetThumbnailClip(this, new Rectangle(10, 10, 145, 145));


}

On this example it is well visible that to use this method very easily.

Let’s create the small application where we will look at possibilities of this functionality. For this purpose I will create the empty application in which I will add some some controls. After an application launch preview windows will look as follows.

image

It is visible, that in preview all window is displayed. Let’s limit area of display by means of SetThumbnailClip method. Let’s display, for example, only entry fields which are on the form.

image

It is interesting, that if the form has a dynamic content (for example, video) it will be displayed in dynamics. I have placed some animated images and have displayed them in preview in the my demonstration application. Thus there is no necessity for this purpose to do something else.

image image

It is important, that we can change contents preview in the course of application work. For example, during any moment of time it can be necessary to display contents of some important entry field, and during other moment – to display the image from the form. Such dynamism can give the chance to receive to the user the actual information for it.

Finally, if it is necessary for us to display all contents of the form it is possible to use the same method, but to pass it the sizes of all form. In this case all window will be displayed in preview again.

private void NoClip_Click(object sender, EventArgs e)


{


    WindowsFormsExtensions.SetThumbnailClip(this, new Rectangle(new Point(0, 0), Size));


}





Sample application:



Windows 7 programming: Taskbar. Part 6 – AppId.

Windows 7 +1Recently I wrote how it is possible to display tabs for child windows of our application. In this case we needed to display child windows of our application. For these purposes we used CustomWindowManager class.

The same effect can achieve, if we start some copies of same our application. In this case Windows 7 will define that this same application and will group windows in one icon.

Let’s try understand how it works. Basis is concept Application ID. For each window we can set the ID. This feature also can be used when it is necessary to avoid such behaviour. In this case it is necessary to set various AppId for each such window. To make it it is possible using. For this purpose it is necessary to invoke SetAppId method.

private void SetAppIdButton_Click(object sender, EventArgs e)


{


    WindowsFormsExtensions.SetAppId(this, “SomeAppId”);


}



Thus, we can set various AppId for each window and they will not be grouped.





However, similarly also probably to group some windows from different applications. For this purpose it is necessary to set identical AppId for each of windows. In this case the task panel of Windows 7 will group these applications in one icon. For example, in this case I have set identical AppId for different windows from different applications and these windows have appeared grouped in the task panel of Windows 7.





Sample application:



Windows 7 programming: Taskbar. Part 5 – CustomWindowsManager.

Windows 7 +1

We continue to study possibilities of the task panel in Windows 7. For certain all of you have paid attention that if the same application to start some times, Windows 7 will automatically group them in one button on the task panel. Besides at hover on a icon of this application the task panel will show a set preview for these windows.

However, we see, that the same behaviour is realised for opened tabs in Internet Explorer 8. In this case one copy IE8 (we will lower technical details) and many tabs is actually started. In this case Internet Explorer displays them in the form of several previews. In this case it is very useful, because directly from the task panel it is possible to be switched at once on necessary tab.

Windows 7 - Internet Explorer

It is possible to guess, such functionality is simple for implementing and for the our application. It is actual, if in your window the set of other windows (as in a case with IE8) contains. For these purposes in .NET Interop Sample Library there is class CustomWindowsManager.  Let’s more in detail consider creation of such application.

So, to begin with it is necessary for us to create the application. Let this application will work in MDI mode. Our purpose – to achieve that all child windows of this application also were displayed in the task panel. After creation of the main and child window let’s be engaged in the last.

The moment of display of a window when this window is already created and ready to work is necessary for us. Very well for these purposes OnShown event approaches. In the handler we should create the CustomWindowsManager object and transfer to it Handle an child window, and also the parental window, from the point of view of model MDI.

CustomWindowsManager _windowsManager;
 
protected override void OnShown(EventArgs args)
{
    _windowsManager = 
        CustomWindowsManager.CreateWindowsManager(Handle, MdiParent.Handle);
 
    base.OnShown(args);
}


We will keep the reference to object CustomWindowsManager, it is required to us later. However, it is not enough these actions for correct work. First, we should subscribe for ThumbnailRequested event in which to generate Bitmap, containing display of our window for emerging windows in the task panel. Secondly, we should subscribe for PeekRequested event in which to generate Bitmap..



ThumbnailRequested event contains parametre of type BitmapRequestedEventArgs. This object will operate that will be displayed on the task panel. Most simple, that we can make, it to specify parametre UseWindowScreenshot equal true. In this case the screenshot of a window without your participation will be made. If it is necessary to display any own picture in preview windows, it is possible to take advantage of parametre Bitmap where to put object Bitmap which should be displayed. However, it is necessary to understand, that this object should have strictly set sizes. These sizes we also can receive from BitmapRequestedEventArgs.



_windowsManager.ThumbnailRequested += (o, e) =>
{
    Bitmap bmp = new Bitmap(e.Width, e.Height);
    this.DrawToBitmap(bmp, new Rectangle(0, 0, e.Width, e.Height));
    e.Bitmap = bmp;
};


It is clear, that here we can manipulate this object as to us conveniently. For example, we can impose on preview our picture.



_windowsManager.ThumbnailRequested += (o, e) =>
{
    Bitmap bmp = new Bitmap(e.Width, e.Height);
    this.DrawToBitmap(bmp, new Rectangle(0, 0, e.Width, e.Height));
    Graphics.FromImage(bmp).DrawImage(Images.coffeecup, 35, 5);
    e.Bitmap = bmp;
};


PeekRequested event is intended to allocate a window at mouse hover for it preview in the task panel. Here too there is a parametre of type BitmapRequestedEventArgs. Similarly it is possible to use properties UseWindowScreenshot and Bitmap.



_windowsManager.PeekRequested += (o, e) =>
{
    var result = new Bitmap(e.Width, e.Height);
    DrawToBitmap(result, new Rectangle(0, 0, e.Width, e.Height));
    e.Bitmap = result;
};




Pay attention, that I use method DrawToBitmap of the form in these examples. However, I can display preview any other control. For example, I can display TextBox.



Finally, it is necessary to fire WindowClosed method when the window is closed. For this purpose event OnClosed well approaches.



protected override void OnClosed(EventArgs e)
{
    if (_windowsManager != null)
    {
        _windowsManager.WindowClosed();
    }
 
    base.OnClosed(e);
}


After that we have received the following application.





We will see the following if we look at the task panel.



 



However, if we start to change appearance of the form we will see, that preview has not changed. It is caused by that Windows 7 did not request event preview. It is especially critical, if on the form there is a content which constantly changes, for example video. In this case there is a way to update preview manually. For this purpose it is necessary to start InvalidatePreviews method.



private void InvalidateButton_Click(object sender, EventArgs e)
{
    _windowsManager.InvalidatePreviews();
}








This method in each concrete case should be caused during the necessary moments of time. For example, at text updating in TextBox. For video a good variant – to cause it on the timer.





Here so it is easy and simple you can display the quantity of child windows of your application.



Sample application:



Windows 7 programming: Taskbar. Part 4 – Custom OverlayIcon.

Windows 7 +1Earlier I wrote how it is possible to add a icon of a state for your application in the task panel of Windows 7. We considered static images for these purposes. I will remind, that for this purpose we used SetTaskbarOverlayIcon method in which parametres the reference on Icon is transferred.

However, there are other scenarios in which on a place of an overlay icon few information is displayed. For example, it can be current download speed if your software something downloads from a network. Or it can be quantity of new letters in a mail box if it is the post client.

I will remind, that to set the static image it is necessary to execute the following code.

WindowsFormsExtensions.SetTaskbarOverlayIcon(this, Icons.Error, "Error");




As the second parametre Icon object here is transferred. However who prevents to us to generate this object dynamically? Let’s take advantage of a simple code and we will do it. I will create a method which will generate such image and I will show this icon.



private static Icon BuildIcon(int param)
{
    Bitmap image = Icons.BLANK2334242;
    Graphics.FromImage(image).DrawString(param.ToString(@"D2"), new Font("Arial", 54), Brushes.White, 10, 25);
    return Icon.FromHandle(image.GetHicon());
}
 
 
private void ShowStatus(object sender, EventArgs e)
{
    WindowsFormsExtensions.SetTaskbarOverlayIcon(this, BuildIcon(50, "Status");
}


Thus, by means of method BuildIcon the new icon which will be displayed on the task panel will be generated.



Custom OverlayIcon



On the given screenshot it is well visible, that to a standard icon we have added the text and have displayed it on the task panel.



In the sample application I have created the timer which simulates work of the download manager, giving out constantly different speed of downloading.



Sample application:



Windows 7 programming: Taskbar. Part 3 – OverlayIcon.

Windows 7 +1This time we will talk about other feature of task panel of Windows 7 – OverlayIcon. This possibility is very useful for applications which have any state from the point of view of the user.

For example, such applications are programs for an exchange of instant messages (IM). For example, Windows Live Messanger actively uses this possibility. For example, if we open WLM and we will change a state it is possible to see as it is displayed on the task panel of Windows 7.

Windows Live Messanger

Windows Live Messanger Windows Live Messanger

Such functionality can be useful to many applications. Fortunately to implement it not so it is difficult. As well as in the previous examples we will use library .NET Interop Sample Library.

To add a state icon to the main icon of the application, it is necessary to add a file of resources in the project and to place there the necessary icons. Also, we can get Icon objects from other places if it is necessary.

Ресурсы

Now everything, that it is necessary to make is to use extension methods, allowing to set icons for our application. For these purposes method SetTaskbarOverlayIcon is defined. We should pass to them a form reference, an icon and a description. Thus, setting of a new icon from a code will look as follows.

WindowsFormsExtensions.SetTaskbarOverlayIcon(this, Icons.Error, "Error");


Also we can remove this icon. For this purpose it is necessary to pass null value instead of the icon.



WindowsFormsExtensions.SetTaskbarOverlayIcon(this, null, String.Empty);






Let’s create the small application in which we can switch a state. It will look as follows.



Sample application



By pressing each of buttons the state which is displayed in the task panel is switched.







Normal state Config state Error stateLogin state Stop state



Sample application:



PHP driver for SQL Server now in Web Platform Installer

Web Platform Installer is a tool, which contains many libraries, components and ready to use solutions. This tool very useful, because allow to save many time. Also, it is free tool and you can download it and install on your machine.

Web platform installer

However, speech now not about it. Recently there was updating WPI in which there was a driver for support SQL Server in PHP. It is easy to be convinced of it if to start an installing package.

Thus, developing on PHP you can use all benefits of usage of SQL Server. I will remind, that support PHP in IIS7 exists already for a long time and works not worse, than on *nix.

Update of .NET RIA Services (preview) is available

.NET RIA Services

Update of .NET RIA Services preview has been become available.

What’s news:

  • Additional template for business applications. This template contain a forms for login and new user.
  • Authentication sample.
  • Provider for store metadata in XML. This approach allow to store metadata outside the class definition (attributes). It can be useful in some cases.
  • Sample of implementation an own provider for .NET RIA Services.

Besides, some bugs has been fixed. These bugs has been received including via forum. It once again underlines importance of a feedback for developers team.

I will remind, that the platform.NET RIA Services allows to quickly create RIA.

Download: http://www.microsoft.com/downloads/details.aspx?FamilyID=76bb3a07-3846-4564-b0c3-27972bcaabce&displaylang=en

Source: http://blogs.msdn.com/brada/archive/2009/05/11/net-ria-services-may-2009-preview.aspx

Windows 7 programming: Taskbar. Part 2 – ThumbButtons.

Windows7 +1

Recently I was telling about how we can display progress inside task panel in Windows7. This time we will continue to talk about Windows 7 possibilities for developers. Let’s look how we can use buttons in window’s preview in task panel.

Such functionality you can see in Windows Media Player already. These buttons allow to change tracks and pause of a playing. Maximum count of these buttons is seven.

Windows 7 - Media player

Such functionality can be useful for other applications undoubtedly. Let’s look how we can use it in our applications.

As I already spoke, for these system functions of Windows 7 there is .NET wrapper which is called .NET Interop Sample Library. We were using this library when were creating application for progress bar state changing. We will are using this library again.

The creation of buttons should be executed when WM_TaskbarButtonCreated message is firing. Therefore it is necessary to override WndProc method in a form.

protected override void WndProc(ref Message m)
{
    if (m.Msg == Windows7Taskbar.TaskbarButtonCreatedMessage)
    {
        // initialize buttons
    }
    
    base.WndProc(ref m);
}


A ThumbButtonManager object is necessary to initialize buttons. This object is managing behavior and visibility of these buttons. We can create the ThumbButtonManager object via extension method CreateThumbButtonManager. It is necessary to use CreateThumbButton method to create buttons after that. At last, we should add these buttons to the task panel via AddThumbButtons method.



protected override void WndProc(ref Message m)
{
    if (m.Msg == Windows7Taskbar.TaskbarButtonCreatedMessage)
    {
        InitializeThumbButtons();
    }
 
    base.WndProc(ref m);
}
 
 
protected void InitializeThumbButtons()
{
    ThumbButtonManager thumbButtonManager = 
        WindowsFormsExtensions.CreateThumbButtonManager(this);
 
    var decreaseThumbButton = thumbButtonManager.CreateThumbButton(1, 
                        Icons.Navigation_First_2, "To reduce the progress");
    decreaseThumbButton.Clicked += delegate
                           {
                            // ..
                           };
 
    thumbButtonManager.AddThumbButtons(decreaseThumbButton);
}


Now, when application starts we can see that thumb button is shown. However, if we try click her we can see that event was not fired. It is necessary to dispatch a message in WndProc method that event handler can fire.



As a result we will receive the following simple code.



private ThumbButtonManager _thumbButtonManager;
 
protected override void WndProc(ref Message m)
{
    if (m.Msg == Windows7Taskbar.TaskbarButtonCreatedMessage)
    {
        InitializeThumbButtons();
    }
    
    if (_thumbButtonManager != null)
        _thumbButtonManager.DispatchMessage(ref m);
 
 
    base.WndProc(ref m);
}
 
 
protected void InitializeThumbButtons()
{
    if (_thumbButtonManager == null)
    {
        _thumbButtonManager = WindowsFormsExtensions.CreateThumbButtonManager(this);
    }
    
    var decreaseThumbButton = _thumbButtonManager.CreateThumbButton(1, Icons.Navigation_First_2, "To reduce the progress");
    decreaseThumbButton.Clicked += delegate
                       {
                        Progress.Text = (float.Parse(Progress.Text) - 10).ToString();
                        WindowsFormsExtensions.SetTaskbarProgress(this, float.Parse(Progress.Text));
                       };
 
    // other buttons
 
    _thumbButtonManager.AddThumbButtons(decreaseThumbButton, normalStateThumbButton, indeterminateStateThumbButton, pauseStateThumbButton, errorStateThumbButton, increaseThumbButton);
}


This application contains thumb buttons to control progress bar.



Windows 7 - Thumb buttons



Good luck to you in development of your Windows 7 applications!



Sample application: