From ee5f647c5093bea60ef7d3cc105a7b896555f315 Mon Sep 17 00:00:00 2001 From: Dmitry Bagdanov Date: Thu, 2 May 2019 23:25:42 +0700 Subject: Rewrite implementation to much walk's approach --- fontresource.go | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/fontresource.go b/fontresource.go index c50db637..61f90307 100644 --- a/fontresource.go +++ b/fontresource.go @@ -8,6 +8,7 @@ package walk import ( "github.com/lxn/win" + "syscall" ) // FontMemResource represents a font resource loaded into memory from @@ -16,12 +17,10 @@ type FontMemResource struct { hFontResource win.HANDLE } -// NewFontMemResource function loads a font resource from the executable's resources. -// The font must be embedded into resources using corresponding operator in the -// application's RC script. -func NewFontMemResource(hModule win.HMODULE, resourceName *uint16) (*FontMemResource, error) { +func newFontMemResource(resourceName *uint16) (*FontMemResource, error) { + hModule := win.HMODULE(win.GetModuleHandle(nil)) if hModule == win.HMODULE(0) { - hModule = win.HMODULE(win.GetModuleHandle(nil)) + return nil, lastError("GetModuleHandle") } hres := win.FindResource(hModule, resourceName, win.MAKEINTRESOURCE(8) /*RT_FONT*/) @@ -54,10 +53,26 @@ func NewFontMemResource(hModule win.HMODULE, resourceName *uint16) (*FontMemReso return &FontMemResource { hFontResource: hFontResource }, nil } +// NewFontMemResourceByName function loads a font resource from the executable's resources +// using the resource name. +// The font must be embedded into resources using corresponding operator in the +// application's RC script. +func NewFontMemResourceByName(name string) (*FontMemResource, error) { + return newFontMemResource(syscall.StringToUTF16Ptr(name)) +} + +// NewFontMemResourceById function loads a font resource from the executable's resources +// using the resource ID. +// The font must be embedded into resources using corresponding operator in the +// application's RC script. +func NewFontMemResourceById(id int) (*FontMemResource, error) { + return newFontMemResource(win.MAKEINTRESOURCE(uintptr(id))) +} + // Dispose removes the font resource from memory func (fmr *FontMemResource) Dispose() { if fmr.hFontResource != 0 { win.RemoveFontMemResourceEx(fmr.hFontResource) fmr.hFontResource = 0 } -} \ No newline at end of file +} -- cgit v1.2.3-59-g8ed1b