OT? Changing the fontsize of a non-AHK listbox? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

OT? Changing the fontsize of a non-AHK listbox?

Post by PuzzledGreatly » 24 Jun 2022, 17:19

I use Notepad++ to write AHK scripts and recently started using the extension snippets. This is very useful but the fontsize for the snippets list is very small. WinSpy indicates the snippets classNN as ListBox1. I don't suppose there is a way to increase the font size of this listbox is there? Thanks


User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: OT? Changing the fontsize of a non-AHK listbox?

Post by PuzzledGreatly » 25 Jun 2022, 17:31

Thanks for the reply. The link didn't open for me. I know how to change the font of notepad++ itself but not that used by the snippet plugin.

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

Re: OT? Changing the fontsize of a non-AHK listbox?  Topic is solved

Post by teadrinker » 26 Jun 2022, 01:21

Try this:

Code: Select all

ControlGet, hListBox, hwnd,, ListBox1, ahk_class Notepad++
ChangeFontInExternalControl(hListBox, "Calibri", "s12 q5")
ExitApp

ChangeFontInExternalControl(hCtrl, fontName, options, isUnicode := true) { ; options like in Gui, Font, ...
   static PROCESS_ALL_ACCESS := 0x1F0FFF, MEM_RELEASE := 0x8000, INFINITE := 0xFFFFFFFF
        , params := ["UInt", MEM_COMMIT := 0x1000, "UInt", PAGE_EXECUTE_READWRITE := 0x40, "Ptr"]
        , WM_SETFONT := 0x30
   szLF := 28 + (32 << !!isUnicode)
        
   WinGet, PID, PID, ahk_id %hCtrl%
   Loop 1 {
      if GetProcessBitness(PID) != A_PtrSize*8 && error := "Bitness mismatch"
         break
      
      GetFont(fontName, options, LOGFONT, isUnicode)
      
      if !hProc := DllCall("OpenProcess", "UInt", PROCESS_ALL_ACCESS, "Int", 0, "UInt", PID, "Ptr") {
         error := "OpenProcess failed, error: " . SysError()
         break
      }
      if !remoteAddr := DllCall("VirtualAllocEx", "UInt", hProc, "UInt", 0, "UInt", szLF, params*) {
         error := "VirtualAllocEx failed, error: " . SysError()
         break
      }
      if !DllCall("WriteProcessMemory", "Ptr", hProc, "Ptr", remoteAddr, "Ptr", &LOGFONT, "UInt", szLF, "UInt", 0) {
         error := "WriteProcessMemory failed, error: " . SysError()
         break
      }
      hModule := DllCall("GetModuleHandle", "str", "Gdi32.dll", "Ptr")
      pFunc := DllCall("GetProcAddress", "Ptr", hModule, AStr, "CreateFontIndirect" . (isUnicode ? "W" : "A"), "Ptr")
      if !hThread := DllCall("CreateRemoteThread", "Ptr", hProc, "Ptr", 0, "Ptr", 0
                                                 , "Ptr", pFunc, "Ptr", remoteAddr, "UInt", 0, "UInt", 0) {
         error := "CreateRemoteThread failed, error: " . SysError()
         break
      }
      DllCall("WaitForSingleObject", "Ptr", hThread, "UInt", INFINITE)
      if !DllCall("GetExitCodeThread", "Ptr", hThread, "UIntP", hFont)
         error := "GetExitCodeThread failed, error: " . SysError()
   }
   (hThread && DllCall("CloseHandle", "Ptr", hThread))
   (remoteAddr && DllCall("VirtualFreeEx", "Ptr", hProc, "Ptr", remoteAddr, "UInt", 0, "UInt", MEM_RELEASE))
   (hProc && DllCall("CloseHandle", "Ptr", hProc))
   if error
      MsgBox, 16, Error, % error
   else
      SendMessage, WM_SETFONT, hFont, true,, ahk_id %hCtrl%
}

GetFont(fontName, options, ByRef LOGFONT, isUnicode) {
   static WM_GETFONT := 0x31, szLF := 28 + (32 << !!isUnicode)
   Gui, New
   Gui, Font, % options, % fontName
   Gui, Add, Text, hwndhText
   SendMessage, WM_GETFONT,,,, ahk_id %hText%
   hFont := ErrorLevel
   Gui, Destroy
   VarSetCapacity(LOGFONT, szLF, 0)
   DllCall("GetObject", "Ptr", hFont, "Int", szLF, "Ptr", &LOGFONT)
   DllCall("DeleteObject", "Ptr", hFont)
}

GetProcessBitness(PID) {
   if !A_Is64bitOS
      Return 32
   hProc := DllCall("OpenProcess", "UInt", PROCESS_QUERY_INFORMATION := 0x400, "UInt", 0, "UInt", PID, "Ptr")
   DllCall("IsWow64Process", "Ptr", hProc, "IntP", is32)
   DllCall("CloseHandle", "Ptr", hProc)
   Return 32 << !is32
}

SysError(ErrorNum = "")
{ 
   static FORMAT_MESSAGE_ALLOCATE_BUFFER := 0x100, FORMAT_MESSAGE_FROM_SYSTEM := 0x1000
   (ErrorNum = "" && ErrorNum := A_LastError)
   DllCall("FormatMessage", UInt, FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM
                          , UInt, 0, UInt, ErrorNum, UInt, 0, PtrP,  pBuff, UInt, 512, Str, "")
   Return (str := StrGet(pBuff)) ? str : ErrorNum
}
Last edited by teadrinker on 10 Oct 2022, 19:48, edited 1 time in total.

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: OT? Changing the fontsize of a non-AHK listbox?

Post by PuzzledGreatly » 26 Jun 2022, 17:07

Wow! I'm beyond words. That worked like a charm. Thank you teadrinker. I can now read the snippet names without screwing up my eyes. Incredibly useful.


User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: OT? Changing the fontsize of a non-AHK listbox?

Post by PuzzledGreatly » 10 Oct 2022, 18:53

With recent versions of Notepad++ (8.5 and up) when I run the code the changes to the font aren't fully redrawn. I tried refreshing the Notepad window by using Winset, redraw but that didn't help. If toggle full screen mode on and off using Notepad itself by pressing f11 twice the font change shows correctly. Is there any tweak I can add to the code to refresh the display more smoothly? Thanks.

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

Re: OT? Changing the fontsize of a non-AHK listbox?

Post by teadrinker » 10 Oct 2022, 19:42

Just add WinHide and WinShow.

Code: Select all

ControlGet, hListBox, hwnd,, ListBox1, ahk_class Notepad++
ChangeFontInExternalControl(hListBox, "Calibri", "s12 q5")
WinHide, ahk_id %hListBox%
WinShow, ahk_id %hListBox%

User avatar
PuzzledGreatly
Posts: 1303
Joined: 29 Sep 2013, 22:18

Re: OT? Changing the fontsize of a non-AHK listbox?

Post by PuzzledGreatly » 10 Oct 2022, 19:53

Thanks, teadrinker, just after posting I found a similar solution:

Code: Select all

Control, hide,,ListBox1,A
Control, show,,ListBox1,A

Post Reply

Return to “Ask for Help (v1)”