# VBForums CodeBank > CodeBank - Visual Basic 6 and earlier >  [RESOLVED] VB6 Unexpected Error on Windows 7

## ACProctor

A forms-based VB6 application that runs on XP and Windows Server 2003 fails on Windows 7 with "Unexpected Error", even before the first form is launched.

(I will be answering myself as I've just solved it. I just wanted to pass on the knowledge)

----------


## ACProctor

The forms in this application were given high-resolution icons (256x256). Although VB6 can't make full use of these, it all seems to work well under XP and Windows Server 2003. The expectation was that it would yield better quality icons on Windows 7.

However, a copy deployed on Windows 7 using the Package & Deployment Wizard would not launch. It immediately failed with "Unexpected Error", and with no clues as to the cause.

The original application was re-compiled with smaller 72x72 icons, and re-deployed. This worked fine on Windows 7.

----------


## Nightwalker83

Did fixing the icon solve the problem with the "Unexpected Error"?

----------


## Bonnie West

@ ACProctor

Can you attach a sample of the icon(s)? 256x256 icons can be made to work flawlessly in XP and Win 7 just by rearranging the order of the internal images.

----------


## ACProctor

These icons were on the VB6 forms rather than in a resource file. I thought I'd mention that in case it was relevant to the suggesting of ordering. As far as I am aware, the IDE cannot be made to choose an application icon from a resource file when there are forms in the project.

Yes, changing the form icons from 256x256 to 72x72 worked fine.

----------


## Bonnie West

Try these 256x256 icons that were edited in order to be compatible with VB6. The included project demonstrates how to utilize them by calling some API functions.

----------


## Joacim Andersson

This thread has been reported a couple of times. It's been asked to be moved to the VB6 forum. Even though the original post didn't have any code (which is probably why people don't think it belongs in the CodeBank) it's a valuable tip, just as good as any code snippet. Bonnie also posted some code which might very well belong here so I've decided to leave this thread here in the CodeBank instead of moving it to VB6 where it soon will just disappear among the huge amount of new threads in that forum.

----------


## afdoal

> Try these 256x256 icons that were edited in order to be compatible with VB6. The included project demonstrates how to utilize them by calling some API functions.


thanks for the sample. 
but i have a problem:
how to add your code in MDIForm?



```
Option Explicit

Private hIcon As Long


Private Sub MDIForm_Load()
Dim R As RECT, sngHeight As Single, sngWidth As Single


    R.Top = 20&:    R.Bottom = 20& + ICON_JUMBO
    R.Left = 20&:   R.Right = 20& + ICON_JUMBO


    If AdjustWindowRectEx(R, GetWindowLongW(hWnd, GWL_STYLE), 0&, GetWindowLongW(hWnd, GWL_EXSTYLE)) Then
        sngWidth = (R.Right - R.Left) * Screen.TwipsPerPixelX
        sngHeight = (R.Bottom - R.Top) * Screen.TwipsPerPixelY
        Move Screen.Width * HALF - sngWidth, (Screen.Height - sngHeight) * HALF, sngWidth, sngHeight
    End If


    If App.LogMode Then
        hIcon = LoadImageW(App.hInstance, WINDOWS_ICON, IMAGE_ICON, ICON_JUMBO, ICON_JUMBO)
        SendMessageW hWnd, WM_SETICON, ICON_BIG, LoadImageW(App.hInstance, WINDOWS_ICON, IMAGE_ICON, , , LR_DEFAULTSIZE)
        SendMessageW hWnd, WM_SETICON, ICON_SMALL, LoadImageW(App.hInstance, WINDOWS_ICON, IMAGE_ICON, 16&, 16&)
    End If
End Sub


Private Sub MDIForm_Resize()
    If WindowState <> vbMinimized Then
        Cls
        If hIcon Then
            DrawIconEx hDC, (ScaleWidth - ICON_JUMBO) * HALF, _
                            (ScaleHeight - ICON_JUMBO) * HALF, hIcon, ICON_JUMBO, ICON_JUMBO
        Else
            PaintPicture Icon, (ScaleWidth - ScaleX(Icon.Width, vbHimetric, vbPixels)) * HALF, _
                               (ScaleHeight - ScaleY(Icon.Height, vbHimetric, vbPixels)) * HALF
        End If
    End If
End Sub


Private Sub MDIForm_Unload(Cancel As Integer)
    SendMessageW hWnd, WM_SETICON, ICON_BIG, 0&
    SendMessageW hWnd, WM_SETICON, ICON_SMALL, 0&
End Sub
```

if i write your code on MDIForm i get error *Sub Or Function not defined* on:
*Cls
**PaintPicture**ScaleX
**ScaleY*
*
*how to solve this?

thanks b4  :Smilie:

----------


## Bonnie West

> if i write your code on MDIForm i get error *Sub Or Function not defined* on:
> *Cls**PaintPicture**ScaleX**ScaleY*


That's because an MDIForm doesn't have any of those methods.




> how to add your code in MDIForm?




```
Option Explicit     'Don't forget to set the "windows_perfection_logo_v2_d-bliss.ico" icon as the MDIForm's Icon! (Set it via the Properties Window)

Private Const pbID As String = "picIcon"

Private Declare Function DestroyIcon Lib "user32.dll" (ByVal hIcon As Long) As Long
Private Declare Function FindWindowExW Lib "user32.dll" (Optional ByVal hWndParent As Long, Optional ByVal hWndChildAfter As Long, Optional ByVal lpszClass As Long, Optional ByVal lpszWindow As Long) As Long
Private Declare Function InvalidateRect Lib "user32.dll" (ByVal hWnd As Long, Optional ByVal lpRect As Long, Optional ByVal bErase As Long = -True) As Long

Private m_hIcon   As Long
Private m_hWndMC  As Long
Private m_picIcon As VB.PictureBox

Private Sub MDIForm_Load()
    m_hWndMC = FindWindowExW(hWnd, , StrPtr("MDIClient"))

    If App.LogMode Then
        Set Icon = Nothing
        m_hIcon = LoadImageW(App.hInstance, WINDOWS_ICON, IMAGE_ICON, ICON_JUMBO, ICON_JUMBO)
        SendMessageW hWnd, WM_SETICON, ICON_BIG, LoadImageW(App.hInstance, WINDOWS_ICON, IMAGE_ICON, , , LR_DEFAULTSIZE)
        SendMessageW hWnd, WM_SETICON, ICON_SMALL, LoadImageW(App.hInstance, WINDOWS_ICON, IMAGE_ICON, 16&, 16&)
    End If

    Set m_picIcon = Controls.Add("VB.PictureBox", pbID)
    With m_picIcon
        .AutoRedraw = True
        .BackColor = BackColor
        .BorderStyle = 0
        .ClipControls = False
        .ScaleMode = vbPixels
    End With
End Sub

Private Sub MDIForm_Resize()
    If WindowState <> vbMinimized Then
        With m_picIcon
            .Cls
            .Move 0!, 0!, ScaleWidth, ScaleHeight

            If m_hIcon Then
                DrawIconEx .hDC, (.ScaleWidth - ICON_JUMBO) * HALF, _
                                 (.ScaleHeight - ICON_JUMBO) * HALF, m_hIcon, ICON_JUMBO, ICON_JUMBO
            ElseIf Not Icon Is Nothing Then
               .PaintPicture Icon, (.ScaleWidth - .ScaleX(Icon.Width, vbHimetric, vbPixels)) * HALF, _
                                   (.ScaleHeight - .ScaleY(Icon.Height, vbHimetric, vbPixels)) * HALF
            End If

            Set Picture = .Image
            InvalidateRect m_hWndMC
        End With
    End If
End Sub

Private Sub MDIForm_Unload(Cancel As Integer)
    Set m_picIcon = Nothing
    Controls.Remove pbID

    If m_hIcon Then
        DestroyIcon m_hIcon
        DestroyIcon SendMessageW(hWnd, WM_SETICON, ICON_BIG, 0&)
        DestroyIcon SendMessageW(hWnd, WM_SETICON, ICON_SMALL, 0&)
    End If
End Sub
```

BTW, I've just realized that the code in the attachment above was leaking the icon handles. I've included the fixes (Set Icon = Nothing and DestroyIcon ...) in this MDIForm code.

----------


## LaVolpe

Tip: To avoid having to  worry about leaking the big/small icons, maybe consider using the LR_SHARED constant during LoadImage call?

Edited: LR_SHARED, however, may not be appropriate for every situation

----------

