Wednesday, June 6, 2012

Transparent Grids - Now & Then


I have been wondering whether I can make my grid transparent and indeed I found a way.  However, said transparency can only be attained when there is no RecordSource and yes it suits me just fine because who, in his right mind, would want a transparent grid with data?  That is not only painful to the eyes but is likewise annoying, LOL!


Here is a sample how we can achive that:



Local oForm
oForm=Newobject("Form1")
oForm.Show
Read Events
Return

Define Class Form1 As Form
      Height = 460
      Width = 615
      AutoCenter = .T.
      Caption = 'Temporary Transparent Grids'
      Picture = Addbs(Getenv("windir"))+"Soap Bubbles.bmp"
      ShowTips = .T.

      Add Object grid1 As Grid With ;
            GridLines = 0, ;
            Height = 328, ;
            Left = 10, ;
            Top = 50, ;
            Width = 590,;
            GridLines = 3,;
            DeleteMark = .F.,;
            GridLineColor = Rgb(192,192,192),;
            ScrollBars = 2

      Add Object label1 As Label With ;
            top = 15,;
            left = 10,;
            caption = 'Search',;
            Backstyle = 0,;
            ForeColor = Rgb(255,255,255)

      Add Object Text1 As TextBox With ;
            top = 10,;
            left = 55,;
            Height = 23,;
            Width = 300,;
            value = '',;
            ToolTipText = 'Start typing to search matching records,'+;
            ' Double-click to clear search'

      Add Object Shape1 As Shape With ;
            top = 400,;
            left = 10,;
            Height = 50,;
            Width = 590

      Add Object label2 As Label With ;
            top = 410,;
            left = 15,;
            Height = 36,;
            caption = 'This grid shows transparency without resorting to'+;
            ' API calls or any trick outside of VFP box.  However, '+;
            'transparency effect is only temporary! Find out why!',;
            WordWrap = .T.,;
            Width = 570

      Procedure Load
            Set Talk Off
            Set Safety Off
            Close Databases All
            Select cust_id, company, contact From ;
                  (Home(2)+"data\customer") Where .F. ;
                  Into Cursor junk1 Readwrite
      Endproc

      Procedure Init

            This.grid1.RecordSource = ''
            This.grid1.LockColumns = 3
      Endproc

      Procedure Text1.DblClick
            This.Value = ''
            Thisform.grid1.RecordSource = ''
            Thisform.grid1.RecordSourceType = 1
            Thisform.grid1.Refresh
      Endproc

      Procedure Text1.InteractiveChange
            Local lcSearch
            lcSearch = Upper(Alltrim(This.Value))
            If Empty(m.lcSearch)
                  Thisform.grid1.RecordSource = ''  && Remove binding
            Else
                  Select cust_id, company, contact From ;
                        Home(2)+"data\customer" ;
                        Where Upper(cust_id+company+contact);
                        LIKE '%'+m.lcSearch+'%' Into Cursor junk2 NOFILTER

                  Select junk1
                  Zap In junk1
                  Append From Dbf('Junk2')
                  Go Top
                  Thisform.grid1.RecordSource = 'junk1'
            Endif
            Thisform.grid1.Refresh
      Endproc

      Procedure Destroy
            Clear Events
      Endproc

Enddefine



The trick, in case you haven't noticed,  lies between two properties of the grid, i.e.:

1.  RecordSource (should be empty)
2.  LockColumns - here is the other secret if you haven't noticed in the code.  The columns locked will be transparent while those that are not locked cannot be.  In the above sample, I locked all columns.

Well, I hope you may find this trick worth your time.  Whether you will do this approach or not, I believe that this is a plus onto your grid insight.

For a trick of Grid Watermark effect, you may be interested in Bernard Bout's trick: 
http://weblogs.foxite.com/bernardbout/2009/03/03/decorating-a-grid-with-gdi/

Cheers!

No comments:

Post a Comment