github.com/secoba/wails/v2@v2.6.4/internal/frontend/desktop/windows/winc/icon.go (about)

     1  //go:build windows
     2  
     3  /*
     4   * Copyright (C) 2019 The Winc Authors. All Rights Reserved.
     5   * Copyright (C) 2010-2013 Allen Dang. All Rights Reserved.
     6   */
     7  
     8  package winc
     9  
    10  import (
    11  	"errors"
    12  	"fmt"
    13  	"syscall"
    14  
    15  	"github.com/secoba/wails/v2/internal/frontend/desktop/windows/winc/w32"
    16  )
    17  
    18  type Icon struct {
    19  	handle w32.HICON
    20  }
    21  
    22  func NewIconFromFile(path string) (*Icon, error) {
    23  	ico := new(Icon)
    24  	var err error
    25  	if ico.handle = w32.LoadIcon(0, syscall.StringToUTF16Ptr(path)); ico.handle == 0 {
    26  		err = errors.New(fmt.Sprintf("Cannot load icon from %s", path))
    27  	}
    28  	return ico, err
    29  }
    30  
    31  func NewIconFromResource(instance w32.HINSTANCE, resId uint16) (*Icon, error) {
    32  	ico := new(Icon)
    33  	var err error
    34  	if ico.handle = w32.LoadIconWithResourceID(instance, resId); ico.handle == 0 {
    35  		err = errors.New(fmt.Sprintf("Cannot load icon from resource with id %v", resId))
    36  	}
    37  	return ico, err
    38  }
    39  
    40  func ExtractIcon(fileName string, index int) (*Icon, error) {
    41  	ico := new(Icon)
    42  	var err error
    43  	if ico.handle = w32.ExtractIcon(fileName, index); ico.handle == 0 || ico.handle == 1 {
    44  		err = errors.New(fmt.Sprintf("Cannot extract icon from %s at index %v", fileName, index))
    45  	}
    46  	return ico, err
    47  }
    48  
    49  func (ic *Icon) Destroy() bool {
    50  	return w32.DestroyIcon(ic.handle)
    51  }
    52  
    53  func (ic *Icon) Handle() w32.HICON {
    54  	return ic.handle
    55  }