Click to See Complete Forum and Search --> : Picture Boxes / Image Lists / Icons


Jason Teagle
March 14th, 2001, 09:41 AM
I have a situation where I need to build a picture out of a collection of icons, mosaic-style.

My first attempt was to create a form that would have Visible set to True, but be positioned off-screen so user couldn't see it; this form had all the icons I would need in pictures boxes. The main form then grabbed these icons using the picture box's Image property and tried to use the API call DrawIcon(). The problem is, the call failed, even though the hDC was valid, the Image property returned something non-zero, and the position was well within limits.

Question (1a) Why did the call fail? I can only suspect that what the Image property returns is NOT an HICON as required by DrawIcon()...

My second attempt was to take this handle and use it with the API call BitBlt() instead, assuming it was an HBITMAP. Despite creating a memory DC compatible with the display (passing 0, or NULL, to CreateCompatibleDC() ), it failed when I tried to select the HBITMAP(?) into the memory DC for blitting.

Question (1b) Why did the select fail? I assume the handle was not an HBITMAP, or it was not compatible with the DC (which should not be the case, since the DC matched the display!).

Question (1c) Given a picture box with an icon as its Picture property, how can I get an HICON handle from it, if the Image property does not give it as it hinted it did?

My third attempt was to use an image list. I added all the icons to the list(s), and sure enough they show when I DrawIcon() after calling ExtractIcon on the image list entries. Great. The problem is, it is not using the colours present in the icons (which are 16 colour)! It seems to use shades of grey, but not even a proportional shade to the real colours - white comes out as black. I told it NOT to use the mask colour, and I set the mask colour to an unused colour just be sure.

Question (2) Why is it not using my colours? How can I get it to do so?


If it is of any help, I am trying to draw these on a large picture box (AutoRedraw = False). I see no Palette property, so I don't understand why colours are being messed up.

Can anyone help?

Clearcode
March 14th, 2001, 10:01 AM
When an API call fails it often sets the Err.LastDllError member to say why.
You can make sense of this number thus:

'\\ API error decoding
private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (byval dwFlags as Long, lpSource as Any, byval dwMessageId as Long, byval dwLanguageId as Long, byval lpBuffer as string, byval nSize as Long, Arguments as Long) as Long

'\\ -- [ LastSystemError ]------------------------------------------------------------------
'\\ Returns the message from the system which describes the last dll error to occur, as
'\\ held in Err.LastDllError. This function should be called as soon after the API call
'\\ which might have errored, as this member can be reset to zero by subsequent API calls.
'\\ ----------------------------------------------------------------------------------------
'\\ You have a royalty free right to use, reproduce, modify, publish and mess with this code
'\\ I'd like you to visit http://www.merrioncomputing.com for updates, but won't force you
'\\ ----------------------------------------------------------------------------------------
public Function LastSystemError() as string

Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Dim sError as string * 500 '\\ Preinitilise a string buffer to put any error message into
Dim lErrNum as Long
Dim lErrMsg as Long

lErrNum = Err.LastDllError

lErrMsg = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, byval 0&, lErrNum, 0, sError, len(sError), 0)

LastSystemError = Trim(sError)

End Function




Hopefully from this you'll be able to answer the other questions...?

HTH,
Duncan


-------------------------------------------------
Ex. Datis: Duncan Jones
Merrion Computing Ltd
http://www.merrioncomputing.com

Jason Teagle
March 14th, 2001, 11:40 AM
Thanks for this, I will try it when I have some time later (the image list method is working the best so far, so I have to stick to that).

Jason Teagle
March 14th, 2001, 11:43 AM
The problem with the image list method, I have discovered, is that the icon is being merged (probably XORed) with the background. I know this because if I overlay another icon in the same spot, parts revert back to the background colour (where they are the same colour in both icons being drawn). How can I prevent this? This is not the way an icon should be drawn! I think it may be being pulled out of the image list incorrectly (or the ExtractIcon part is going wrong), since I know DrawIcon() normally works OK.