Jump to content

Setup PERMANENT Font For RichEdit Control


Go to solution Solved by Nine,

Recommended Posts

Hi,

I've searching for a while to solve this.
I found also other people searching a possibility to setup a permanent (say default) font for RichEdit Control.

In fact a default font is set for RichEdit Control, but it is limited to some StockObject Fonts, which are not satisfying.

You can see this fonts under:
https://www.autoitscript.com/autoit3/docs/libfunctions/_WinAPI_GetStockObject.htm

The line where the font is defined in the _GUICtrlRichEdit_Create function is:

_SendMessage($hRichEdit, $__RICHEDITCONSTANT_WM_SETFONT, _WinAPI_GetStockObject($DEFAULT_GUI_FONT), True)

_WinAPI_GetStockObject($DEFAULT_GUI_FONT) gets the handle to the preselected RichEdit default font $DEFAULT_GUI_FONT.

My question is how can i get the font handle (for example used by notepad) which replaces this RichEdit default font.

Sounds easy, but so far I did not come further...

Thanks for assistance.

Cheers mike

Link to comment
Share on other sites

  • Solution

You could wrap a function around like this :

#include <GUIConstantsEx.au3>
#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>
#include <WinAPIGdi.au3>
#include <FontConstants.au3>

Example()

Func Example()
    Local $hGui, $hRichEdit, $iMsg
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName, StringLen(".exe")) & ")", 320, 350, -1, -1)
    $hRichEdit = _GUICtrlRichEdit_CreateEx($hGui, "This is a test.", 10, 10, 300, 220, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    _GUICtrlRichEdit_AppendText($hRichEdit, @CRLF & "This is more text")
    GUISetState(@SW_SHOW)

    While True
        $iMsg = GUIGetMsg()
        Select
            Case $iMsg = $GUI_EVENT_CLOSE
                _GUICtrlRichEdit_Destroy($hRichEdit) ; needed unless script crashes
                ; GUIDelete()   ; is OK too
                Exit
        EndSelect
    WEnd
EndFunc   ;==>Example

Func _GUICtrlRichEdit_CreateEx($hWnd, $sText, $iLeft, $iTop, $iWidth = 150, $iHeight = 150, $iStyle = -1, $iExStyle = -1)
  Local $hCtrl = _GUICtrlRichEdit_Create($hWnd, $sText, $iLeft, $iTop, $iWidth, $iHeight, $iStyle, $iExStyle)
  If @error Then Return SetError(@error)
  Local $hFont = _WinAPI_CreateFont(20, 0, 0, 0, 600, False, False, False, $DEFAULT_CHARSET, _
        $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Arial')
  _SendMessage($hCtrl, $WM_SETFONT, $hFont, True)
  _WinAPI_DeleteObject($hFont)
  Return $hCtrl
EndFunc

Where you would have the definition of the font standardized...

Edited by Nine
Link to comment
Share on other sites

Thanks lot Nine,

works exellent.
I wonder why this is not implented.

The other way setting the font is just not working satisfying.

BTW
I use 10, Lucida Console
but  have to setup 13, Lucida Console if I setup with the _Send function.

$hFont = _WinAPI_CreateFont(13, 0, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, 'Lucida Console')
_SendMessage($hWnd, $WM_SETFONT, $hFont, True)
_WinAPI_DeleteObject($hFont)

Anyway, thanks again

Cheers mike

Link to comment
Share on other sites

44 minutes ago, mike1950r said:

Thanks lot Nine,

works exellent. I wonder why this is not implented.

Aside : My opinion regarding the new feature of the 'Solved Counter' is somewhat ambivalent, frankly speaking. However, since this feature is now in place, and likely to remain, it should be used to make sense of it.

@mike1950r : As @Nine 's contribution obviously solved your problem, you can mark it accordingly, if you like :).

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

It is because they both sending different messages to the control.  _GUICtrlRichEdit_SetFont uses $EM_SETCHARFORMAT message and does a different calculation to determine the size of the font.

Link to comment
Share on other sites

Nine,

thanks for the info.

so i have modified my function respecting this difference of 3.

Func WindowEditSetDefaultFont($hWnd, $iPoints, $sName)
    Local $hFont

    $hFont = _WinAPI_CreateFont($iPoints + 3, 0, 0, 0, $FW_NORMAL, False, False, False, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $DEFAULT_QUALITY, 0, $sName)
    _SendMessage($hWnd, $WM_SETFONT, $hFont, True)
    _WinAPI_DeleteObject($hFont)
EndFunc   ;==>WindowEditSetDefaultFont

All the best and thanks for all.

Cheers mike

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...