ChooseFont

 

Since userforms only allow you to write to the caption bar area, changing the font is of little use..

Nevertheless, for the sake of completeness, here it is.

Again, functions and declarations are in their own module.

Create a userform with two command buttons (CommandButton1/2) and add following code:


Option Explicit

Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" _
   (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, _
   ByVal nCount As Long) As Long
Private Declare Function ExtTextOut Lib "gdi32" Alias "ExtTextOutA" _
   (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal wOptions As Long, _
   ByVal lpRect As Any, ByVal lpString As String, ByVal nCount As Long, lpDx As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
   
Private Sub CommandButton2_Click()
   Dim UF_hWnd As Long
   Dim UF_hDC As Long
   Dim strTest As String
   
   UF_hWnd = FindWindow("ThunderDFrame", UserForm1.Caption)
   UF_hDC = GetWindowDC(UF_hWnd)
   If CDlgChooseFont(UF_hWnd, UF_hDC) Then
      FontSetFont UF_hWnd
      strTest = "Font Test on Form"
      ExtTextOut UF_hDC, 0&, 0&, 0&, 0&, strTest, Len(strTest), ByVal 0&
   End If

End Sub


ChooseFont needs a Device Context (DC) to work and therefore a parent handle for creating the DC.

After clicking commandbutton2(Set Font) and choosing a font, you will see this:

 

 

Go to top