You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Elixir Desktop能否为Windows应用设置任务栏与窗口顶部双图标?

背景

我有一个基于Elixir Desktop开发的Windows桌面个人项目,目前运行良好,现需为其设置图标。

通常在以下模块中进行配置:

defmodule WebInterface.Application do
  # See https://hexdocs.pm/elixir/Application.html
  # for more information on OTP Applications
  @moduledoc false

  use Application

  alias Desktop
  alias Manager
  alias WebInterface.{Endpoint, Telemetry}
  alias WebInterface.Live.MenuBar

  @impl true
  def start(_type, _args) do
    children = [
      Telemetry,
      {Phoenix.PubSub, name: WebInterface.PubSub},
      Endpoint,
      Manager,
      {Desktop.Window,
       [
         app: :web_interface,
         id: WebInterface,
         title: "Market Manager",
         size: {900, 960},
         menubar: MenuBar,
         icon: "static/images/resized_logo_4.png", # 这里是图标配置位置
         url: &WebInterface.Endpoint.url/0
       ]}
    ]

    opts = [strategy: :one_for_one, name: WebInterface.Supervisor]
    Supervisor.start_link(children, opts)
  end

  @impl true
  def config_change(changed, _new, removed) do
    WebInterface.Endpoint.config_change(changed, removed)
    :ok
  end
end
问题

当前存在的问题是,Windows任务栏和应用窗口顶部共用同一图标:

图标显示效果示意图

其中底部Windows任务栏的图标(黄色标记处)显示正常,但窗口顶部的图标出现失真,效果极差。

理想的解决方式是为任务栏和窗口顶部分别设置不同的图标,但查阅官方示例应用后未找到实现方法。

疑问

请问是否可以实现该需求?若可以,具体操作方法是什么?


解决方案

可以实现分别设置任务栏和窗口图标的需求,以下是两种可行方案:

方案一:调用wxWidgets底层API单独设置窗口图标

Elixir Desktop基于wxWidgets,你可以通过调用底层Erlang wx绑定API,在窗口启动后为其单独设置标题栏图标,任务栏图标保持由Desktop.Window的icon参数控制。

修改WebInterface.Application的start/2函数:

@impl true
def start(_type, _args) do
  children = [
    Telemetry,
    {Phoenix.PubSub, name: WebInterface.PubSub},
    Endpoint,
    Manager,
    {Desktop.Window,
     [
       app: :web_interface,
       id: WebInterface,
       title: "Market Manager",
       size: {900, 960},
       menubar: MenuBar,
       icon: "static/images/taskbar_icon.png", # 指定任务栏图标
       url: &WebInterface.Endpoint.url/0
     ]}
  ]

  opts = [strategy: :one_for_one, name: WebInterface.Supervisor]
  {:ok, supervisor} = Supervisor.start_link(children, opts)

  # 获取窗口实例并设置标题栏图标
  if window = Desktop.Window.get(WebInterface) do
    window_icon = :wxIcon.new("static/images/window_title_icon.png", :wxBITMAP_TYPE_PNG)
    :wxFrame.set_icon(window, window_icon)
    :wxIcon.destroy(window_icon) # 释放资源
  end

  {:ok, supervisor}
end

注意:窗口标题栏图标推荐使用16x16或32x32像素的PNG文件,避免因尺寸不匹配导致失真。

方案二:Electron后端下分离配置(仅适用于Electron渲染模式)

如果你的项目使用Elixir Desktop的Electron后端,可直接修改Electron主进程配置,分别设置两类图标:

  1. 找到项目中的Electron主进程文件(通常为priv/electron/main.js),修改内容如下:
const { app, BrowserWindow } = require('electron')

function createWindow () {
  const mainWindow = new BrowserWindow({
    width: 900,
    height: 960,
    icon: __dirname + '/../static/images/window_title_icon.png', // 窗口标题栏图标
    webPreferences: {
      nodeIntegration: true
    }
  })

  // 设置任务栏图标
  app.setIcon(__dirname + '/../static/images/taskbar_icon.png')

  mainWindow.loadURL('http://localhost:4000')
}

app.whenReady().then(() => {
  createWindow()
})
  1. 同时在Desktop.Window的配置中移除icon参数,避免配置冲突。

内容的提问来源于stack exchange,提问作者Flame_Phoenix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 19:50:39