2007-02-16

NEW GRADIENT BACKGROUND CLASS

After all those posts discussing and showing how to create gradient backgrounds for vfp forms, I finally decided to create a simple but powerful class to ease this process.

The GradBackgrounds class is stored inside the gradientobjects.vcx, the class that was published last year that converts any commandbutton, graphical checkbox or graphical optionbox.

Just drop an instance of the class gradbackgrounds to any form, container or pageframe and set some properties to obtain the gradient effects shown below.

All the background images are created using gdi+, but with no helper class, because I wanted to obtain the best performance possible.

 

properties



 
gradientmode - numeric, from 1 to 4, determines the gradient type to be created
1 - horizontal   2 - vertical   3 - diagonal 1   4 - diagonal 2 using linear gradient
brushes with 2 colors

backcolor1 - numeric, the rgb value of the starting color of the gradient background

backcolor2 - numeric, the rgb value of the destination color of the gradient background

reducecolorlevel - numeric, automatically sets the destination color of the gradient (backcolor2) ranging from 0 (no change) to 100 (white). if left to .f., then no change is applied and the original values of backcolor2 and selbackcolor2 will be used.

updatetabcolor - in the case of a pageframe, sets "themes" off to allow the tab background to be in the same color of the gradient

method
update - updates the gradient if you change any of the above properties

 

form using gradientmode = 1

 

 

form using gradientmode = 2

 

 

form using gradientmode = 3

 

 

form using gradientmode = 4

 

 

playing with pageframes

As containers, pageframes allow to set a picture property. we can also obtain a cool effect changing the tab colors to use the same color of the gradient picture.

To make it work, drop an instance of the class inside every page thet you want to convert to gradient. when inside a pageframe, the property gradient mode becomes automatic, and works according to the 'tabOrientation' property of the pageframe.

 

 

 

The gradient adapts to the "taborientation" property of the pageframe

 

 

 

important notes

To make the class work inside a container, don't forget to set its backstyle to 1 - opaque
Don't forget to set the backstyle property of your labels to 0 - transparent
 


 

vfp9 is cool !

In this version of the class I started using some scripts to ease the manipulation of the properties. For example, now if you double click on the backcolor1 property, the color picker opens automatically, to allow you to choose your color. thanks to the great book "what's new in nine", and some great tips from Fabio Lunardon and Tamar Granor.

 

disclaimer

This class is totally free. The information provided on this page and the source code related to this article comes without any warranty whatsoever. use it at your own risk.

 

some related links

gradient backgrounds in your forms with gdi+

gradient command buttons with gdi+

gradient objects with gdi+ revisited

 

 

download

click here to download the latest version of the gradient objects class.

8 comments:

  1. Parabens Cesar.

    Sempre melhorando o que já era ótimo.

    ReplyDelete
  2. OMG, I feel in heaven ... these are just great

    Cesar you're great man ... and FoxPro just ROOOOCKS

    ReplyDelete
  3. Good Stuff as always Cesar.


    Have you solved the problem of loading these images without writing to a file?

    ReplyDelete
  4. Another thought.

    Could we use

    GdipSaveImageToStream

    to write to the USER field in the class or to a blob field in memory?

    Calvin Hsia has an article about this which may help at:

    http://blogs.msdn.com/calvin_hsia/archive/2006/02/17/534529.aspx

    Cheers 

    Bernard
    Off to OzFox next week.
    Hey Bernard,
    Thanks for the feedback. Yep ! I know that article, very inspiring indeed. That will be the probable best way to do it. Calvin needed to create a DLL that needs to be registered using "regsvr". I hope to prepare a sample using it. One of the few things that is still missing in GdiPlusX is the Stream management class. As Bo Durban said, this will probably need a helper FLL for that. In that article, Calvin gives some details about why that is needed. So, I'll leave this part for Bo. As soon as this class is finished, we'll be able to do some great new "miracles" with GDI+.
    Regards
    Cesar

    ReplyDelete
  5. hi cesar,

    I want to use your class to make a similar toolbar office 2003 (blue gradient)
    But i don't reproduce the same gradient.
    Before i could make, but with the class, the color is too white.
    I think to come the property ReduceColorLevel, cause it's not possible to put the value 0.

    i want to make a similar background that the OutlookBar guy with the new gradient background

    Thank's for all
    Olivier

    before i use the old code.

    ** Create a colorObject and store ARGB color values to variables
    LOCAL loClr as GpColor OF HOME() + "classes/_gdiplus.vcx"


    LOCAL lnColor1, lnColor2
    loClr = CREATEOBJECT("gpColor")
    loClr.FoxRGB = lnRGBColor1
    lnColor1 = loClr.ARGB
    loClr.FoxRGB = lnRGBColor2
    lnColor2 = loClr.ARGB

    ** Create a bitmap
    LOCAL loBmp as GpBitmap OF HOME() + "classes/_gdiplus.vcx"
    loBmp = CREATEOBJECT("gpBitmap")
    loBmp.Create(lnWidth, lnHeight)

    ** Get a bitmab graphics object
    LOCAL loGfx as GpGraphics OF HOME() + "classes/_gdiplus.vcx"
    loGfx = CREATEOBJECT("gpGraphics")
    loGfx.CreateFromImage(loBmp)

    ** Declare API
    DECLARE Long GdipCreateLineBrushI IN "gdiplus.dll" ;
      String point1, String point2, ;
      Long color1, Long color2, ;
      Long wrapMode, Long @lineGradient

    ** Get a gradient brush
    LOCAL loBrush as GpBrush OF HOME() + "classes/_gdiplus.vcx"
    LOCAL hBrush && Brush Handle
    hBrush = 0
    GdipCreateLineBrushI(BINTOC(x1,"4rs")+BINTOC(y1,"4rs"), ;
         BINTOC(x2,"4rs")+BINTOC(y2,"4rs"), ;
         lnColor1, lnColor2, 0, @hBrush)
    loBrush = CREATEOBJECT("gpBrush")
    loBrush.SetHandle(hBrush, .T.)


    ** Fill the bitmap with our gradient
    loGfx.FillRectangle(loBrush,0,0,lnWidth, lnHeight)
    loBmp.SaveToFile(.cTempGradFile,"image/bmp")
     
    Hey Olivier,
    For the 2nd color to become a non white, you need to store a .F. to the property "ReduceColorLevel". Please test it, and tell me how it works. In the samples, I added some forms that create non white ending colors, you may want to check there too.
    Hope this helps !
    Saltutations,
    Cesar

    ReplyDelete
  6. Mario Cesar HernandezSeptember 11, 2007 at 5:12 PM

    Hi Cesar;
    I'm using your class, but i have some troubles,whit one form
    this form don't showme the gradient, in the form and container
    in the container   the backstyle =1 and my labels is transparent
    please help me

    best reggards and thaks for all

    pd. Una disculpa por mi ingles
    Hola Mario,
    Perdon por tardar tanto para te contestar. Puedes enviarme tu informe ? Las muestras que vienen junto con "GradObjects" funcionan correctamente ?
    Probablemente, tienes que ajustar alguna de las propiedades de la clase. Puedes intentar de "Paste" la clase directamente de una de las muestras.
    Saludos
    Cesar

    ReplyDelete
  7. Great! I have successfully implemented this in my application and it it looks now much much better than before. Thanks.

    I have observed that If I drag an instance into a container and add command buttons in this container, they are all getting the effect, but if I add any olecontrol (I use menubutton control), it is not affected. Can I do that?

    Vivek
    Hi Vivek,
    I'm glad to know you find it helpful. GradObjects depends on the "Picture" property of the objects from the container. Maybe we can make it work, if your control has the "Picture" property, with some small adjustments in the source code of GradObjects.
    Please create a sample form with your ActiveX, and send to : vfpimaging at hotmail dot com
    Regards
    Cesar

    ReplyDelete