Problem displaying icons from resource file - IconBug.zip 
Author Message
 Problem displaying icons from resource file - IconBug.zip

Gertjan...

Quote:
>  Can anyone explain this strange behaviour?  More importantly, does
>anyone know how to work around it?  I am using icons because of the
>transparancy, and I'm painting a lot of different icons on one picture
>box, so using .bmp/maskcolor would mean a lot of overhead.

You're right, it appears to be a bug. I first ran into this several weeks
ago, and did find a workaround: Don't use LoadResPicture to get the picture
from your resource cache. Instead, use the LoadImage API to get an hIcon (or
hBitmap) for your resource, and draw it some other way (such as DrawIconEx,
etc.). The drawback is that resource API calls work *only* in the compiled
executable, not in the IDE... so if for some reason you really need to see
the images in the IDE, you'll have to do some checking in your app to see
whether you're in the IDE or not... if so, use LoadResPicture, and if not,
use LoadImage.

--
Ben Baird, MVP
Visual Basic Thunder
http://www.*-*-*.com/
Common Controls Replacement Project, Official Member
http://www.*-*-*.com/
Please direct your programming questions to the newsgroups.



Thu, 18 Oct 2001 03:00:00 GMT  
 Problem displaying icons from resource file - IconBug.zip
On Sun, 2 May 1999 06:47:17 -0600, "Ben Baird (MSMVP)"

Quote:

>You're right, it appears to be a bug. I first ran into this several weeks
>ago, and did find a workaround: Don't use LoadResPicture to get the picture
>from your resource cache. Instead, use the LoadImage API to get an hIcon (or
>hBitmap) for your resource, and draw it some other way (such as DrawIconEx,
>etc.).

  The problem with this approach (for me) is that I use/paint the icons
in many different places. I load them at program startup in StdPicture
objects, and would really like to keep the same basic structure.  I
don't mind making the module loading the icons a little more difficult
if I don't have to reprogram everything else.  Basically, what I'm
asking is: is it possible to create or fill a StdPicture object from the
images (hIcon) returned from the LoadImage API?

Quote:
>The drawback is that resource API calls work *only* in the compiled
>executable, not in the IDE... so if for some reason you really need to see
>the images in the IDE, you'll have to do some checking in your app to see
>whether you're in the IDE or not... if so, use LoadResPicture, and if not,
>use LoadImage.

  That would not be a problem for me.

  Thanks for your help,
  Gertjan.

--

The Boot Control home page: http://www.xs4all.nl/~gklein/bcpage.html



Sat, 20 Oct 2001 03:00:00 GMT  
 Problem displaying icons from resource file - IconBug.zip
Gertjan...

Quote:
>  The problem with this approach (for me) is that I use/paint the icons
>in many different places. I load them at program startup in StdPicture
>objects, and would really like to keep the same basic structure.  I
>don't mind making the module loading the icons a little more difficult
>if I don't have to reprogram everything else.  Basically, what I'm
>asking is: is it possible to create or fill a StdPicture object from the
>images (hIcon) returned from the LoadImage API?

Yes. Use the following declarations and code to do it:

Public Type GUID
  Data1 As Long
  Data2 As Integer
  Data3 As Integer
  Data4(7) As Byte
End Type
Public Type PICTDESC
    cbSizeOfStruct As Long
    picType As Long
    hgdiobj As Long
    hgdipal As Long
    dwReserved As Long
End Type

Public Declare Function OleCreatePictureIndirect Lib "olepro32.dll"
(lpPictDesc As PICTDESC, riid As GUID, ByVal fPictureOwnsHandle As Long,
IPic As IPicture) As Long

Public Function PictureFromhIcon(ByVal hIco As Long) As StdPicture
Dim IPic As IPicture
Dim picdes As PICTDESC
Dim TIID_IDispatch As GUID
'Fill picture description
picdes.picType = vbPicTypeIcon
picdes.hgdiobj = hIco
picdes.hgdipal = 0
picdes.cbSizeOfStruct = Len(picdes)

'Fill in IDispatch GUID
With TIID_IDispatch
   .Data1 = &H20400
   .Data4(0) = &HC0
   .Data4(7) = &H46
End With
'Create picture from icon handle
OleCreatePictureIndirect picdes, TIID_IDispatch, 1, IPic

Set PictureFromhIcon = IPic

End Function

--
Ben Baird, MVP
Visual Basic Thunder
http://www.vbthunder.com
Common Controls Replacement Project, Official Member
http://www.mvps.org/ccrp/
Please direct your programming questions to the newsgroups.



Sat, 20 Oct 2001 03:00:00 GMT  
 Problem displaying icons from resource file - IconBug.zip
On Tue, 4 May 1999 06:31:35 -0600, "Ben Baird (MSMVP)"

Quote:

>>Basically, what I'm
>>asking is: is it possible to create or fill a StdPicture object from the
>>images (hIcon) returned from the LoadImage API?

>Yes. Use the following declarations and code to do it:

[Snip code]

  Excellent, that works!  I was thinking about creating the icon using
LoadResPicture and then replacing the actual bitmap with the one loaded
with LoadPicture, but I got stuck due to the less than adequate info in
Appleman's VBPGW32API.  However, this is much more elegant - and I dare
say so even though I don't understand what you're doing ;-)  Would you
care to elaborate on how this code works, and more importantly, where
you got the information required to create it?  I have a (probably
outdated) version of the Win32 SDK, but it doesn't mention IPicture or
OleCreatePictureIndirect.

  Thanks very much for your help,
  Gertjan.

--

The Boot Control home page: http://www.xs4all.nl/~gklein/bcpage.html



Sun, 21 Oct 2001 03:00:00 GMT  
 Problem displaying icons from resource file - IconBug.zip
Gertjan...

Quote:
>  Excellent, that works!  I was thinking about creating the icon using
>LoadResPicture and then replacing the actual bitmap with the one loaded
>with LoadPicture, but I got stuck due to the less than adequate info in
>Appleman's VBPGW32API.

I don't consider Appleman's API book very impressive, even though many seem
to think it is a wonderful resource :-(

Quote:
>However, this is much more elegant - and I dare
>say so even though I don't understand what you're doing ;-)  Would you
>care to elaborate on how this code works, and more importantly, where
>you got the information required to create it?  I have a (probably
>outdated) version of the Win32 SDK, but it doesn't mention IPicture or
>OleCreatePictureIndirect.

If your copy of the SDK doesn't cover this function then it's *definitely*
out of date :-) You can find this and all other Ole* functions in the MSDN
library at http://msdn.microsoft.com -- which is always up-to-date.

As far as I know, Visual Basic actually uses this function to load bitmaps
into its own StdPicture objects. It's a standard OLE/COM function that is
made available to anyone who wishes to use it. Another similar function is
called OleCreateFontIndirect, which creates an StdFont object -- although
this one may not be as useful for VB programmers, since you can simply Dim x
As New StdFont.

Anyway, basically all this function does is take a bitmap handle (or
metafile handle) and convert it to a COM StdPicture object of type Bitmap,
Icon or Metafile. The parameters of this function are defined in MSDN as
follows:

==============================
Parameters

pPictDesc
[in] Pointer to a caller-allocated structure containing the initial state of
the picture.

riid
[in] Reference to the identifier of the interface describing the type of
interface pointer to return in ppvObj.

fOwn
[in] If TRUE, the picture object is to destroy its picture when the object
is destroyed. If FALSE, the caller is responsible for destroying the
picture.

ppvObj
[out] Address of pointer variable that receives the interface pointer
requested in riid. Upon successful return, *ppvObj contains the requested
interface pointer on the newly created object. If the call is successful,
the caller is responsible for calling Release through this interface pointer
when the new object is no longer needed. If the call fails, the value of
*ppvObj is set to NULL.
==============================

If you're not familiar with the inner workings of COM then a lot of this may
be gibberish to you. Suffice it to say that you can use it for *any* hBitmap
or hIcon to get the StdPicture object that you need.

An IPicture is really only slightly different from an StdPicture object in
that the IPicture is an interface rather than an object. Again, without some
knowledge of the difference between interfaces and objects, this really
shouldn't matter to you at all -- Visual Basic can easily switch between the
two data types without a problem.

--
Ben Baird, MVP
Visual Basic Thunder
http://www.vbthunder.com
Common Controls Replacement Project, Official Member
http://www.mvps.org/ccrp/
Please direct your programming questions to the newsgroups.



Sun, 21 Oct 2001 03:00:00 GMT  
 Problem displaying icons from resource file - IconBug.zip

Quote:
>I don't consider Appleman's API book very impressive, even though many
>seem to think it is a wonderful resource :-(

If you have a better resource in mind, I would like to go and buy it. If
you don't want to recommend in a newsgroup would you please send me the

Thanks

Charles Kincaid



Sun, 21 Oct 2001 03:00:00 GMT  
 Problem displaying icons from resource file - IconBug.zip
On Wed, 5 May 1999 12:37:20 -0600, "Ben Baird (MSMVP)"

Quote:

>I don't consider Appleman's API book very impressive, even though many seem
>to think it is a wonderful resource :-(

  One of the most annoying things of this book to me is that often he
doesn't tell you how to do something, but tells you to buy his company's
products instead.  He also seems to leave out a lot of important
information (why is RtlMoveMemory not in his book, for example?).

Quote:
>If your copy of the SDK doesn't cover this function then it's *definitely*
>out of date :-)

  It's on the MSDN CD that came with the initial distribution of W95, so
it's a few years old ;-)  I'll download the current version as soon as I
have a faster Internet connection.

Quote:
>The parameters of this function are defined in MSDN as
>follows:
[...]
>riid
>[in] Reference to the identifier of the interface describing the type of
>interface pointer to return in ppvObj.

  This is the one I had trouble with, the others I had guessed.  Why do
you pass the IDispatch GUID here - or maybe I should ask, what else
could you pass?  Can two different StdPicture objects have different
interfaces?

Quote:
>An IPicture is really only slightly different from an StdPicture object in
>that the IPicture is an interface rather than an object. Again, without some
>knowledge of the difference between interfaces and objects, this really
>shouldn't matter to you at all -- Visual Basic can easily switch between the
>two data types without a problem.

  I'm not nearly as familiar with COM as I'd like to be. I've started
reading "Essential COM" (Box), but I've only finished the first chapter
which basically explains why COM exists.  Unfortunately for me the book
is C++ orientated, a language I don't know very well, so it's hard
reading ;-)

  Anyway, as far as I know an interface is how an object communicates
with the outside world (i.e. a set of properties/methods, collectively
given a name and GUID).  So, objects can have one or more interfaces,
but the interface itself isn't a real thing, it is just a description of
how to communicate with a given object.  Am I far off?

  Gertjan
.
--

The Boot Control home page: http://www.xs4all.nl/~gklein/bcpage.html



Wed, 24 Oct 2001 03:00:00 GMT  
 Problem displaying icons from resource file - IconBug.zip
Gertjan...

Quote:
>  One of the most annoying things of this book to me is that often he
>doesn't tell you how to do something, but tells you to buy his company's
>products instead.  He also seems to leave out a lot of important
>information (why is RtlMoveMemory not in his book, for example?).

Yes, that is one of the many weak points that the book has.

Quote:
>>riid
>>[in] Reference to the identifier of the interface describing the type of
>>interface pointer to return in ppvObj.

>  This is the one I had trouble with, the others I had guessed.  Why do
>you pass the IDispatch GUID here - or maybe I should ask, what else
>could you pass?  Can two different StdPicture objects have different
>interfaces?

There aren't many different choices of GUIDs you could pass here. I'm
assuming that this parameter was included to allow for future expansion of
the function, e.g. different types of pictures. The IID of IPicture or
StdPicture also works, as I recall. Passing the IID for IUnknown probably
works as well, although I haven't tried that one. And no, StdPicture objects
always expose the same interface.

Quote:
>  I'm not nearly as familiar with COM as I'd like to be. I've started
>reading "Essential COM" (Box), but I've only finished the first chapter
>which basically explains why COM exists.  Unfortunately for me the book
>is C++ orientated, a language I don't know very well, so it's hard
>reading ;-)

I read Inside COM, which is also C++-oriented, but I know enough C++ to see
pretty well what's going on. I'm currently reviewing another book called VB
COM from Wrox Press (written by Thomas Lewis), and it seems to be a good
introduction to COM from a VB point of view.

Quote:
>  Anyway, as far as I know an interface is how an object communicates
>with the outside world (i.e. a set of properties/methods, collectively
>given a name and GUID).  So, objects can have one or more interfaces,
>but the interface itself isn't a real thing, it is just a description of
>how to communicate with a given object.  Am I far off?

That's essentially the way it is. An interface is simply a "diagram" of
functions in memory, while an object actually provides the functionality
behind the interface.

--
Ben Baird, MVP
Visual Basic Thunder
http://www.vbthunder.com
Common Controls Replacement Project, Official Member
http://www.mvps.org/ccrp/
Please direct your programming questions to the newsgroups.



Wed, 24 Oct 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Help with icon files. - icons.zip (0/1)

2. Help with icon files. - icons.zip (1/1)

3. Displaying GIF files from a resource file???

4. Reading files from a zip file (Just reading the filenames, not unzipping or zipping)

5. Problems displaying Japanese text from a resource DLL

6. Problems displaying Japanese text from a resource DLL

7. Resource files and Icons

8. Adding Bitmaps or Icons to a Toolbar from a Resource File

9. Executable Icon from Resource Files

10. Icons in Resource File

11. PictureBox, Resource File and 16 X 16 Icons

12. Icons From a Resource File

 

 
Powered by phpBB® Forum Software