Windows defautl font

Windows specific forum
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

Hi,

I've playing around trying to use the default font for my GUIs, using the size set in the display properties (is there any other way it can be set?). I've come up with this code, but there seems to be some problem when trying to use the font for gadgets. Can anyone point out what's going wrong? Oh, and if people would be kind enough to test it on an OS other than Win95 and let me know how it goes I'd be very grateful :)

If InitGadget(10)=0 : End : EndIf
If OpenConsole()=0 : End : EndIf

fnt.l = GetStockObject_(#DEFAULT_GUI_FONT)
If fnt
PrintN("Hey, got the font "+Str(SizeOf(LOGFONT)))
DefType.LOGFONT finfo
GetObject_(fnt, SizeOf(LOGFONT), @finfo)
fname$ = PeekS(@finfo\lfFaceName[0])
fsize.l = finfo\lfHeight
PrintN("Font name is "+PeekS(@finfo\lfFaceName[0])+"/"+Str(fsize))
If fsize<0 : fsize=-fsize : EndIf
LoadFont(1,fname$,8)
UseFont(1)
EndIf

mainwindow.l = OpenWindow(0,600,600,400,300,#PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_MaximizeGadget|#PB_Window_SizeGadget,"Ty")
If mainwindow

If CreateGadgetList(WindowID())
SetGadgetFont(FontID())
ButtonGadget(1,4,50,200,30,"Ty")
EndIf

If fnt
StartDrawing(WindowOutput())
DrawingMode(1)
DrawingFont(FontID())
Locate(0,0)
FrontColour(0,0,0)
DrawText("Ty Move Size")
StopDrawing()
CloseFont(1)
EndIf

quit.w=0
While quit=0
ev.l=WaitWindowEvent()
Select ev
Case #PB_EventCloseWindow
quit = 1
EndSelect
Wend
CloseWindow(0)
EndIf
CloseConsole()
If fnt : DeleteObject_(fnt) : EndIf
End
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Can anyone point out what's going wrong?
> fnt.l = GetStockObject_(#DEFAULT_GUI_FONT)

I've been using GetStockObject_(#ANSI_VAR_FONT) to get the default
system font, so I don't know if that helps you at all?


PB - Registered PureBasic Coder

Edited by - PB on 08 February 2002 19:38:23
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.
> Can anyone point out what's going wrong?
> fnt.l = GetStockObject_(#DEFAULT_GUI_FONT)
I've been using GetStockObject_(#ANSI_VAR_FONT) to get the default
system font, so I don't know if that helps you at all?
I had tried that too. The main problem with the code was that the gadgets did not seem to use the correct font. I expected them to be the same as the text which gets printed to the window (and that text is the same as the system menus and things).

I'll try out the ANSI font though. The Win32 docs say that DEFAULT_GUI_FONT is Win95 only (although I think it is the Win95 version of the SDK :wink:.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by tinman.

Hi PB
I've been using GetStockObject_(#ANSI_VAR_FONT) to get the default
system font, so I don't know if that helps you at all?
Yeah, thanks for that. Seems ANSI_VAR_FONT was the correct one to use. However, the reason the gadget was never shown correctly was because I had closed the font (doh). Obviously it needs to stay open for the duration of having your gadgets defined.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Are there any other way to get the default Font handler or ID without the use of any API function?
Does someone know?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Code: Select all

Procedure.s GetDefaultFontName()
  fnt.l=GetStockObject_(#ANSI_VAR_FONT)
  If fnt
   DefType.LOGFONT finfo
   GetObject_(fnt,SizeOf(LOGFONT),@finfo)
   systemfontname.s=PeekS(@finfo\lfFaceName[0])
   ProcedureReturn PeekS(@finfo\lfFaceName[0])
  EndIf
  ProcedureReturn "System"
EndProcedure

Procedure.l GetDefaultFontSize()
  fnt.l=GetStockObject_(#ANSI_VAR_FONT)
  If fnt
   DefType.LOGFONT finfo
   GetObject_(fnt,SizeOf(LOGFONT),@finfo)
   systemfontsize=finfo\lfHeight
   ProcedureReturn finfo\lfHeight
  EndIf
ProcedureReturn 12
EndProcedure
I use those two functions,
and then do something like this:

Code: Select all

 LoadFont(#Font,fontname,fontsize,fontstyle)
 UseFont(#Font)
 SetGadgetFont(#PB_Default,FontID())
Which allows automatic default font settings,
and very easy to allow the user to override with their opwn choise as well!

The two functions are based on info/examples I found elsewhere,
most likely here on the forums, forgot where. Sorry!
The advantage is that you get the PB "handle" #Font in this example
which is the constant I used in this case!
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Thank you for this Rescator :) This is useful for me at the moment.
When making an OS independent app which is intended to work with fonts i don't know how to get the default font. Perhaps there should be a native command to get the handle for an object currently in use.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post by Rescator »

Good point! That would be nice!
But fonts afaik is very platform spesific!
Unless you use some kind of opensource crossplatform font library, if such exist.
Sub-Routine
User
User
Posts: 82
Joined: Tue May 03, 2005 2:51 am
Location: Wheeling, Illinois, USA
Contact:

Post by Sub-Routine »

Isn't a PB program going to use the default font?

Rand
Post Reply