lxn / walk

A Windows GUI toolkit for the Go Programming Language
Other
6.84k stars 884 forks source link

How can I embed an image within the exe file #665

Open swagar opened 4 years ago

swagar commented 4 years ago

How can I embed an image file within the exe file as a resource like the manifest. At the moment I need to put the original image file along with the exe file and this is a problem since the program doesn't run without the image file.

spddl commented 3 years ago

I had the same problem with the notifyicon example.

with version 1.16 we can simply insert the icon into the *.exe

// Copyright 2011 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.

package main

import (
    "bytes"
    "image"
    "log"

    _ "embed"

    "github.com/lxn/walk"
)

/// NEW since go 1.16
//go:embed stop.ico
var stop []byte

func main() {
    // We need either a walk.MainWindow or a walk.Dialog for their message loop.
    // We will not make it visible in this example, though.
    mw, err := walk.NewMainWindow()
    if err != nil {
        log.Fatal(err)
    }

    // We load our icon from a file.
    img, _, err := image.Decode(bytes.NewReader(stop)) // NEW
    if err != nil {
        log.Fatal(err)
    }
    icon, err := walk.NewIconFromImageForDPI(img, 96) // NEW
    if err != nil {
        log.Fatal(err)
    }
    // icon, err := walk.Resources.Icon("../img/stop.ico") // OLD
    // if err != nil {
    //  log.Fatal(err)
    // }

    // Create the notify icon and make sure we clean it up on exit.
    ni, err := walk.NewNotifyIcon(mw)
    if err != nil {
        log.Fatal(err)
    }
    defer ni.Dispose()

    // Set the icon and a tool tip text.
    if err := ni.SetIcon(icon); err != nil {
        log.Fatal(err)
    }
    if err := ni.SetToolTip("Click for info or use the context menu to exit."); err != nil {
        log.Fatal(err)
    }

    // When the left mouse button is pressed, bring up our balloon.
    ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
        if button != walk.LeftButton {
            return
        }

        if err := ni.ShowCustom(
            "Walk NotifyIcon Example",
            "There are multiple ShowX methods sporting different icons.",
            icon); err != nil {

            log.Fatal(err)
        }
    })

    // We put an exit action into the context menu.
    exitAction := walk.NewAction()
    if err := exitAction.SetText("E&xit"); err != nil {
        log.Fatal(err)
    }
    exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
    if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
        log.Fatal(err)
    }

    // The notify icon is hidden initially, so we have to make it visible.
    if err := ni.SetVisible(true); err != nil {
        log.Fatal(err)
    }

    // Now that the icon is visible, we can bring up an info balloon.
    if err := ni.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
        log.Fatal(err)
    }

    // Run the message loop.
    mw.Run()
}

I hope it helps

roffe commented 3 years ago

Thank you @spddl, I was just looking for how to do this! :)

GodGavin commented 2 years ago

Thank you @spddl, I was also just looking for how to do this! :)

GodGavin commented 2 years ago

Thank you @spddl, I was also just looking for how to do this! :)

but image cant deode a .ico file, false:image: unknown format ,how to sovle this problem ?@@spddl

529124368 commented 2 years ago

@GodGavin You can use png image to convert to ico image。 show my code. QQ图片20220625003806

82kg commented 2 years ago

Thank you @spddl, I was also just looking for how to do this! :)

but image cant deode a .ico file, false:image: unknown format ,how to sovle this problem ?@@spddl

Thank you @spddl, I was also just looking for how to do this! :)

but image cant deode a .ico file, false:image: unknown format ,how to sovle this problem ?@@spddl

have you finger out this problem?

wilon commented 10 months ago

Because of the "image" can't parse .ico file. Just use this package:

go get github.com/mat/besticon/ico
// Copyright 2011 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.

package main

import (
    "bytes"
    "log"

    _ "embed"

    "github.com/lxn/walk"
    "github.com/mat/besticon/ico"
)

// / NEW since go 1.16
//
//go:embed favicon.ico
var stop []byte

func main() {
    // We need either a walk.MainWindow or a walk.Dialog for their message loop.
    // We will not make it visible in this example, though.
    mw, err := walk.NewMainWindow()
    if err != nil {
        log.Fatal(err)
    }

    // We load our icon from a file.
    img, err := ico.Decode(bytes.NewReader(stop)) // NEW parse ico file.
    if err != nil {
        log.Fatal(err)
    }
    icon, err := walk.NewIconFromImageForDPI(img, 96) // NEW
    if err != nil {
        log.Fatal(err)
    }
    // icon, err := walk.Resources.Icon("../img/stop.ico") // OLD
    // if err != nil {
    //  log.Fatal(err)
    // }

    // Create the notify icon and make sure we clean it up on exit.
    ni, err := walk.NewNotifyIcon(mw)
    if err != nil {
        log.Fatal(err)
    }
    defer ni.Dispose()

    // Set the icon and a tool tip text.
    if err := ni.SetIcon(icon); err != nil {
        log.Fatal(err)
    }
    if err := ni.SetToolTip("Click for info or use the context menu to exit."); err != nil {
        log.Fatal(err)
    }

    // When the left mouse button is pressed, bring up our balloon.
    ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
        if button != walk.LeftButton {
            return
        }

        if err := ni.ShowCustom(
            "Walk NotifyIcon Example",
            "There are multiple ShowX methods sporting different icons.",
            icon); err != nil {

            log.Fatal(err)
        }
    })

    // We put an exit action into the context menu.
    exitAction := walk.NewAction()
    if err := exitAction.SetText("E&xit"); err != nil {
        log.Fatal(err)
    }
    exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
    if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
        log.Fatal(err)
    }

    // The notify icon is hidden initially, so we have to make it visible.
    if err := ni.SetVisible(true); err != nil {
        log.Fatal(err)
    }

    // Now that the icon is visible, we can bring up an info balloon.
    if err := ni.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
        log.Fatal(err)
    }

    // Run the message loop.
    mw.Run()
}
wunaiyaoguai commented 4 months ago

Directly using the above method, it is not possible to display the icon in the tableview cell. Adding two lines of code makes it work.

import (
    "bytes"
    "embed"
    "fmt"
    "github.com/lxn/walk"
    . "github.com/lxn/walk/declarative"
    "github.com/lxn/win"
    "github.com/mat/besticon/ico"
    "image"
    "image/draw"
    _ "image/png"
    "log"
)

//go:embed assets/img/*
var ICONS embed.FS

func OpenIco(name string) (*walk.Icon, error) {
    content, err := ICONS.ReadFile("assets/img/" + name)
    if err != nil {
        return nil, err
    }

    img, err := ico.Decode(bytes.NewReader(content)) 
    if err != nil {
        log.Fatal(err)
    }

    rgba := image.NewRGBA(img.Bounds())                                 // new
    draw.Draw(rgba, img.Bounds(), img, image.Point{}, draw.Src) // new

    icon, err := walk.NewIconFromImageForDPI(img, 96)  

    size := icon.Size()
    fmt.Println(size)
    if err != nil {
        log.Fatal(err)
    }
    return icon, nil
}

use for TableView


type ItemModel struct {
    walk.TableModelBase
    items            []*ListItem
    connectedIcon    *walk.Icon
    disconnectedIcon *walk.Icon
}

func NewItemModel() *ItemModel {

    //connectedIcon, err := OpenIco("connected.png")
    connectedIcon, err := OpenIco("connected.ico")
    if err != nil {
        log.Fatal(err)
    }
    disconnectedIcon, err := OpenIco("disconnected.ico")
    if err != nil {
        log.Fatal(err)
    }

    items := []*ListItem{
        {Status: "disconnected", Name: "123456"},
        {Status: "connected", Name: "123456"},
        {Status: "connected", Name: "123456"},
        {Status: "disconnected", Name: "123456"},
    }

    return &ItemModel{items: items, connectedIcon: connectedIcon, disconnectedIcon: disconnectedIcon}
}

TableView{
        AssignTo:         tv,
        AlternatingRowBG: true,
        CheckBoxes:       false,
        ColumnsOrderable: false,
        MultiSelection:   false,
        Columns: []TableViewColumn{
            {Title: "", Alignment: AlignFar},
            {Title: "name", Width: 125, Alignment: AlignNear},
            {Title: "status", Width: 50, Alignment: AlignCenter},
        },
        Model: model,
        OnItemActivated: func() {
            selectedIndex := (*tv).CurrentIndex()
            if selectedIndex > -1 {
                item := (*tv).Model().(*ItemModel).items[selectedIndex]
                walk.MsgBox(nil, "Item Activated", item.Name, walk.MsgBoxIconInformation)
            }
        },
        StyleCell: func(style *walk.CellStyle) {
            item := model.items[style.Row()]
            style.Font = nil
            if style.Col() == 0 {
                if item.Status == "connected" {
                    style.Image = model.connectedIcon

                } else {
                    style.Image = model.disconnectedIcon
                }
            }
        },
    }