We help IT Professionals succeed at work.

Make command button picture show up at runtime

ksm asked
Last Modified: 2007-12-19

I'm using Visual Basic 6, SP5.  I'm trying to make a picture show up onto a command button at runtime.

The CommandButton style is set to 1, which is the correct style to show graphics.  The graphic shows up if loaded at design time.  It doesn't show up at runtime, though.  In fact, if there's a graphic already on the button from design time, then it just vanishes when I try to load up another one at runtime.

Here's the code I'm trying to use:

hIcon = ExtractIcon(App.hInstance, MyIcon_OR_ExePath, 0)
If hIcon <> 0 Then DrawIcon PicControl.hdc, 1, 1, hIcon
CommandButton.Picture = picIcon.Picture

The graphic shows up onto the picture control just fine.  Is the problem that I cannot transfer a picture control picture to a commandbutton picture property?  Is there another way to load up the picture?

Looking forward to youre reponses.

Watch Question

1- Inser one Command Bottom (Command1) to Form1
2- Set Properties to:
Style = 1-Grapical
Picture = Browse Your Picture File

3- Run Your Project

Good Luck
- Karaji
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
One thing:

You are drawing to PicControl.hdc but setting the Picture property to picIcon.  Is that a typo or does PicControl hold a reference to picIcon?

Also, after drawing to the picturebox, try copying the image over, e.g.

DrawIcon picIcon.hdc, 1, 1, hIcon
picIcon.Picture = picIcon.Image
CommandButton.Picture = picIcon.Picture

What you need is OleCreatePictureIndicect:

private enum PicTypeEnum
    PICTYPE_NONE             = 0
    PICTYPE_BITMAP           = 1
    PICTYPE_METAFILE         = 2
    PICTYPE_ICON             = 3
end enum

Private Type PICTDESC
     cbSizeofStruct As Long
     picType As PicTypeEnum
     hImage As Long
     xExt As Long
     yExt As Long
End Type

Private Type Guid
     Data1 As Long
     Data2 As Integer
     Data3 As Integer
     Data4(0 To 7) As Byte

End Type

private IID_IPicture as guid

Private Declare Sub OleCreatePictureIndirect Lib "oleaut32.dll" (ByRef lpPictDesc As PICTDESC, ByVal riid As Long, ByVal fOwn As Long, lplpvObj As Any)

dim pDesc as pictdesc
dim oNewPic as picture
dim lResp as long

hIcon = ExtractIcon(App.hInstance, MyIcon_OR_ExePath, 0)
If hIcon <> 0 Then
    with IID_IPicture
        .Data1 = &h7bf80980
        .Data2 = &hbf32
        .Data3 = &h101a
        .Data4(6) =&hc
    end with
    pdesc.cbSizeofStruct = len(pdesc)
    pdesc.picType = PICTYPE_ICON
    pdesc.hImage = hIcon
    lResp = OleCreatePictureIndirect(pdesc, IID_IPicture, 1, oNewPic)
    if (lResp < 0) then
        msgbox "Could not create picture object"
        destroyicon hIcon
        exit sub
    set CommandButton.Picture = oNewPic
end if

That should be enough.  If you have problems, let me know.


Your answer's no good because it describes how to set up the picture at design time.  I needed something to be able to control it at runtime.

Yes, it was a typo.  PicControl and picIcon are indeed one and the same.  picIcon is what I used in my code.  But I changed it after I pasted it into this web site to clarify that I was using a picture control vs. an image or another control.  A typo is what I get for not proof reading my post.  All in all, though, your method was quite similar to what I was already doing and I could not get it to work.

Nice.  It worked.  Key steps that I was not doing were: Using autoredraw on the picture control; refreshing the pic control (using LoadPicture() also clears it and that's what I'm doing since I'm running a loop and resetting the pic control each time so it can accept a new picture); and, finally, I needed to pass the picture control's IMAGE property to it, not the picture property.  Doing those quick tweaks got me rocking and rolling.  Thanks.  I will accept your comment as the answer.

Thanks for your post.  wOlve submitted a working answer first and so I went with that one.  Though, I will say that yours seems a little complex.  The ExtractIcon and DrawIcon API calls seem to do everything I need.  The key for my problem was using picture.autoredraw, picture.refresh and picture.image.

Thanks everybody.  I know you responded quite rapidly and that my response was rather delayed.  For that I apologize.

Happy coding.


Key steps I was missing was setting picture control autoredraw = True; refreshing the picture control and transferring the image property instead of the pic control's picture property.  Thanks.
Just to clarify...
Yes, Autoredraw was what you were missing.  I had assumed you had that property set already.  The Refresh is actually probably not necessary.  The reason you need to use the Image property is because that is where the graphics are actually being drawn.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions