Page 1 of 2
Draw multi-colored text on the form
Posted: Tue Jun 20, 2023 12:54 pm
by gfilatov
Hello guys,
There is a small issue with rendering multi-colored text on the form.
You can get this with a Label control for each letter, but that's very inconvenient.
I prepared a new example using the ON PAINT event to draw colorful text without using any labels.
Please see the result in the image below.

- Color Text
- image.png (8.2 KiB) Viewed 15733 times
Do you think this feature is useful
Your feedback is welcome.
Re: Draw multi-colored text on the form
Posted: Tue Jun 20, 2023 3:08 pm
by Rathinagiri
That is really nice!
Re: Draw multi-colored text on the form
Posted: Tue Jun 20, 2023 3:57 pm
by serge_girard
Great !
Serge
Re: Draw multi-colored text on the form
Posted: Tue Jun 20, 2023 4:23 pm
by gfilatov
Rathinagiri wrote: ↑Tue Jun 20, 2023 3:08 pm
That is really nice!
Thanks for your feedback.
The main logic is below:
Code: Select all
#include "hmg.ch"
#include "i_winuser.ch"
*------------------------------------------------------------------------------*
FUNCTION Main
*------------------------------------------------------------------------------*
LOCAL cText := "MULTI-COLOR TEXT"
LOCAL hFont
hFont := GetFontHandle( "Font_Text" )
IF hFont == 0
DEFINE FONT Font_Text FONTNAME "IMPACT" SIZE 48
ENDIF
hFont := GetFontHandle( "Font_Text" )
SET EVENTS FUNCTION TO App_OnEvents
DEFINE WINDOW Form_Main ;
AT 0, 0 ;
WIDTH 600 HEIGHT 400 ;
TITLE cText ;
MAIN ;
ON SIZE InvalidateRect( This.Handle, 0 ) ;
ON MAXIMIZE InvalidateRect( This.Handle, 0 ) ;
ON PAINT App_OnPaint( This.Handle, hFont, cText )
DEFINE STATUSBAR FONT "Arial" SIZE 12
STATUSITEM "Color Text" BACKCOLOR HMG_n2RGB( GetSysColor( COLOR_GRADIENTINACTIVECAPTION ) )
END STATUSBAR
ON KEY ESCAPE ACTION ThisWindow.Release
END WINDOW
CENTER WINDOW Form_Main
ACTIVATE WINDOW Form_Main
RETURN NIL
#define DT_SINGLELINE 32
#define DT_CALCRECT 1024
*------------------------------------------------------------------------------*
FUNCTION App_OnPaint( hWnd, hFont, cText )
*------------------------------------------------------------------------------*
LOCAL aColors := { ;
METRO_LIME, METRO_GREEN, METRO_EMERALD, METRO_TEAL, METRO_CYAN, ;
METRO_COBALT, METRO_INDIGO, METRO_VIOLET, METRO_PINK, METRO_MAGENTA, ;
METRO_CRIMSON, METRO_RED, METRO_ORANGE, METRO_AMBER, METRO_YELLOW, ;
METRO_BROWN, METRO_OLIVE, METRO_STEEL, METRO_MAUVE, METRO_TAUPE }
LOCAL aRect := { 0, 0, 0, 0 }
LOCAL c, n, hDC, nRight, bk
GetClientRectArea( hWnd, aRect )
// Center Statusbar Text
Form_Main.Statusbar.Item( 1 ) := PadC( "Color Text", aRect[ 4 ] / 4 )
hDC := GetDC( hWnd )
// Center Text
n := DrawTextEx( hDC, cText, aRect, DT_SINGLELINE + DT_CALCRECT, hFont, 0, @nRight )
aRect[ 1 ] += ( aRect[ 3 ] - aRect[ 1 ] - n ) / 2
aRect[ 2 ] += Int( ( aRect[ 4 ] - nRight ) / 2 )
// Paint Text
bk := SetBkMode( hDC, 1 )
FOR n := 1 TO Len( cText )
c := SubStr( cText, n, 1 )
DrawTextEx( hDC, c, aRect, DT_SINGLELINE + DT_CALCRECT, hFont, aColors[ n ], @nRight )
DrawTextEx( hDC, c, aRect, DT_SINGLELINE, hFont, aColors[ n ] )
aRect[ 2 ] := nRight
NEXT
SetBkMode( hDC, bk )
ReleaseDC( hWnd, hDC )
RETURN NIL
I've also attached a demo executable for your review.

Re: Draw multi-colored text on the form
Posted: Tue Jun 20, 2023 5:59 pm
by Anand
Beautiful !
Thanks
Re: Draw multi-colored text on the form
Posted: Tue Jun 20, 2023 6:17 pm
by franco
Very useful.
Thanks
Re: Draw multi-colored text on the form
Posted: Tue Jun 20, 2023 9:29 pm
by Red2
Thank you Gregory,
If you decide to provide a complete source code example I would very much like to learn from it.
Thanks again!
Red2
Re: Draw multi-colored text on the form
Posted: Wed Jun 21, 2023 6:12 am
by serge_girard
Grigory,
Also possible to resize on RESIZE?
Serge
Re: Draw multi-colored text on the form
Posted: Wed Jun 21, 2023 6:52 am
by gfilatov
serge_girard wrote: ↑Wed Jun 21, 2023 6:12 am
Grigory,
Also possible to resize on RESIZE?
Serge,
Yes, sure.
The text will be center-aligned to the height and width of the form's client area.
You can try the demo application above in your environment.
Thanks for your interest.
Re: Draw multi-colored text on the form
Posted: Wed Jun 21, 2023 7:01 am
by gfilatov
Red2 wrote: ↑Tue Jun 20, 2023 9:29 pm
Thank you Gregory,
If you decide to provide a complete source code example I would very much like to learn from it.
Thanks again!
Red2
Dear Spencer,
The full source code for this example will be available in the next MiniGUI update.
Thank you for your attention. I really appreciate it.