PB3.20 - Set IntercharacterSpacing for Text

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

PB3.20 - Set IntercharacterSpacing for Text

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by MrVainSCL.

Hi @ all
Maybe you wanted to print a word or text on your window/screen with some more pixel spacing between any char? So here is a small example how to change the pixel space between your text. Works for any font! Please dont blame me about the source... Its not really the best methode, but still works

Code: Select all

;-----------------------------------
;
; Set IntercharacterSpacing for Text
;
; (27-Jun-2002 - 07:38am)
;
;-----------------------------------
;
    winw = 300                                            ; setup window width
    winh = 100                                            ; setup window height
    ;   
    xpos = (GetSystemMetrics_(#SM_CXSCREEN)-winw) / 2     ; only to center setup window on desktop
    ypos = (GetSystemMetrics_(#SM_CYSCREEN)-winh) / 2     ; only to center setup window on desktop
    ;       
    ;-------- Define the #Flags for our opening Window --------
    ;
    #Win_Flag1  = #PB_Window_SystemMenu
    #Win_Flag2  = #PB_Window_MinimizeGadget
    #Win_Flags  = #Win_Flag1|#Win_Flag2
    ;
    ;-------- Define some other stuff --------
    ;
    text.s    = "PureBasic"                               ; Replace by any text                           
;       
;======== Init all the needed system stuff ========
;
    hWnd = OpenWindow (0, xpos, ypos, winw, winh, "Set InterCharSpacing Example", #Win_Flags)
    ;
    If hWnd = 0
        MessageBox_ (0,"Could not open Window", "Set InterCharSpacing Example", #MB_ICONINFORMATION|#MB_OK)
        End
    EndIf
;
;----------------------------------------
; M a i n l o o p
;----------------------------------------
;
    Repeat
        EventID = WaitWindowEvent()
        ;
        Select EventID
            Case #PB_Event_CloseWindow
              quit = 1
        EndSelect     
        ;
        ;-------- Clear Window --------
        ;
        StartDrawing(WindowOutput(0))                                    ; You can use API                 
           Box (0,0,winw,winh,RGB(192,192,192))                         ; for clearing the
        StopDrawing()                                                   ; window too...
        ;
        ;-------- Move our Text --------
        ;           
        If movetext = 0 : charspace=charspace + 1 : EndIf              ; set pixel spacing
        If movetext = 1 : charspace=charspace - 1 : EndIf              ; from low to high
        ;                                                               ; and then back
        If charspace = 20 : movetext = 1 : EndIf                 
        If charspace =  0 : movetext = 0 : EndIf   
        ;
        ;-------- Print Text --------
        ;
        Gosub SUB_PrintText
        ;       
    Until quit = 1
    ;
End
;
;----------------------------------------
; Subroutine -> PrintText
;----------------------------------------
SUB_PrintText:   
;   
   hDC = StartDrawing(WindowOutput(0))                  ; ### START DRAWING ###
        DrawingMode(1)                                   ; TextBackground Transparent
        SetTextCharacterExtra_(hDC,charspace)          ; Here we set the pixel space of any char!
        txtpos = TextWidth(text.s)                      ; Get lenght of actual text in pixel...
        DrawText (150-(txtpos/2),winh/2-15, text.s, RGB(mycol,mycol,mycol))                                ; Print "Please insert coin" text
    StopDrawing()     
    ;
Return
;
;----------------------------------------

PIII450, 256MB Ram, 6GB HD, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...

greetz
MrVainSCL! aka Thorsten