Draw multi-colored text on the form

HMG Samples and Enhancements

Moderator: Rathinagiri

User avatar
gfilatov
Posts: 1069
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Draw multi-colored text on the form

Post 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. :arrow:
Color Text
Color Text
image.png (8.2 KiB) Viewed 10713 times
Do you think this feature is useful :?:

Your feedback is welcome.
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Draw multi-colored text on the form

Post by Rathinagiri »

That is really nice!
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
serge_girard
Posts: 3167
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Draw multi-colored text on the form

Post by serge_girard »

Great !

Serge
There's nothing you can do that can't be done...
User avatar
gfilatov
Posts: 1069
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Re: Draw multi-colored text on the form

Post 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. :arrow:
Attachments
demo.zip
Demo executable
(913.99 KiB) Downloaded 129 times
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
User avatar
Anand
Posts: 595
Joined: Tue May 24, 2016 4:36 pm
DBs Used: DBF

Re: Draw multi-colored text on the form

Post by Anand »

Beautiful !

Thanks
Regards,

Anand

Image
franco
Posts: 821
Joined: Sat Nov 02, 2013 5:42 am
DBs Used: DBF
Location: Canada

Re: Draw multi-colored text on the form

Post by franco »

Very useful.
Thanks
All The Best,
Franco
Canada
Red2
Posts: 273
Joined: Sat May 18, 2019 2:11 pm
DBs Used: Visual FoxPro, FoxPro
Location: United States of America

Re: Draw multi-colored text on the form

Post 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
User avatar
serge_girard
Posts: 3167
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Draw multi-colored text on the form

Post by serge_girard »

Grigory,

Also possible to resize on RESIZE?

Serge
There's nothing you can do that can't be done...
User avatar
gfilatov
Posts: 1069
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Re: Draw multi-colored text on the form

Post 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.
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
User avatar
gfilatov
Posts: 1069
Joined: Fri Aug 01, 2008 5:42 am
Location: Ukraine
Contact:

Re: Draw multi-colored text on the form

Post 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.
Kind Regards,
Grigory Filatov

"Everything should be made as simple as possible, but no simpler." Albert Einstein
Post Reply