Visual Basic Wiki
Advertisement

The Windows API function to use is this one:

Private Declare Sub OleCreatePictureIndirect Lib "OleAut32" (ByRef lpPICTDESC As Long, rIID As Long, ByVal fOwn As Long, Result As Picture)

Here's the Visual Basic code that calls it. The parameter Own determines whether the Picture will destroy the Icon when it is itself destroyed. Set this to False if you got the hIcon from any of the following sources:

  • LoadIcon, CreateIconFromResource
  • LoadImage, CreateIconFromResourceEx (if you used the LR_SHARED flag)
  • CopyImage (if you used the LR_COPYRETURNORG flag and the original was shared)
  • Any hIcon on which you will call DestroyIcon
Private Function IconToPicture(ByVal hIcon As Long, ByVal Own As Boolean) As Picture
Dim P(1 To 8) As Long
If hIcon = 0 Then Exit Function
P(1) = 16
P(2) = 3 'Icon
P(3) = hIcon
'P(4) n/a
P(5) = &H7BF80980
P(6) = &H101ABF32
P(7) = &HAA00BB8B
P(8) = &HAB0C3000
OleCreatePictureIndirect P(1), P(5), -Own, IconToPicture
End Function

Public domain because it's trivial code. Example:

Private Declare Function LoadCursorW Lib "User32" (ByVal hInstance As Long, ByVal lpName As Long) As Long

Private Sub Form_Load()
MouseIcon = IconToPicture(LoadCursorW(0, 32649), False)
MousePointer = vbCustom
End Sub

This will make your form use the hand cursor.

Advertisement