diff options
author | Dmitry Bagdanov <dimbo_job@mail.ru> | 2019-05-02 20:07:22 +0700 |
---|---|---|
committer | Dmitry Bagdanov <dimbo_job@mail.ru> | 2019-05-02 20:07:22 +0700 |
commit | 84b33c38ee6367366dd3a1767f979fd3c085ccc7 (patch) | |
tree | d34e18570c7fa23b7248de4d2b07a462b4caf42a | |
parent | Avoid some ineffiency in layouts and factor out common stuff into LayoutBase (diff) | |
download | wireguard-windows-84b33c38ee6367366dd3a1767f979fd3c085ccc7.tar.xz wireguard-windows-84b33c38ee6367366dd3a1767f979fd3c085ccc7.zip |
add FontMemResource type which allows to load fonts from the application's resources
-rw-r--r-- | fontresource.go | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/fontresource.go b/fontresource.go new file mode 100644 index 00000000..c50db637 --- /dev/null +++ b/fontresource.go @@ -0,0 +1,63 @@ +// Copyright 2010 The Walk Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// +build windows
+
+package walk
+
+import (
+ "github.com/lxn/win"
+)
+
+// FontMemResource represents a font resource loaded into memory from
+// the application's resources.
+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) {
+ if hModule == win.HMODULE(0) {
+ hModule = win.HMODULE(win.GetModuleHandle(nil))
+ }
+
+ hres := win.FindResource(hModule, resourceName, win.MAKEINTRESOURCE(8) /*RT_FONT*/)
+ if hres == win.HRSRC(0) {
+ return nil, lastError("FindResource")
+ }
+
+ size := win.SizeofResource(hModule, hres)
+ if size == 0 {
+ return nil, lastError("SizeofResource")
+ }
+
+ hResLoad := win.LoadResource(hModule, hres)
+ if hResLoad == win.HGLOBAL(0) {
+ return nil, lastError("LoadResource")
+ }
+
+ ptr := win.LockResource(hResLoad)
+ if ptr == 0 {
+ return nil, lastError("LockResource")
+ }
+
+ numFonts := uint32(0)
+ hFontResource := win.AddFontMemResourceEx(ptr, size, nil, &numFonts)
+
+ if hFontResource == win.HANDLE(0) || numFonts == 0 {
+ return nil, lastError("AddFontMemResource")
+ }
+
+ return &FontMemResource { hFontResource: hFontResource }, nil
+}
+
+// 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 |