4

I would like to use the DwmSetIconicThumbnail function to set a static image for the thumbnail preview of my app.

As pointed in the reference link above, firstly is necessary to call DwmSetWindowAttribute to enable DWMWA_FORCE_ICONIC_REPRESENTATION and DWMWA_HAS_ICONIC_BITMAP attributes.

I already did all that. I've taken all the definitions from WindowsAPICodePack source code here, and I'm following the same steps (or I think so).

The problem is that when I try to adapt the example for my WinForms Window, I get a E_INVALIDARG HRESULT code when calling DwmSetIconicThumbnail function at the end of the code below, I'm not sure whether the problematic argument is the hwnd, or the hBitmap.

What I'm doing wrong?.


C#:

Bitmap bmp;
IntPtr hBitmap;
IntPtr hwnd;
int hresult;

const int DisplayThumbnailFrame = 0x1;
public enum DwmWindowAttribute : uint
{
    NcRenderingEnabled = 1,
    NcRenderingPolicy,
    TransitionsForceDisabled,
    AllowNcPaint,
    CaptionButtonBounds,
    NonClientRtlLayout,
    ForceIconicRepresentation,
    Flip3DPolicy,
    ExtendedFrameBounds,
    HasIconicBitmap,
    DisallowPeek,
    ExcludedFromPeek,
    Cloak,
    Cloaked,
    FreezeRepresentation,
    Last
}

[DllImport("dwmapi.dll", PreserveSig = true)]
static internal extern int DwmSetWindowAttribute(IntPtr hwnd, 
                                                 DwmWindowAttribute dwAttributeToSet, 
                                                 IntPtr pvAttributeValue, 
                                                 uint cbAttribute);

[DllImport("Dwmapi.dll")]
public static extern int DwmSetIconicThumbnail(IntPtr hwnd, 
                                               IntPtr hBitmap, 
                                               int flags);

private void Form1_Shown() {

    bmp = (Bitmap)Bitmap.FromFile("C:\\Image.jpg");
    hBitmap = bmp.GetHbitmap();
    hwnd = Process.GetCurrentProcess.MainWindowHandle;

    IntPtr block = Marshal.AllocHGlobal(4);
    int value = Math.Abs(Convert.ToInt32(true)); // or 1
    Marshal.WriteInt32(block, value);

    try {
        hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.HasIconicBitmap, block, 4);
        if ((hresult != 0)) {
            throw Marshal.GetExceptionForHR(hresult);
        }

        hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.ForceIconicRepresentation, block, 4);
        if ((hresult != 0)) {
            throw Marshal.GetExceptionForHR(hresult);
        }

    } finally {
        Marshal.FreeHGlobal(block);
    }

    hresult = DwmSetIconicThumbnail(hwnd, hBitmap, DisplayThumbnailFrame);
    if ((hresult != 0)) {
        throw Marshal.GetExceptionForHR(hresult);
    }

}

VB.NET:

Dim bmp As Bitmap
Dim hBitmap As IntPtr
Dim hwnd As IntPtr
Dim hresult As Integer

Const DisplayThumbnailFrame As Integer = &H1

Enum DwmWindowAttribute As UInteger
    NcRenderingEnabled = 1
    NcRenderingPolicy
    TransitionsForceDisabled
    AllowNcPaint
    CaptionButtonBounds
    NonClientRtlLayout
    ForceIconicRepresentation
    Flip3DPolicy
    ExtendedFrameBounds
    HasIconicBitmap
    DisallowPeek
    ExcludedFromPeek
    Cloak
    Cloaked
    FreezeRepresentation
    Last
End Enum

<DllImport("dwmapi.dll", PreserveSig:=True)>
Friend Shared Function DwmSetWindowAttribute(hwnd As IntPtr,
                                             dwAttributeToSet As DwmWindowAttribute,
                                             pvAttributeValue As IntPtr,
                                             cbAttribute As UInteger
) As Integer
End Function

<DllImport("Dwmapi.dll")>
Public Shared Function DwmSetIconicThumbnail(ByVal hwnd As IntPtr,
                                             ByVal hBitmap As IntPtr,
                                             ByVal flags As Integer
) As Integer
End Function

Private Sub Form1_Shown() Handles MyBase.Shown

    bmp = DirectCast(Bitmap.FromFile("C:\Image.jpg"), Bitmap)
    hBitmap = bmp.GetHbitmap()
    hwnd = Process.GetCurrentProcess.MainWindowHandle

    Dim block As IntPtr = Marshal.AllocHGlobal(4)
    Dim value As Integer = Math.Abs(CInt(True)) ' or 1
    Marshal.WriteInt32(block, value)

    Try
        hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.HasIconicBitmap, block, 4)
        If (hresult <> 0) Then
            Throw Marshal.GetExceptionForHR(hresult)
        End If

        hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.ForceIconicRepresentation, block, 4)
        If (hresult <> 0) Then
            Throw Marshal.GetExceptionForHR(hresult)
        End If

    Finally
        Marshal.FreeHGlobal(block)

    End Try

    hresult = DwmSetIconicThumbnail(hwnd, hBitmap, DisplayThumbnailFrame)
    If (hresult <> 0) Then
        Throw Marshal.GetExceptionForHR(hresult)
    End If

End Sub

3 Answers 3

6
+50

According to MSDN Documentation:

An application typically calls the DwmSetIconicThumbnail function after it receives a WM_DWMSENDICONICTHUMBNAIL message for its window. The thumbnail should not exceed the maximum x-coordinate and y-coordinate that are specified in that message. The thumbnail must also have a 32-bit color depth.

So, using the following 32-by-32 bitmap, with 32-bit color depth, it worked:

enter image description here

The exception was gone. However, it didn't quite replace the application icon thumbnail, but appended it.

This is what it looks like using ALT+TAB:

enter image description here

And this when hovering over it:

enter image description here

Note that I did not modify your code at all and ran it exactly as it is, but just using a suitable Bitmap. These are results for a Windows 10 machine.


UPDATE

The reason because the DwmSetIconicThumbnail function returns an error is because the image exceeds the maximum size for the thumbnail, that's it, a resize is not handled by Windows itself, so we need to do a little bit more of work.

I'm not sure about which factor determines the maximum possible thumbnail size that we can establish for our image, this is speculation but I think it depends on a Windows registry value that determines the width and height for thumbnail previews (I exactlly don't remember the registry location of that value, sorry, but it can be Googled easy).

Well, as pointed above, we need to handle the WM_DWMSENDICONICTHUMBNAIL (0x323) message, so we need to override the base Window procedure a.k.a WNDPROC of our Win32 window (a Form), then finally we can retrieve the maximum width and height for the thumbnail creation from the message parameters.

This is a working code sample:

Private Const WM_DWMSENDICONICTHUMBNAIL As Integer = &H323

Protected Overrides Sub WndProc(ByRef m As Windows.Forms.Message)

    Select Case m.Msg

        Case WM_DWMSENDICONICTHUMBNAIL

            Dim hwnd As IntPtr = Process.GetCurrentProcess().MainWindowHandle
            Dim dWord As Integer = m.LParam.ToInt32()
            Dim maxWidth As Short = BitConverter.ToInt16(BitConverter.GetBytes(dWord), 2)
            Dim maxHeight As Short = BitConverter.ToInt16(BitConverter.GetBytes(dWord), 0)

            Using img As Image = Bitmap.FromFile("C:\Image.jpg")

                Using thumb As Bitmap = CType(img.GetThumbnailImage(maxWidth, maxHeight, Nothing, Nothing), Bitmap)

                    Dim hBitmap As IntPtr = thumb.GetHbitmap()

                    Dim hresult As Integer = NativeMethods.DwmSetIconicThumbnail(hwnd, hBitmap, 0)
                    If (hresult <> 0) Then
                        ' Handle error...
                        ' Throw Marshal.GetExceptionForHR(hresult)
                    End If

                    NativeMethods.DeleteObject(hBitmap)

                End Using

            End Using

    End Select

    ' Return Message to base message handler.
    MyBase.WndProc(m)

End Sub

As last comment, and if in the future I need to remember this, I will share this question that I found on MSDN, which can be helpful for someone having problems with WM_DWMSENDICONICTHUMBNAIL message:

2
  • The answer points to a conclusive remark from MSDN that I missed, then with further investigation I finally solved it, thanks for that !, but this answer only demostrates that a tiny icon of 32x32 will work, which is not a solution for a real-life situation. May 21, 2016 at 22:23
  • The reason of the appended image is because the DISPLAYFRAME flag is set, but supposedlly it should be a frame effect instead of a "image append", anyways that effect doesn't interest me, don't worry about, but the information provided in your answer is inconclusive, I hope you can give me permission to extend your answer with the additional required details, in some minutes I'll do that and give bounty reward, thanks again!!. May 21, 2016 at 22:23
1

Using slightly-modified declarations from Microsoft Reference Source (to provide a return value), I'm able to get this to function as expected.

internal static class NativeMethods
{
    [DllImport("dwmapi.dll")]
    public static extern int DwmSetIconicThumbnail(IntPtr hwnd, IntPtr hbmp, DWM_SIT dwSITFlags);

    [DllImport("dwmapi.dll")]
    public static extern int DwmSetWindowAttribute(IntPtr hwnd, DWMWA dwAttribute, ref int pvAttribute, int cbAttribute);

    public enum DWM_SIT
    {
        None,
        DISPLAYFRAME = 1
    }

    public enum DWMWA
    {
        NCRENDERING_ENABLED = 1,
        NCRENDERING_POLICY,
        TRANSITIONS_FORCEDISABLED,
        ALLOW_NCPAINT,
        CAPTION_BUTTON_BOUNDS,
        NONCLIENT_RTL_LAYOUT,
        FORCE_ICONIC_REPRESENTATION,
        FLIP3D_POLICY,
        EXTENDED_FRAME_BOUNDS,
        // New to Windows 7:
        HAS_ICONIC_BITMAP,
        DISALLOW_PEEK,
        EXCLUDED_FROM_PEEK
        // LAST
    }

    public const uint TRUE = 1;
}

Then, I just modified your existing C# code to fit these signatures.

    private void Form1_Shown(object sender, EventArgs e)
    {
        bmp = (Bitmap)Bitmap.FromFile("C:\\Image.jpg");
        hBitmap = bmp.GetHbitmap();
        hwnd = Process.GetCurrentProcess().MainWindowHandle;

        int attributeTrue = (int)NativeMethods.TRUE;
        hresult = NativeMethods.DwmSetWindowAttribute(hwnd, NativeMethods.DWMWA.HAS_ICONIC_BITMAP, ref attributeTrue, sizeof(int));
        if ((hresult != 0))
            throw Marshal.GetExceptionForHR(hresult);

        hresult = NativeMethods.DwmSetWindowAttribute(hwnd, NativeMethods.DWMWA.FORCE_ICONIC_REPRESENTATION, ref attributeTrue, sizeof(int));
        if ((hresult != 0))
            throw Marshal.GetExceptionForHR(hresult);

        hresult = NativeMethods.DwmSetIconicThumbnail(hwnd, hBitmap, NativeMethods.DWM_SIT.DISPLAYFRAME);
        if ((hresult != 0))
            throw Marshal.GetExceptionForHR(hresult);
    }
3
  • Thanks for comment, but you could have seen in my question that I too modified the declarations of DwmSetWindowAttribute and DwmSetIconicThumbnail to return a value... we are using the same. Anyways, of course I tried to verify if the solution that you provided works, but I'm getting the same HRESULT code (System.ArgumentException using your Marshal call) in the DwmSetIconicThumbnail function. Sorry, it does not work for me. May 21, 2016 at 21:24
  • For reference, my note about the retval was to indicate what I'd changed from MS's declarations, not yours. But with my example not working for you, I suspect @jstreet's answer might be more relevant than mine. Would it be possible to post the specific image?
    – cokeman19
    May 21, 2016 at 21:56
  • Thanks again for your help, about the image is not relevant now, but the size it is as I discovered, lets say I was using a random big resolution image, it will fail because I expected that Windows will resize it to fit the maximum possible thumbnail size, but I need to determine the maximum possible size, then resize it by my own. The problem is solved!. I added a code example in jstreet's answer if maybe you are interested to apply this functionality to your apps!. regards. May 21, 2016 at 22:58
0

Maybe the hardcoded "4" for the sizes is at play? See if (c# mods here) this works:

...
IntPtr block = Marshal.AllocHGlobal(sizeof(int));
...
hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.HasIconicBitmap, block, sizeof(int));
...
hresult = DwmSetWindowAttribute(hwnd, DwmWindowAttribute.ForceIconicRepresentation, block, sizeof(int));

Please also be aware that you may need to free the memory used by the bitmap after calling Bitmap.GetHbitmap() (https://msdn.microsoft.com/en-us/library/1dz311e4(v=vs.110).aspx#Anchor_2)

Also, a nice primer on marshaling: http://justlikeamagic.com/2010/03/09/marshaling/

3
  • That does not have relevance, 32 Bits (Int) = 4 Bytes. If you see, I'm using the same flags (ForceIconicRepresentation, HasIconicBitmap), same datatype (Int32) and passing the same right size value (4) like the guys of WindowsCodeApiPack, also, that function does not return any failed HRESULT code, as I mentioned is the other function which returns the error, but Thankyou anyways for the try. May 19, 2016 at 14:15
  • Since you are throwing the exception, what does the exception detail provide? May 20, 2016 at 15:03
  • There is no detail because It is not a managed exception, it is an HRESULT code returned by the DwmSetIconicThumbnail Win32 Function, that reffers to E_INVALIDARG (0x80070057) as I explained, which is the equivalent to System.ArgumentException .NET exception: msdn.microsoft.com/en-us/library/9ztbc5s1%28v=vs.110%29.aspx May 20, 2016 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.