Programming Windows 7: Taskbar. Part 9 - PeekBitmap

    Earlier, I wrote about the ability of the Windows 7 taskbar to change the preview for a window. We talked about the fact that in preview you can display both part of the window and your own image. Windows 7 has such a nice feature that if we hover over the preview window, then all other windows will be hidden, and the selected window will be displayed. It looks as follows.



    The Windows 7 taskbar in this case also allows us to set our own behavior. In this case, we are able to set the contents of our form with our own image. For example, there we can write some useful text. In this case, the scenario may look as follows. The user has an application in the taskbar that is running something. It can track its status based on the ProgressBar and OverlayIcon, which I wrote about earlier. If this information is not enough for him, he can move the mouse cursor over the application icon. In this case, a preview will be displayed to him, which contains additional information (which I also wrote about). The user can point to the preview of the desired window and in this case all the windows will be hidden and only the selected window will remain on the screen.

    In general, by default, in such a scenario, when the other windows are hidden, the contents of the window will be displayed. Overriding the contents of the window at this moment can be useful if the information on the form is scattered and it is not immediately clear what is happening in the application.

    In order to implement a similar scenario in our application, we will use the .NET Interop Sample Library. The SetPeekBitmap method of the wrapper class is intended for these purposes. It is necessary to generate an image at a point in time when the user points to the preview window. For these purposes, override the WndProc method and catch the WM_DWMSENDICONICLIVEPREVIEWBITMAP event. Just at this point in time you need to generate an 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 call the method that will generate the Bitmap we need. The generation of this image is also not of any complexity. In general, we can copy a screenshot of our window into this Bitmap and draw the information we need on top of it. In a demo application, let's fill this area with some background and draw a status icon on top of it. Please note that for the correct display the size of this image must match the size of the form. After that, we get an application that looks like this.

    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;
    }






    In fact, after this image can be installed from anywhere. The main thing is that this image is also generated at the time of processing the above event. For example, in a demo application, I also set this image in a timer. Thus, I can move the mouse cursor over the preview and watch how the form itself changes when the timer is triggered.

    In addition, the SetPeekBitmap method has a third parameter of the boolean type. By changing this parameter, you can specify whether to remove the application frame when a similar view is performed. For example, if I set this parameter to false, then I will see the following result.



    Sample Application:
    Taskbar-PeekBitmap.zip

    Also popular now: