⬆️ ⬇️

We program Windows 7: Taskbar. Part 9 - PeekBitmap

Earlier, I wrote about the possibility of Windows 7 taskbar to change the preview for the 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 the mouse over the preview windows, all other windows will disappear and the highlighted window will be displayed. It looks like this.







The Windows 7 taskbar in this case also allows us to set our own behavior. In this case, we have the opportunity 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 like this. The user in the taskbar is an application that does 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 hover the mouse over the application icon. In this case, it will display a preview, which contains additional information (which I also wrote about). The user can hover on the preview of the desired window, in which case all windows will disappear and only the highlighted window will remain on the screen. In this window, we can display even more information that the user needs.

')

In general, by default, in such a scenario, if you hide the other windows, the actual contents of the window will be displayed. Redefining the contents of the window at this point 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 such a script in our application, we will use the .NET Interop Sample Library. For these purposes, the SetPeekBitmap method of a wrapper class is intended. It is necessary to generate the image at the time when the user points to the preview window. For this purpose, 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 difficult. In general, we can copy a snapshot of our window into this Bitmap and draw the information we need over it. Let's fill this area with some background in the demo app and draw a status icon over 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;

}




Please note that in order to display correctly, the size of this image must match the size of the form. After that, we have an application that looks like this.







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 the demo application, I also set this image in the timer. Thus, I can hover the mouse 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 (boolean). 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, I will see the following result.







Demo application:

Taskbar-PeekBitmap.zip

Source: https://habr.com/ru/post/60791/



All Articles