篇章前言
来到第五篇,软件的 1.0 版就算告一段落,已经完全满足宝妈的需求,不过对于追求完美的开发者来说,软件还少了桌面图标,这得让它自动生成才是。
本系列博文并没有过细的提到WPF的基本知识,如一些XAML怎么写,怎么弹出对话框等等,这类教程文章网上太多了,没必要去深入写,何况这是开源项目,大家可以直接看代码,我更多的是贴一些方法工具类以及项目逻辑
桌面图标
一个完整的应用怎能少了桌面上的小图标,如何生成图标?如何开机启动?上代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
namespace YiZaiPlayer.Common
{
/// <summary>
///
/// </summary>
public class QuickIcon
{
public QuickIcon(string quickName)
{
QuickName = quickName;
}
/// <summary>
/// 快捷方式名称-任意自定义
/// </summary>
public string QuickName = "";
/// <summary>
/// 自动获取系统自动启动目录
/// </summary>
private string systemStartPath { get { return Environment.GetFolderPath(Environment.SpecialFolder.Startup); } }
/// <summary>
/// 自动获取程序完整路径
/// </summary>
private string appAllPath
{
get
{
string path = Process.GetCurrentProcess().MainModule.FileName;
if (path.Contains(".vshost"))
{
path = path.Replace(".vshost", "");
}
return path;
}
}
/// <summary>
/// 自动获取桌面目录
/// </summary>
private string desktopPath { get { return Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); } }
/// <summary>
/// 设置图标,生成桌面图标? 开机自动启动
/// </summary>
/// <param name="desktop"></param>
/// <param name="autostart"></param>
public void SetIcon(bool desktop = true, bool autostart = true)
{
try
{
if (autostart)//开机启动
{
//获取启动路径应用程序快捷方式的路径集合
List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);
//存在2个以快捷方式则保留一个快捷方式-避免重复多于
if (shortcutPaths.Count >= 2)
{
for (int i = 1; i < shortcutPaths.Count; i++)
{
DeleteFile(shortcutPaths[i]);
}
}
else if (shortcutPaths.Count < 1)//不存在则创建快捷方式
{
CreatShortcut(systemStartPath, QuickName, appAllPath, "");
}
}
else//开机不启动
{
//获取启动路径应用程序快捷方式的路径集合
List<string> shortcutPaths = GetQuickFromFolder(systemStartPath, appAllPath);
//存在快捷方式则遍历全部删除
if (shortcutPaths.Count > 0)
{
for (int i = 0; i < shortcutPaths.Count; i++)
{
DeleteFile(shortcutPaths[i]);
}
}
}
if (desktop)
{
//创建桌面快捷方式
CreateDesktopQuick(desktopPath, QuickName, appAllPath);
}
}
catch (Exception ex)
{
// throw;
}
}
/// <summary>
/// 获取指定文件夹下指定应用程序的快捷方式路径集合
/// </summary>
/// <param name="directory">文件夹</param>
/// <param name="targetPath">目标应用程序路径</param>
/// <returns>目标应用程序的快捷方式</returns>
private List<string> GetQuickFromFolder(string directory, string targetPath)
{
List<string> tempStrs = new List<string>();
tempStrs.Clear();
string tempStr = null;
string[] files = Directory.GetFiles(directory, "*.lnk");
if (files == null || files.Length < 1)
{
return tempStrs;
}
for (int i = 0; i < files.Length; i++)
{
tempStr = GetShortCutTarget(files[i]);
if (tempStr == targetPath)
{
tempStrs.Add(files[i]);
}
}
return tempStrs;
}
public static readonly Guid CLSID_WshShell = new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8");
public static string GetShortCutTarget(string lnk) // 取快捷方式目标
{
if (lnk != null && System.IO.File.Exists(lnk))
{
dynamic objWshShell = null, objShortcut = null;
try
{
objWshShell = Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_WshShell));
objShortcut = objWshShell.CreateShortcut(lnk);
Console.WriteLine(objShortcut.TargetPath);
return objShortcut.TargetPath;
}
finally
{
Marshal.ReleaseComObject(objShortcut);
Marshal.ReleaseComObject(objWshShell);
}
}
return string.Empty;
}
/// <summary>
/// 根据路径删除文件-用于取消自启时从计算机自启目录删除程序的快捷方式
/// </summary>
/// <param name="path">路径</param>
private void DeleteFile(string path)
{
FileAttributes attr = System.IO.File.GetAttributes(path);
if (attr == FileAttributes.Directory)
{
Directory.Delete(path, true);
}
else
{
System.IO.File.Delete(path);
}
}
/// <summary>
/// 在桌面上创建快捷方式
/// </summary>
/// <param name="desktopPath">桌面地址</param>
/// <param name="appPath">应用路径</param>
private void CreateDesktopQuick(string DesktopPath = "", string quickName = "", string AppPath = "")
{
DesktopPath = desktopPath;
AppPath = appAllPath;
List<string> shortcutPaths = GetQuickFromFolder(desktopPath, AppPath);
//如果没有则创建
if (shortcutPaths.Count < 1)
{
CreatShortcut(desktopPath, quickName, AppPath, "");
}
}
[ComImport]
[Guid("00021401-0000-0000-C000-000000000046")]
internal class ShellLink
{
}
[ComImport]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214F9-0000-0000-C000-000000000046")]
internal interface IShellLink
{
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, IntPtr pfd, int fFlags);
void GetIDList(out IntPtr ppidl);
void SetIDList(IntPtr pidl);
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName);
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName);
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath);
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir);
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath);
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs);
void GetHotkey(out short pwHotkey);
void SetHotkey(short wHotkey);
void GetShowCmd(out int piShowCmd);
void SetShowCmd(int iShowCmd);
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon);
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon);
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved);
void Resolve(IntPtr hwnd, int fFlags);
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile);
}
/// <summary>
/// 向目标路径创建指定文件的快捷方式
/// </summary>
/// <param name="desktopPath">快捷方式路径</param>
/// <param name="quickname">快捷方式名</param>
/// <param name="apppath">App路径</param>
/// <param name="description">提示信息</param>
private void CreatShortcut(string desktopPath, string quickname, string apppath, string description)
{
IShellLink link = (IShellLink)new ShellLink();
link.SetDescription(description);
link.SetPath(apppath); //指定文件路径
IPersistFile file = (IPersistFile)link;
string sfile = Path.Combine(desktopPath, quickname + ".lnk");
if (File.Exists(sfile))
File.Delete(sfile);
file.Save(sfile, false); //快捷方式保存到桌面
}
}
}
开机启动
在 App.xaml.cs 调用方法即可,快捷方式的名称,参数是否生成图标,是否开机启动
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
namespace YiZaiPlayer
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
public App()
{
new Common.QuickIcon("一仔播放器").SetIcon(true,true);
}
}
}
背景平铺
为了美观弄了个背景
关键参数 TileMode="Tile" Viewport="0,0,157,157" ViewportUnits="Absolute"
Viewport="0,0,157,157" 后面的157,157 是图片的宽和高
<Grid >
<Grid.Background>
<ImageBrush ImageSource="/Resource/cloud.png" TileMode="Tile" Opacity="0.1" Viewport="0,0,157,157" ViewportUnits="Absolute" ></ImageBrush>
</Grid.Background>
</Grid>
完美运行
开源地址
来自宝妈的需求 一仔播放器 WPF 开源项目: 宝妈的需求“一仔播放器” WPF 的开源项目 HandyControl、MwvmLight
QQ群:924610292
个人微信:jialongvip