Custom Painting

Started by Richard Kelly, February 07, 2015, 06:41:23 AM

Previous topic - Next topic

Richard Kelly

On my main form, I am painting a gradient filled rectangle at the top across the whole client area.

I want to put some label controls in that area. In the labels custom paint function, how can I write out the text without disturbing the gradient background that is already in place and use the font/font color specified in the designer? I have the windows transparency option set so the label is painted after the main form.


Rick Kelly

Paul Squires

Hi Rick,

Over the years I have ran into this same problem you are trying to accomplish. In the end, it is just too hard to use the FF Label control to accomplish this because it is really a faked "transparency" that FF uses. When that property is set it simply matches the background color of the form to the label rather than providing true transparency.

If I remember correctly, I usually resorted to drawing the gradient and then manually drawing the text using api calls like the DrawText api.
Paul Squires
PlanetSquires Software

Richard Kelly

Quote from: TechSupport on February 07, 2015, 11:29:15 AM
Hi Rick,

Over the years I have ran into this same problem you are trying to accomplish. In the end, it is just too hard to use the FF Label control to accomplish this because it is really a faked "transparency" that FF uses. When that property is set it simply matches the background color of the form to the label rather than providing true transparency.

If I remember correctly, I usually resorted to drawing the gradient and then manually drawing the text using api calls like the DrawText api.

The strange thing is, I can get "close" by adding WS_EX_TRANSPARENT to the control and setting the associated FLY_DATA background brush to NULL via GetStockObject (%NULL_BRUSH) in the forms CREATE function. By "close" I mean it looks right but the darn text occasionally disappears as I'm clicking around the form. I guess I can live with using a solid color.

Rick Kelly

Richard Kelly

I couldn't stand giving up on my nice pretty gradients. Does anybody see any problems with this code in the custom paint function for the label control? It seems to work.


Local ff    As FLY_DATA Ptr
Local hDC   As Dword
Local ps    As PAINTSTRUCT
Local sText As String

    ff = GetProp (hWndControl, "FLY_PTR")
    sText = FF_Control_GetText(hWndControl)
    hDC = BeginPaint(hWndControl,ps)
   
    SelectObject(hDC,GetStockObject (%HOLLOW_BRUSH))
    SetBkMode hDC, %TRANSPARENT
    FillRect(hDC,ps.rcPaint,GetStockObject(%HOLLOW_BRUSH))
   
    SelectObject(hDC,@ff.hFont)
    SetTextColor(hDC,@ff.nForeColor)
   
    DrawText(hDC,ByVal StrPtr(sText),Len(sText),ps.rcPaint,%DT_LEFT Or %DT_SINGLELINE Or %DT_TOP)
   
    EndPaint(hWndControl,ps)
    ReleaseDC(hWndControl,hDC)
   
    Function = %TRUE


Rick Kelly

Richard Kelly

#4
Quote from: Richard Kelly on February 07, 2015, 09:24:34 PM
I couldn't stand giving up on my nice pretty gradients. Does anybody see any problems with this code in the custom paint function for the label control? It seems to work.

Well, there is a problem. Anytime I move the mouse over any part of the form that could get focus, the label text disappears. I had to add FF_Control_Redraw for each label AFTER my custom form background painting.

I noticed a few extraneous things in the paint function - I didn't need to select a brush into the DC and EndPaint releases the DC so I didn't need to do that.


' Custom Paint for Label control

Local ff    As FLY_DATA Ptr
Local hDC   As Dword
Local ps    As PAINTSTRUCT
Local sText As String

    ff = GetProp (hWndControl, "FLY_PTR")
    sText = FF_Control_GetText(hWndControl)
    hDC = BeginPaint(hWndControl,ps)
   
    SetBkMode hDC, %TRANSPARENT
    FillRect(hDC,ps.rcPaint,GetStockObject(%HOLLOW_BRUSH))
   
    SelectObject(hDC,@ff.hFont)
    SetTextColor(hDC,@ff.nForeColor)
   
    DrawText(hDC,ByVal StrPtr(sText),Len(sText),ps.rcPaint,%DT_LEFT Or %DT_SINGLELINE Or %DT_TOP)
   
    EndPaint(hWndControl,ps)

    Function = %TRUE


Rick Kelly