Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Transparent GIF images
Message
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01114632
Message ID:
01138011
Views:
22
Robin, Joel,

I have used this method in the past, but then changed it due to a caveat I came across. If you have one image on two different buttons on the same form, it won't work becuase when you try to clear the resources of the image for either button, the other button has a reference to it and the clear resources fails (without -- error by design). The approach I moved to was to maintain a text file (built into the project) containing a list of file names of all images containing transparency. In the main program, I create a collection of image objects with an image per object. Then, after the clear events, I release them. The text file is even auto-generated when the main program is run from the IDE. It works well and prevents the necessity of having multiple copies of the same image (with different names) to be able to use the same image more than once on a form.

Here's the code I use in my main program. First, this creates the text file (we only use PNGs so you'd have to modify it a bit):
IF VERSION(2) > 0                    && AND llDevelopment
	*JCJB*	09/14/2005 - In Development, (re)create the PNGs.txt file.
	LOCAL	lcFile as String, ;
			lnX as Integer, ;
			llSafety as Boolean

	*JCJB*	09/14/2005 - If this errors (most likely due to the project not
	*JCJB*	09/14/2005 - being opened), it's no big deal. Ignore the error.
	TRY
		lcFile = ''

		*JCJB*	09/14/2005 - Gather the names of all of the PNGs in the project.
		FOR lnX = 1 TO _VFP.ActiveProject.Files.Count
			IF LOWER(RIGHT(_VFP.ActiveProject.Files(lnX).Name, 4)) == '.png'
				lcFile = lcFile + _VFP.ActiveProject.Files(lnX).Name + CHR(13)
			ENDIF
		ENDFOR

		*JCJB*	09/14/2005 - Save the current state of Safety.
		llSafety = (SET('Safety') == 'ON')

		*JCJB*	09/14/2005 - Turn Safety off and overwrite the PNGs file.
		SET SAFETY OFF
		STRTOFILE(lcFile, 'PNGs.txt')

		*JCJB*	09/14/2005 - Turn Safety back on if it was on.
		IF llSafety
			SET SAFETY ON
		ENDIF

		*JCJB*	09/14/2005 - Add the PNGs file to the project.
		_VFP.ActiveProject.Files.Add('PNGs.txt')
	CATCH
	ENDTRY
ENDIF
Here are the load/unload functions:
FUNCTION loadImages() as Collection
	LOCAL ARRAY laImages[1]

	LOCAL	llTalk as Boolean, ;
			lnX as Integer, ;
			loImages as Collection

	*JCJB*	10/06/2005 - Save the current Talk setting and turn Talk off.
	llTalk = (SET('Talk') == 'ON')
	SET TALK OFF

	*JCJB*	09/14/2005 - Initialize the variable to null.
	loImages = null

	*JCJB*	09/14/2005 - See if a file containing the names of the PNGs exists.
	IF FILE('PNGs.txt')
		*JCJB*	09/14/2005 - Clear Resources to free any cached PNG images.
		CLEAR RESOURCES

		*JCJB*	09/14/2005 - Create a collection to hold PNG images.
		loImages = NEWOBJECT('Collection')

		*JCJB*	09/14/2005 - Iterate the file names in PNGs.txt
		*JCJB*	09/14/2005 - For each file, add an Image object to the collection.
		FOR lnX = 1 TO ALINES(laImages, FILETOSTR('PNGs.txt'), 1)
			loImages.Add(NEWOBJECT('Image'))
			loImages[loImages.Count].Picture = laImages[lnX]
		ENDFOR
	ENDIF

	*JCJB*	10/06/2005 - Restore the previous Talk setting.
	IF llTalk
		SET TALK ON
	ENDIF

	*JCJB*	09/14/2005 - Return the Image collection.
	RETURN loImages
ENDFUNC

FUNCTION unloadImages(toImages as Collection)
	*JCJB*	09/14/2005 - See if an Image Collection was passed.
	IF VARTYPE(toImages) == 'O' AND LOWER(toImages.BaseClass) == 'collection'
		*JCJB*	09/14/2005 - Release all of the Image objects.
		DO WHILE toImages.Count > 0
			toImages.Remove(1)
		ENDDO
	ENDIF
ENDFUNC
And here is how it's called:
*JCJB*	09/14/2005 - Create a collection containing the project's PNG files.
loImages = loadImages()

* Here is where your application loop goes.

*JCJB*	09/14/2005 - Release the collection since the app is done.
unloadImages(loImages)
HTH,
Chad

>Hi Joel
>
>Thats a much cleaner way of solving the problem.
>
>Thx
>
>>FYI, I came up with a generic solution to this problem. Put the code below in FixTransparency.prg, then call FixTransparency(This) from the Init() of your CommandButton (or any other applicable) class. Works like a charm!
>>
>>* Fix Image Transparency
>>* VFP/GDI+ has a bug where image transparency is not respected on buttons
>>* The solution is to load the picture in an image control first
>>* This program clears the image from cache and reloads it in an image control
>>Lparameters loControl as CommandButton
>>Local lcPicture, loImage as Image
>>
>>* Only applies to formats that support transparency
>>lcPicture = loControl.Picture
>>If Empty(lcPicture) or !InList(Upper(JustExt(lcPicture)),"JPG","PNG","GIF")
>>    Return
>>EndIf
>>
>>* Clear image from cache
>>loControl.Picture = ""
>>Clear Resources (lcPicture)
>>
>>* Reload picture in Image control
>>loImage = CreateObject("Image")
>>loImage.Picture = lcPicture
>>
>>* Reset Picture on control
>>loControl.Picture = lcPicture
>>
>>
>>>Hi!
>>>
>>>How do I get foxpro to use my transparent GIF image in my button , IE it has a white background but when I open it in my graphics package, it shows no background & tells me it is transparent?
>>>
>>>Any help extremley appreciated
>>>
>>>TAI
_________________________________
There are 2 types of people in the world:
    Those who need closure
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform