Gui Calendar, MonthCal: how to change the font size? Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
emp00
Posts: 147
Joined: 15 Apr 2023, 12:02

Gui Calendar, MonthCal: how to change the font size?

Post by emp00 » 10 Jun 2023, 06:21

Dear Team, I'm working on a Calendar Gui, adapted from here.

Question: How do I change the font type and font size of the complete calendar display? As per documentation, this should work using SetFont but I don't get the desired effect. See screenshots below: The font does not change at all -> only the calendar window height is affected, getting somewhat smaller?! What am I doing wrong? Thanks for helping out!

Code: Select all

CreateCalendar()
{
    global Calendar := Gui('-MinimizeBox', GetTitle())
    Calendar.MarginX := -18
    Calendar.MarginY := -15
    ;Calendar.SetFont("s14 bold", "Tahoma"))  <-- this does not really work!
    Calendar.Add('MonthCal', '4 R3 W-4')

    Calendar.OnEvent('Close', DestroyCalendar)
    Calendar.OnEvent('Escape', DestroyCalendar)
    SetTimer(UpdateTitle, 1000)
}

Initial display without SetFont (commented out)
Image

Calendar display with Calendar.SetFont("s14 bold", "Tahoma")
Image

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Gui Calendar, MonthCal: how to change the font size?

Post by teadrinker » 10 Jun 2023, 08:08

Code: Select all

wnd := Gui()
MonthCal := wnd.AddMonthCal('4 R3 W-4')
DllCall('UxTheme\SetWindowTheme', 'Ptr', MonthCal.hwnd, 'Ptr', 0, 'Str', ';')
SendMessage WM_SETFONT := 0x30, GetFont('s14 bold', 'Tahoma'), true, MonthCal
wnd.Show()

GetFont(options, fontName) {
    wnd := Gui()
    wnd.SetFont(options, fontName)
    hFont := SendMessage(WM_GETFONT := 0x31,,, wnd.AddText())
    wnd.Destroy()
    return hFont
}

emp00
Posts: 147
Joined: 15 Apr 2023, 12:02

Re: Gui Calendar, MonthCal: how to change the font size?

Post by emp00 » 10 Jun 2023, 10:03

@teadrinker Thank you, this works partially. Is there any documentation on this trick in order to understand how this works? This code also changes some of the colors and I have no idea how this can be configured... Complete mystery to me, no ok for now... ;-)

My remaining problem: The Gui window is too small now, it's not automatically adjusted to the bigger font calendars. Instead of a 4x3 display I now only get a 3x1 thus only one row with 3 calendars in a completely "non fitting" window. Looks like the Gui window size is still based on the small default font size? Can this be fixed "automatically"? See my complete Gui setup code above. I would like to avoid setting a hardcoded window size which likely will not work on another PC setup with different dpi / screen resolutions etc.

Many thanks for helping with this! I have a feeling, that a "font size change" is actually a "missing feature" in the official MonthCal Gui implementation? Otherwise such a special and complex DllCall workaround would not be needed, right?

Image

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Gui Calendar, MonthCal: how to change the font size?

Post by teadrinker » 10 Jun 2023, 11:55

emp00 wrote: Gui window is too small now
This should work:

Code: Select all

wnd := Gui()
rows := 3
columns := 4
MonthCal := wnd.AddMonthCal('4 R' . rows . ' W-' . columns)
SetMonthCalFont('s14 bold', 'Tahoma', MonthCal)
AdjustMonthCal(rows, columns, MonthCal)
wnd.Show('AutoSize')

SetMonthCalFont(options, fontName, ctrl) {
    DllCall('UxTheme\SetWindowTheme', 'Ptr', ctrl.hwnd, 'Ptr', 0, 'Str', ';')
    SendMessage WM_SETFONT := 0x30, GetFont(options, fontName), true, ctrl

    GetFont(options, fontName) {
        wnd := Gui()
        wnd.SetFont(options, fontName)
        hFont := SendMessage(WM_GETFONT := 0x31,,, wnd.AddText())
        wnd.Destroy()
        return hFont
    }
}

AdjustMonthCal(rows, columns, ctrl) {
    rect := Buffer(16, 0)
    SendMessage MCM_GETMINREQRECT := 0x1009,, rect, ctrl
    width := NumGet(rect, 8, 'Int')
    height := NumGet(rect, 12, 'Int')
    ctrl.Move(,, width * columns, height * rows)
}
emp00 wrote: "font size change" is actually a "missing feature" in the official MonthCal Gui implementation?
AHK does not implement all features as built-in functions, you often have to look for more information on MSDN.
Last edited by teadrinker on 10 Jun 2023, 12:03, edited 1 time in total.

emp00
Posts: 147
Joined: 15 Apr 2023, 12:02

Re: Gui Calendar, MonthCal: how to change the font size?

Post by emp00 » 10 Jun 2023, 14:02

@teadrinker Thank you VERY MUCH, this works!! The window is now adjusted correctly, excellent solution! :dance: :rainbow:

"One more question": Could you explain, how I can influence the colors? The yellow marked text is now pale blue and barely readable. I tried changing the 0x30 / 0x31 or 0x1009 codings, but obviously this is not related to the colors. Can you give me a hint?

Image

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: Gui Calendar, MonthCal: how to change the font size?  Topic is solved

Post by teadrinker » 10 Jun 2023, 16:20

You can change the colors in this way:

Code: Select all

wnd := Gui()
rows := 3
columns := 4
MonthCal := wnd.AddMonthCal('4 R' . rows . ' W-' . columns)
SetMonthCalFont('s14 bold', 'Tahoma', MonthCal)
AdjustMonthCal(rows, columns, MonthCal)

mcColors := {
    MCSC_BACKGROUND  : 0,
    MCSC_TEXT        : 1,
    MCSC_TITLEBK     : 2,
    MCSC_TITLETEXT   : 3,
    MCSC_MONTHBK     : 4,
    MCSC_TRAILINGTEXT: 5
}
SetMonthCalColor(0x0099DD, mcColors.MCSC_TITLEBK, MonthCal)

wnd.Show('AutoSize')

SetMonthCalFont(options, fontName, ctrl) {
    DllCall('UxTheme\SetWindowTheme', 'Ptr', ctrl.hwnd, 'Ptr', 0, 'Str', '')
    SendMessage WM_SETFONT := 0x30, GetFont(options, fontName), true, ctrl

    GetFont(options, fontName) {
        wnd := Gui()
        wnd.SetFont(options, fontName)
        hFont := SendMessage(WM_GETFONT := 0x31,,, wnd.AddText())
        wnd.Destroy()
        return hFont
    }
}

AdjustMonthCal(rows, columns, ctrl) {
    rect := Buffer(16, 0)
    SendMessage MCM_GETMINREQRECT := 0x1009,, rect, ctrl
    width := NumGet(rect, 8, 'Int') * 96 // A_ScreenDPI
    height := NumGet(rect, 12, 'Int') * 96 // A_ScreenDPI
    ctrl.Move(,, width * columns, height * (0.91 * (rows - 1) + 1))
}

SetMonthCalColor(colorRGB, part, ctrl) {
    colorBGR := colorRGB >> 16 | colorRGB & 0xFF00 | (colorRGB & 0xFF) << 16
    SendMessage MCM_SETCOLOR := 0x100A, part, colorBGR, ctrl
}

emp00
Posts: 147
Joined: 15 Apr 2023, 12:02

Re: Gui Calendar, MonthCal: how to change the font size?

Post by emp00 » 11 Jun 2023, 06:49

Thank you @teadrinker ! Problem 100% solved, excellent help (and I even begin to understand how the MS controls are programmed! thanks also for the links!)


Post Reply

Return to “Ask for Help (v2)”