Making a gadget have a bold font

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Making a gadget have a bold font

Post by PB »

Code updated for 5.20+

Thanks to Sparkie and Xombie for providing the original code upon which this procedure is derived. :)

Here's my procedure that works after checking the rest of this thread for problems.

Code: Select all

Procedure SetGadgetFontStyle(gad,bold,italic,underline,strikeout)
  font=SendMessage_(GadgetID(gad),#WM_GETFONT,0,0) : GetObject_(font,SizeOf(LOGFONT),lg.LOGFONT)
  If bold=1 : lg\lfWeight=#FW_BOLD : EndIf : lg\lfItalic=italic : lg\lfUnderline=underline : lg\lfStrikeOut=strikeout
  font=CreateFontIndirect_(lg) : SendMessage_(GadgetID(gad),#WM_SETFONT,font,1)
EndProcedure

If OpenWindow(0,300,250,300,200,"test",#PB_Window_SystemMenu)
  FrameGadget(1,10,10,200,150,"This will be italic") : SetGadgetFontStyle(1,0,1,0,0)
  ButtonGadget(2,30,50,120,25,"And this is bold") : SetGadgetFontStyle(2,1,0,0,0)
  TextGadget(3,30,100,120,25,"And this is a mix!") : SetGadgetFontStyle(3,1,1,1,1)
  Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
Last edited by PB on Mon Sep 13, 2010 9:23 am, edited 2 times in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post by dagcrack »

Very nice! Thanks for sharing!
:)
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

Yeah, it is nice :D I was looking at the title and thought, "Sweet - this will help me out a bit in my project" and then read that you got some code ideas from something I did. And I'm thinking... what? Where? :D

Thanks for the code ^_^ Not surprised to see Sparkie being credited for something but it's odd seeing myself credited :D
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> read that you got some code ideas from something I did. And I'm
> thinking... what? Where?

From here:

viewtopic.php?t=12853

All I did was modify your "frankenstein-ed" function to make it work. :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

Thanks for sharing! It seems to solve one problem I stumbled over in my current project, which
is to highligt some text without messing with users preferred system fonts and size! :)

However:

Code: Select all

r=LoadFont(#PB_Any,PeekS(@lf\lfFacename),lf\lfHeight,#PB_Font_Bold)
Nothing happens if I change the flag (=,#PB_Font_Bold) to _Italic, or _Underline, or if I remove it altogether?? from the function call . Why is that ?? :?:
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Not to speak about the font face and font size. The font gets set to MS Sans Serif 10 bold. No matter what font face or size the gadget had before and no matter what you substitute #PB_Font_Bold with. I thought that point was to preserve the existing font?

Here is a way that is more customizable, and also portable:

Code: Select all

Procedure MakeGadgetItalic(gadget)
  Static font
  If IsFont(font) = 0
    font = LoadFont(#PB_Any, "Tahoma", 10, #PB_Font_Italic)
  EndIf
  UseFont(font)
  SetGadgetFont(gadget, FontID())
EndProcedure 

If OpenWindow(1,300,250,400,200,#PB_Window_SystemMenu,"Window") 
  CreateGadgetList(WindowID())
  Frame3DGadget(1,10,10,200,100,"This will be italic") : MakeGadgetItalic(1)
  ButtonGadget(2,30,50,120,25,"And so will this")    : MakeGadgetItalic(2)
  Repeat : Until WaitWindowEvent()=#PB_EventCloseWindow 
EndIf
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> The font gets set to MS Sans Serif 10 bold

Eek, you're right! :oops:

> I thought that point was to preserve the existing font?

That was the intention, but as I didn't test it with anything else, I thought it
was preserving the current gadget font. Not so. Hmm, sorry about that, all.
I will have a bit of a play with it and see what comes. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Sub-Routine
User
User
Posts: 82
Joined: Tue May 03, 2005 2:51 am
Location: Wheeling, Illinois, USA
Contact:

Post by Sub-Routine »

I use this to keep the current font:

Code: Select all

FontID3 = LoadFont(3, "", "9", #PB_Font_Bold)
A size does have to be specified, or it comes out really tiny :shock:

Rand
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

> I use this to keep the current font:
> FontID3 = LoadFont(3, "", "9", #PB_Font_Bold)

Awesome! :D Now that's what we were looking for! ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

No, not if I have to specify a font size. I need to add the bold attribute to whatever font and size
the user has selected as his/hers system default. :)
Sub-Routine
User
User
Posts: 82
Joined: Tue May 03, 2005 2:51 am
Location: Wheeling, Illinois, USA
Contact:

Post by Sub-Routine »

I was disappointed that I have to specify a size, but since they have to fit a gadget and I am sizing the gadget...

Shouldn't there be a Registry entry for the default font?

Rand
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

I played around a bit with PB's code and got this to work the way I want to. (but have no idea why it works :oops: )

Code: Select all

Procedure MakeGadgetBold(gad) 
  lf.LOGFONT : cur=SendMessage_(GadgetID(gad),#WM_GETFONT,0,0) 
  GetObject_(cur,SizeOf(LOGFONT),@lf.LOGFONT)
  If LoadFont(#PB_Any,PeekS(@lf\lfFacename),lf\lfHeight,#PB_Font_Underline)
    SetGadgetFont(gad,FontID())
  EndIf 
EndProcedure 

or
.
.
Procedure SetGadgetFontAttrib(Id,Flag) 
  Lf.LOGFONT
  GetObject_(SendMessage_(GadgetID(Id),#WM_GETFONT,0,0),SizeOf(LOGFONT),@Lf.LOGFONT)
  If LoadFont(#PB_Any,PeekS(@Lf\lfFacename),Lf\lfHeight,Flag)
    SetGadgetFont(Id,FontID())
  EndIf 
EndProcedure
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Thanks utopiomania, works great! :)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Umm, I don't want to destroy the fun for you, but it doesn't preserve the font style. In other words, you can't have two procedures, one for setgadgetitalic and one for setgadgetbold and use these on the same gadget to get both bold and italic text.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

I know, which is why utopiomania gave the second Procedure which lets you
set many styles with one call. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Post Reply