当前位置: 首页 > news >正文

C# 加载本地文件设置应用程序图标

	static class Program{[STAThread]static void Main(){Application.EnableVisualStyles();Application.SetCompatibleTextRenderingDefault(false);Form mainForm = new Form1();mainForm.Show();//IntPtr hProcess = Process.GetCurrentProcess().MainWindowHandle;// 设置应用程序图标SetApplicationIcon(@"./vip.ico");string exeFilePath = Assembly.GetEntryAssembly().Location;CreateShortcut(exeFilePath, @"C:\Users\Admin\Desktop\shortcut.lnk");Application.Run(mainForm);}static void SetApplicationIcon(string iconFilePath){// 从资源文件中加载图标Icon icon = Icon.ExtractAssociatedIcon(iconFilePath);// 将图标设置为应用程序的主图标IntPtr hIcon = icon.Handle;SendMessage(hProcess, WM_SETICON, ICON_SMALL, hIcon);SendMessage(hProcess, WM_SETICON, ICON_BIG, hIcon);// 释放资源//icon.Dispose();}const int WM_SETICON = 0x80;const int ICON_SMALL = 0x0;const int ICON_BIG = 0x1;[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]extern static IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);static IntPtr hProcess{get { return Process.GetCurrentProcess().MainWindowHandle; }}static void CreateShortcut(string targetPath, string shortcutPath){// 创建 WshShell 对象WshShell shell = new WshShell();// 创建快捷方式对象IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);// 设置目标路径和其他属性shortcut.TargetPath = targetPath;shortcut.WorkingDirectory = System.IO.Path.GetDirectoryName(targetPath);shortcut.Description = "Shortcut Description";shortcut.Save();string exeFilePath = Assembly.GetEntryAssembly().Location;string iconPath = Path.ChangeExtension(exeFilePath, ".ico");SetShortcutIcon(shortcutPath, iconPath);}static void SetShortcutIcon(string shortcutPath, string iconPath){// 创建 WshShell 对象WshShell shell = new WshShell();// 读取快捷方式对象IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);// 设置图标路径shortcut.IconLocation = iconPath;// 保存快捷方式shortcut.Save();}}
http://www.lryc.cn/news/261323.html

相关文章:

  • 苹果计划将全球1/4的IPhone产能转移至印度
  • el-date-picker 选择一个或多个日期
  • 5个创建在线帮助文档的好方法!
  • 听GPT 讲Rust源代码--src/tools(14)
  • arcgis api for js 中使用API的代理页面(跨越配置)
  • Unity_FairyGUI发布导入Unity编辑器资源报错
  • 1.5【应用开发】缓冲区(二)
  • RTMP流设置超时时间失败
  • 如何一步步让MySQL支撑亿级流量
  • MFC CLXHHandleEngine动态库-自定义设置对话框使用
  • Python生成器(Generator)(继续更新...)
  • Spring Boot 3 整合 Mybatis-Plus 动态数据源实现多数据源切换
  • 快速学习C++中的模板
  • Pandas-DataFtame的索引与切片(第3讲)
  • MySQL低版本中:字符串中的数字、英文字符、汉字提取
  • 多窗口文件管理工具Q-Dir安装以及使用教程
  • Spring入门
  • Linux——进程创建与进程终止
  • 轻量封装WebGPU渲染系统示例<50>- Json数据描述材质等场景信息
  • AtCoder ABC周赛2023 12/10 (Sun) D题题解
  • 基于C/C++的rapidxml加载xml大文件 - 上部分翻译
  • 小程序使用Nodejs作为服务端,Nodejs与与MYSQL数据库相连
  • 深度解读 Cascades 查询优化器
  • Bash 操作审计和安全加固 —— 筑梦之路
  • C/C++常见面试知识总结(三)
  • AR眼镜_AR智能眼镜整机硬件方案定制
  • 2. 皇后的控制力
  • 南京邮电大学数据库实验二
  • 数据库 02-03 补充 SQL的子查询(where,from),子查询作为集合来比较some,exists,all(某一个,存在,所有)
  • 提升英语学习效率,尽在Eudic欧路词典 for Mac