【WPF】Toolkit(一个项目)的要点总结

2022-11-11 11:55:37 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

架构相关

1. 插件式开发:MEF

具体怎么使用可参考百度 Demo (密码: k8ck)

2. 备份机制(项目独有功能)

3. 镜像机制(项目独有功能)

4. 分模块记录日志

(转)非常完善的Log4net详细说明

UI相关

1. 多语言

读取系统的显示语言(displayLanguage),显示语言的定义是:假如你的系统现在是中文的,你把它切换到了英文,但是英文的语言包并没有下载下来或者并没有将英文设置为显示语言,那么注销系统再登录之后,你系统显示的将还是中文。此时中文就是显示语言。

  • 显示语言的获取 通过System.Globalization.CultureInfo.CurrentUICulture.TwoLetterISOLanguageName可以拿到两个字符的ISO语言名称,如:zh,nl,en,fr,ja等。通过System.Globalization.CultureInfo.CurrentUICulture.Name获取区域名称,如:zh-CN,zh-SG,zh-TW,nl-NL,en-US,fr-FR,ja-JP等。第二个是第一个的子类。
  • 处理翻译好的多语言文件 每种翻译好的语言都是一个ResourceDictionary的文件,对于同一个字符串这N个文件都用同一个key,只是value是不同的翻译。这里以英语的ResourceDictionary文件为基准(称为file1),读取当前显示语言对应的ResourceDictionary(称为file2)。将file2中每个key的value,覆盖file1中对应key的value上。这样如果file2中有哪些字符串没来得及翻译,在程序中将以英语的形式展示。
  • 将ResourceDictionary添加的主程序的Resource中 this.Resources.MergedDictionaries.Add(languageResource);
2. 鼠标位置捕获

可参考:【WPF】DPI对控件定位产生的影响

3. 在win10弹出toast

可参考:【WPF】右下角弹出自定义通知样式(Notification)——简单教程

4. 自定义日历控件

可参考:【C#】wpf自定义calendar日期选择控件的样式

5. 高对比度主题处理
  • 如果.Net Framework>=4.5 可订阅SystemParameters.StaticPropertyChanged事件,然后在订阅方法里写:
代码语言:javascript复制
 private void SystemParameters_StaticPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        { 
   
            if (e.PropertyName == "HighContrast" && SystemParameters.HighContrast)
            { 
   
                var item = SystemParameters.HighContrastKey;

                if (SystemColors.WindowBrush.Color == Colors.White)
                { 
   
                    //高对比度白色
                    ell.Fill = new SolidColorBrush(Colors.Black);
                }
                else
                { 
   
                    //高对比黑色
                    ell.Fill = new SolidColorBrush(Colors.White);
                }
            }
        }
  • 如果.Net Framework<4.5 可订阅SystemEvents.UserPreferenceChanged,然后写:
代码语言:javascript复制
 private void SystemEvents_UserPreferenceChanged(object sender, UserPreferenceChangedEventArgs e)
        { 
   

            if(SystemParameters.HighContrast)
            { 
   
                if (SystemColors.WindowBrush.Color == Colors.White)
                { 
   
                    ell.Fill = new SolidColorBrush(Colors.Black);
                }
                else
                { 
   
                    ell.Fill = new SolidColorBrush(Colors.White);
                }
            }
        }
6. gif动图的显示

可参考:【C#】wpf添加gif动图支持

文件设备相关

1. 通过shell处理系统的特殊目录,读取相关属性

可参考:【C#】WindowsAPICodePack-Shell使用教程

2. 读取文件和文件夹的缩略图

可参考: 【C#】获取任意文件的缩略图

3. 长路径管理(解决PathTooLong的异常)

可参考:【C#】简单解决PathTooLong的Exception

4. 磁盘的格式化
  • 调用系统的格式化框:
代码语言:javascript复制
        [DllImport("shell32.dll")]
        public static extern uint SHFormatDrive(IntPtr hwnd, uint drive, uint fmtID, uint option);

使用时:

代码语言:javascript复制
                    int index = driverletter - 'a';//driverletter是:c,d,e,f等
                    SHFormatDrive(_pageHandle, Convert.ToUInt32(index), 0xFFFF, 0x0001);
  • 自动格式化为NTFS(一个帮助类)
代码语言:javascript复制
    public class DriveManager
{ 

#region SetLabel
/// <summary>
/// set a drive label to the desired value
/// </summary>
/// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
/// <param name="label">label for the drive</param>
/// <returns>true if success, false if failure</returns>
public static bool SetLabel(char driveLetter, string label = "")
{ 

#region args check
if (!Char.IsLetter(driveLetter))
{ 

return false;
}
if (label == null)
{ 

label = "";
}
#endregion
try
{ 

DriveInfo di = DriveInfo.GetDrives()
.Where(d => d.Name.StartsWith(driveLetter.ToString()))
.FirstOrDefault();
di.VolumeLabel = label;
return true;
}
catch (Exception)
{ 

return false;
}
}
#endregion
#region FormatDrive
/// <summary>
/// Format a drive using the best available method
/// </summary>
/// <param name="driveLetter">drive letter. Example : 'A:', 'B:', 'C:', 'D:', ..., 'Z:'.</param>
/// <param name="label">label for the drive</param>
/// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
/// <param name="quickFormat">quick formatting?</param>
/// <param name="enableCompression">enable drive compression?</param>
/// <param name="clusterSize">cluster size (default=null for auto). Possible value depends on the file system : 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, ...</param>
/// <returns>true if success, false if failure</returns>
public static bool FormatDrive(string driveLetter, string label = "", string fileSystem = "NTFS", bool quickFormat = true, bool enableCompression = false, int? clusterSize = null)
{ 

if (string.IsNullOrEmpty(driveLetter) || driveLetter.Length != 2) return false;
return FormatDrive_CommandLine(driveLetter, label, fileSystem, quickFormat, enableCompression, clusterSize);
}
#endregion
#region FormatDrive_CommandLine
/// <summary>
/// Format a drive using Format.com windows file
/// </summary>
/// <param name="driveLetter">drive letter. Example : 'A:', 'B:', 'C:', 'D:', ..., 'Z:'.</param>
/// <param name="label">label for the drive</param>
/// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
/// <param name="quickFormat">quick formatting?</param>
/// <param name="enableCompression">enable drive compression?</param>
/// <param name="clusterSize">cluster size (default=null for auto). Possible value depends on the file system : 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, ...</param>
/// <returns>true if success, false if failure</returns>
public static bool FormatDrive_CommandLine(string driveLetter, string label = "", string fileSystem = "NTFS", bool quickFormat = true, bool enableCompression = false, int? clusterSize = null)
{ 

#region args check
if (driveLetter.Length!=2 ||
!IsFileSystemValid(fileSystem))
{ 

return false;
}
#endregion
bool success = false;
try
{ 

var di = new DriveInfo(driveLetter);
var psi = new ProcessStartInfo();
psi.FileName = "format.com";
psi.WorkingDirectory = Environment.SystemDirectory;
psi.Arguments = "/FS:"   fileSystem  
" /Y"  
" /V:"   label  
(quickFormat ? " /Q" : "")  
((fileSystem == "NTFS" && enableCompression) ? " /C" : "")  
(clusterSize.HasValue ? " /A:"   clusterSize.Value : "")  
" "   driveLetter;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
var formatProcess = Process.Start(psi);
var swStandardInput = formatProcess.StandardInput;
swStandardInput.WriteLine();
formatProcess.WaitForExit();
success = true;
}
catch (Exception) { 
 }
return success;
}
#endregion
#region FormatDrive_Shell32
#region interop
// http://msdn.microsoft.com/en-us/library/windows/desktop/bb762169(v=vs.85).aspx
[DllImport("shell32.dll")]
private static extern uint SHFormatDrive(IntPtr hwnd, uint drive, SHFormatFlags fmtID, SHFormatOptions options);
private enum SHFormatFlags : uint
{ 

SHFMT_ID_DEFAULT = 0xFFFF,
/// <summary>
/// A general error occured while formatting. This is not an indication that the drive cannot be formatted though.
/// </summary>
SHFMT_ERROR = 0xFFFFFFFF,
/// <summary>
/// The drive format was cancelled by user/OS.
/// </summary>
SHFMT_CANCEL = 0xFFFFFFFE,
/// <summary>
/// A serious error occured while formatting. The drive is unable to be formatted by the OS.
/// </summary>
SHFMT_NOFORMAT = 0xFFFFFFD
}
[Flags]
private enum SHFormatOptions : uint
{ 

/// <summary>
/// Full formatting
/// </summary>
SHFMT_OPT_COMPLETE = 0x0,
/// <summary>
/// Quick Format
/// </summary>
SHFMT_OPT_FULL = 0x1,
/// <summary>
/// MS-DOS System Boot Disk
/// </summary>
SHFMT_OPT_SYSONLY = 0x2
}
#endregion
/// <summary>
/// Format a drive using Shell32.dll
/// </summary>
/// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
/// <param name="label">label for the drive</param>
/// <param name="quickFormat">quick formatting?</param>
/// <returns>true if success, false if failure</returns>
[Obsolete("Unsupported by Microsoft nowadays. Prefer the FormatDrive() or FormatDrive_CommandLine() methods")]
public static bool FormatDrive_Shell32(char driveLetter, string label = "", bool quickFormat = true)
{ 

#region args check
if (!Char.IsLetter(driveLetter))
{ 

return false;
}
#endregion
bool success = false;
string drive = driveLetter   ":";
try
{ 

var di = new DriveInfo(drive);
var bytes = Encoding.ASCII.GetBytes(di.Name.ToCharArray());
uint driveNumber = Convert.ToUInt32(bytes[0] - Encoding.ASCII.GetBytes(new[] { 
 'A' })[0]);
var options = SHFormatOptions.SHFMT_OPT_COMPLETE;
if (quickFormat)
options = SHFormatOptions.SHFMT_OPT_FULL;
uint returnCode = SHFormatDrive(IntPtr.Zero, driveNumber, SHFormatFlags.SHFMT_ID_DEFAULT, options);
if (returnCode == (uint)SHFormatFlags.SHFMT_ERROR)
throw new Exception("An error occurred during the format. This does not indicate that the drive is unformattable.");
else if (returnCode == (uint)SHFormatFlags.SHFMT_CANCEL)
throw new OperationCanceledException("The format was canceled.");
else if (returnCode == (uint)SHFormatFlags.SHFMT_NOFORMAT)
throw new IOException("The drive cannot be formatted.");
SetLabel(driveLetter, label);
success = true;
}
catch (Exception) { 
 }
return success;
}
#endregion
#region FormatDrive_Win32Api
// http://msdn.microsoft.com/en-us/library/aa394515(VS.85).aspx
/// <summary>
/// Format a drive using Win32 API
/// </summary>
/// <param name="driveLetter">drive letter. Example : 'A', 'B', 'C', 'D', ..., 'Z'.</param>
/// <param name="label">label for the drive</param>
/// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
/// <param name="quickFormat">quick formatting?</param>
/// <param name="enableCompression">enable drive compression?</param>
/// <param name="clusterSize">cluster size. Possible value depends on the file system : 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, ...</param>
/// <returns>true if success, false if failure</returns>
[Obsolete("Might have troubles formatting ram drives. Prefer the FormatDrive() or FormatDrive_CommandLine() methods")]
public static bool FormatDrive_Win32Api(char driveLetter, string label = "", string fileSystem = "NTFS", bool quickFormat = true, bool enableCompression = false, int clusterSize = 8192)
{ 

#region args check
if (!Char.IsLetter(driveLetter) ||
!IsFileSystemValid(fileSystem))
{ 

return false;
}
#endregion
bool success = false;
try
{ 

var moSearcher = new ManagementObjectSearcher(@"SELECT * FROM Win32_Volume WHERE DriveLetter='"   driveLetter   ":'");
foreach (ManagementObject mo in moSearcher.Get())
{ 

mo.InvokeMethod("Format", new object[] { 
 fileSystem, quickFormat, clusterSize, label, enableCompression });
success = true;
}
}
catch (Exception)
{ 

success = false;
}
return success;
}
#endregion
#region IsFileSystemValid
/// <summary>
/// test if the provided filesystem value is valid
/// </summary>
/// <param name="fileSystem">file system. Possible values : "FAT", "FAT32", "EXFAT", "NTFS", "UDF".</param>
/// <returns>true if valid, false if invalid</returns>
public static bool IsFileSystemValid(string fileSystem)
{ 

#region args check
if (fileSystem == null)
{ 

return false;
}
#endregion
switch (fileSystem)
{ 

case "FAT":
case "FAT32":
case "EXFAT":
case "NTFS":
case "UDF":
return true;
default:
return false;
}
}
#endregion
}    
5.USB设备插拔监控
  • WMI查询语句(WQL)
代码语言:javascript复制
private const string RemovableDiskRemovedWatcherQuery = "SELECT * FROM __InstanceDeletionEvent WITHIN 5 WHERE TargetInstance ISA "Win32_DiskDrive"";//设备移除监控语句
private const string RemovableDiskAddedWatcherQuery = "SELECT * FROM __InstanceCreationEvent WITHIN 15 WHERE TargetInstance ISA "Win32_DiskDrive"";//设备插入监控语句
private const string RemovableDiskQuery = "SELECT SerialNumber, DeviceID, Model, Description, Name, Caption, PNPDeviceID, InterfaceType, MediaType, Index, Size, Partitions, FirmwareRevision from Win32_DiskDrive Where InterfaceType = 'USB'";//当前已插入的USB设备
  • 监控设备的移除和添加

设置操作范围

代码语言:javascript复制
ManagementScope scope = new ManagementScope("root\CIMV2"); 

移除监控

代码语言:javascript复制
void DiskRemovedWatcher()
{
WqlEventQuery query = new WqlEventQuery(RemovableDiskRemovedWatcherQuery);
_diskRemovedWatcher = new ManagementEventWatcher(scope, query);
_diskRemovedWatcher.EventArrived  = (sender, e) =>
{
// occurs when a disk drive is removed
using (
ManagementBaseObject mbo =
e.NewEvent[WMIConstants.TargetInstanceProperty] as ManagementBaseObject)
{
if (mbo != null)
{
//todo
}
}
};
}

添加监控

代码语言:javascript复制
private void DiskAddedWatcher()
{
WqlEventQuery query = new WqlEventQuery(RemovableDiskAddedWatcherQuery);
_diskAddedWatcher = new ManagementEventWatcher(scope, query);
_diskAddedWatcher.EventArrived  = (sender, e) =>
{
// occurs when a disk drive is added
using (
ManagementBaseObject mbo = e.NewEvent[WMIConstants.TargetInstanceProperty] as ManagementBaseObject)
{
if (mbo != null)
{
//todo
}
}
};
}

当前已插入的USB设备

代码语言:javascript复制
using (ManagementObjectSearcher mosDisks = new ManagementObjectSearcher(RemovableDiskQuery))
{       
// Loop through each object (disk) retrieved by WMI
foreach (ManagementObject moDisk in mosDisks.Get())
{
DasBasicPart disk = GetDisk(moDisk);
disks.Add(disk);
}
}

一个方便的WMI测试工具: Win+R,然后输入:wbemtest.exe,回车即可。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/186849.html原文链接:https://javaforall.cn

0 人点赞