Skip to content

Commit

Permalink
恢复 支持自定义背景颜色;新增 屏幕取色功能;新增 组件提示说明
Browse files Browse the repository at this point in the history
  • Loading branch information
XIU2 committed Sep 18, 2020
1 parent 6224ea1 commit 097beee
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 22 deletions.
56 changes: 54 additions & 2 deletions Form.Designer.cs

Large diffs are not rendered by default.

114 changes: 96 additions & 18 deletions Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using WebClient_cs;
using FileDropAdmin_cs;
using AppConfig_cs;
using Other_cs;
using Registry_cs;

namespace 磁贴美化小工具
Expand All @@ -21,8 +20,11 @@ public partial class Form : System.Windows.Forms.Form
private System.Threading.Mutex newMutex; // 互斥体
public FileDropAdmin FileDroper; // 管理员权限下拖放文件
private bool IsRunAsAdmin; // 是否为管理员权限
private bool ScreenColorFlag; // 取色
private bool NewTileState;
private string Now_VerInfo; // 软件版本号
private string SystemColor; // 系统主题色
private string UserColor; // 用户颜色
private string Config_Path; // 磁贴配置文件路径
private string Old_Shortcut_Path; // 快捷方式文件路径
private string Old_Square150x150Logo_Path; // 旧图片文件路径
Expand Down Expand Up @@ -66,17 +68,18 @@ private void Form_Load(object sender, EventArgs e) // 主窗口创建前
Check_Dll();

// 获取主题色并设置图片框背景颜色
SystemColor = Registry_SystemColor.Get_SystemColor();
PictureBox_磁贴图片预览.BackColor = PictureBox_磁贴图标预览.BackColor = Label_磁贴名称预览.BackColor = ColorTranslator.FromHtml("#" + SystemColor);
Recognize_Text_Color(SystemColor);
SystemColor = "#" + Registry_SystemColor.Get_SystemColor();
PictureBox_磁贴图片预览.BackColor = ColorTranslator.FromHtml(SystemColor);
NewTileState = Registry_Other.Get_NewTileState(); // 获取是否开启 2004 新版磁贴样式
Debug.Print(NewTileState.ToString());

// 磁贴预览标签背景透明
Label_磁贴名称预览.BackColor = Color.Transparent;
Label_磁贴名称预览.Parent = PictureBox_磁贴图片预览;
Label_磁贴名称预览.Location = new Point(5, 79);

// 检查是否以管理员身份运行
IsRunAsAdmin = Other.IsRunAsAdmin();
IsRunAsAdmin = Other_cs.Other.IsRunAsAdmin();
if (IsRunAsAdmin)
{
this.AllowDrop = false;
Expand Down Expand Up @@ -122,11 +125,11 @@ private void Kill_SameNameProcess() // 结束同名进程
}
}

private void Recognize_Text_Color(string strHxColor = "") // 智能识别文字颜色
private void Recognize_Text_Color(byte r = 0, byte g = 0, byte b = 0) // 智能识别文字颜色
{
if (PictureBox_磁贴图片预览.Image == null)
{
if (Convert.ToInt32(strHxColor.Substring(0, 2), 16) * 0.299 + Convert.ToInt32(strHxColor.Substring(2, 2), 16) * 0.578 + Convert.ToInt32(strHxColor.Substring(4, 2), 16) * 0.114 >= 168)
if (r * 0.299 + g * 0.578 + b * 0.114 >= 168)
{
Label_磁贴名称预览.ForeColor = Color.Black;
}
Expand Down Expand Up @@ -283,8 +286,23 @@ private void Read_Config() // 读入磁贴配置文件
XElement XML_Application = XML_xDoc.Element("Application");
XElement XML_VisualElements = XML_Application.Element("VisualElements");
//Debug.Print(XML_VisualElements.ToString());
//XML_VisualElements.Attribute("BackgroundColor").Value.ToString();
if (XML_VisualElements.Attribute("Square150x150Logo") != null) // 显示/隐藏磁贴文字
if (XML_VisualElements.Attribute("BackgroundColor") != null) // 磁贴背景颜色
{
if (NewTileState == true) // 如果已经开启新版磁贴样式,则使用系统主题色
{
UserColor = SystemColor;
}
else
{
UserColor = XML_VisualElements.Attribute("BackgroundColor").Value.ToString();
}
PictureBox_磁贴图片预览.BackColor = ColorTranslator.FromHtml(UserColor);
}
else
{
PictureBox_磁贴图片预览.BackColor = ColorTranslator.FromHtml(SystemColor);
}
if (XML_VisualElements.Attribute("ShowNameOnSquare150x150Logo") != null) // 显示/隐藏磁贴文字
{
if (XML_VisualElements.Attribute("ShowNameOnSquare150x150Logo").Value.ToString() == "off")
{
Expand All @@ -296,7 +314,7 @@ private void Read_Config() // 读入磁贴配置文件
}
}
//XML_VisualElements.Attribute("ForegroundText").Value.ToString();
if (XML_VisualElements.Attribute("Square150x150Logo") != null) // 大图
if (XML_VisualElements.Attribute("Square150x150Logo") != null) // 磁贴大图
{
string Temp_Square150x150Logo = XML_VisualElements.Attribute("Square150x150Logo").Value.ToString(); // 获取值
if (Temp_Square150x150Logo != "") // 如果不等于空,则判断文件是否存在
Expand All @@ -312,11 +330,11 @@ private void Read_Config() // 读入磁贴配置文件
Old_Square150x150Logo_Path = "";
}
}
/*if (XML_VisualElements.Attribute("Square70x70Logo") != null) // 小图
/*if (XML_VisualElements.Attribute("Square70x70Logo") != null) // 磁贴大图
{
string Temp_Square70x70Logo = XML_VisualElements.Attribute("Square70x70Logo").Value.ToString();
}*/
if (XML_VisualElements.Attribute("Lnk32x32Logo") != null) // 图标
if (XML_VisualElements.Attribute("Lnk32x32Logo") != null) // 磁贴图标
{
string Temp_Lnk32x32Logo = XML_VisualElements.Attribute("Lnk32x32Logo").Value.ToString(); // 获取值
if (Temp_Lnk32x32Logo != "") // 如果不等于空,则判断文件是否存在
Expand All @@ -338,9 +356,22 @@ private void Read_Config() // 读入磁贴配置文件
}
private void Write_Config() // 写出磁贴配置文件
{
string Temp_BackgroundColor;
string Temp_ShowNameOnSquare150x150Logo;
string Temp_ForegroundText;
string Temp_Square150x150Logo;

// 磁贴背景颜色
if (UserColor == "")
{
Temp_BackgroundColor = SystemColor;
}
else
{
Temp_BackgroundColor = UserColor;
}

// 磁贴是否显示文字
if (Button_显示文字.Text == "显示文字")
{
Temp_ShowNameOnSquare150x150Logo = "off";
Expand All @@ -349,6 +380,8 @@ private void Write_Config() // 写出磁贴配置文件
{
Temp_ShowNameOnSquare150x150Logo = "on";
}

// 磁贴文字颜色
if (Label_磁贴名称预览.ForeColor == Color.White)
{
Temp_ForegroundText = "light";
Expand All @@ -357,6 +390,8 @@ private void Write_Config() // 写出磁贴配置文件
{
Temp_ForegroundText = "dark";
}

// 磁贴图片
if (TextBox_磁贴图片.Text == "")
{
Temp_Square150x150Logo = "";
Expand All @@ -365,22 +400,23 @@ private void Write_Config() // 写出磁贴配置文件
{
Temp_Square150x150Logo = Path.GetFileNameWithoutExtension(TextBox_程序路径.Text) + ".150x150Logo" + Path.GetExtension(TextBox_磁贴图片.Text);
}
// 开始写入配置
XElement XML_Application = new XElement("Application",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));
XElement XML_VisualElements = new XElement("VisualElements",
new XAttribute("BackgroundColor", "#" + SystemColor),
new XAttribute("BackgroundColor", Temp_BackgroundColor),
new XAttribute("ShowNameOnSquare150x150Logo", Temp_ShowNameOnSquare150x150Logo),
new XAttribute("ForegroundText", Temp_ForegroundText),
new XAttribute("Square150x150Logo", Temp_Square150x150Logo),
new XAttribute("Square70x70Logo", Temp_Square150x150Logo),
new XAttribute("Lnk32x32Logo", TextBox_磁贴图标.Text)
);
XML_Application.Add(XML_VisualElements);
XML_Application.Save(Config_Path); //保存配置文件文件
Move_Img();
XML_Application.Save(Config_Path); // 保存配置文件文件
Copy_Img(); // 复制图片文件
}

private void Move_Img() // 移动磁贴图片
private void Copy_Img() // 复制磁贴图片
{
if (TextBox_磁贴图片.Text != "") // 图片路径不等于空时继续
{
Expand Down Expand Up @@ -699,10 +735,10 @@ private void TextBox_程序路径_TextChanged(object sender, EventArgs e) // 输
{
TextBox_磁贴名称.Text = Path.GetFileNameWithoutExtension(TextBox_程序路径.Text);
}
Debug.Print("333" + TextBox_磁贴名称.Text);
//Debug.Print("333" + TextBox_磁贴名称.Text);
// 设置程序配置文件路径
Config_Path = Path.GetDirectoryName(TextBox_程序路径.Text) + @"\" + Path.GetFileNameWithoutExtension(TextBox_程序路径.Text) + ".VisualElementsManifest.xml";
Debug.Print(Config_Path);
//Debug.Print(Config_Path);
// 磁贴图片、磁贴图标清空
TextBox_磁贴图片.Text = TextBox_磁贴图标.Text = "";
PictureBox_磁贴图片预览.Image = PictureBox_磁贴图标预览.Image = null;
Expand Down Expand Up @@ -911,5 +947,47 @@ private void TextBox_磁贴图标_Leave_2()
TextBox_磁贴图标.BackColor = Color.WhiteSmoke;
Label_磁贴图标.BackColor = Color.WhiteSmoke;
}

private void PictureBox_磁贴图片预览_MouseDown(object sender, MouseEventArgs e) // 图片预览框 鼠标按下开始取色
{
if (e.Button == MouseButtons.Left)
{
if (NewTileState == false)
{
PictureBox_磁贴图片预览.Cursor = Cursors.Cross;
ScreenColorFlag = true;
}
}
}
private void PictureBox_磁贴图片预览_MouseMove(object sender, MouseEventArgs e) // 图片预览框 鼠标移动 取色
{
if (ScreenColorFlag)
{
UserColor = "#" + Other_cs.Color.Get_ScreenColor(MousePosition.X, MousePosition.Y);
PictureBox_磁贴图片预览.BackColor = ColorTranslator.FromHtml(UserColor);
}

}
private void PictureBox_磁贴图片预览_MouseUp(object sender, MouseEventArgs e) // 图片预览框 鼠标放开结束取色
{
if (e.Button == MouseButtons.Left)
{
PictureBox_磁贴图片预览.Cursor = Cursors.Default;
ScreenColorFlag = false;
}
}
private void PictureBox_磁贴图片预览_MouseClick(object sender, MouseEventArgs e) // 图片预览框 鼠标右击恢复默认颜色
{
if (e.Button == MouseButtons.Right)
{
PictureBox_磁贴图片预览.BackColor = ColorTranslator.FromHtml(SystemColor);
}
}

private void PictureBox_磁贴图片预览_BackColorChanged(object sender, EventArgs e) // 统一其他预览控件的背景颜色
{
PictureBox_磁贴图标预览.BackColor = Label_磁贴名称预览.BackColor = PictureBox_磁贴图片预览.BackColor;
Recognize_Text_Color(PictureBox_磁贴图片预览.BackColor.R, PictureBox_磁贴图片预览.BackColor.G, PictureBox_磁贴图片预览.BackColor.B); // 智能识别文字颜色
}
}
}
3 changes: 3 additions & 0 deletions Form.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="toolTip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down
24 changes: 24 additions & 0 deletions Other.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,28 @@ public static bool IsRunAsAdmin()
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
public class Color
{
[System.Runtime.InteropServices.DllImport("user32")]
private static extern int GetDC(int hwnd); // 获取DC
[System.Runtime.InteropServices.DllImport("user32")]
private static extern int ReleaseDC(int hwnd, int hdc); // 释放DC
[System.Runtime.InteropServices.DllImport("gdi32")]
private static extern int GetPixel(int hdc, int x, int y);
public static string Get_ScreenColor(int x, int y)
{
//Debug.Print(x + "," + y); // 把坐标显示到窗口上
int Temp_hDc = GetDC(0);
int c = GetPixel(Temp_hDc, x, y);
int r = (c & 0xFF); // 转换R
int g = (c & 0xFF00) / 256; // 转换G
int b = (c & 0xFF0000) / 65536; // 转换B
//Debug.Print(c.ToString()); // 输出10进制颜色
//Debug.Print(r.ToString("x").PadLeft(2, '0') + g.ToString("x").PadLeft(2, '0') + b.ToString("x").PadLeft(2, '0')); // 输出16进制颜色
//Debug.Print(r.ToString() + ',' + g.ToString() + ',' + b.ToString()); // 输出RGB
//PictureBox_磁贴图片预览.BackColor = Color.FromArgb(r, g, b); //设置颜色框
ReleaseDC(0, Temp_hDc);
return (r.ToString("x").PadLeft(2, '0') + g.ToString("x").PadLeft(2, '0') + b.ToString("x").PadLeft(2, '0'));
}
}
}
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
//通过使用 "*",如下所示:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("4.0.6.0")]
[assembly: AssemblyFileVersion("4.0.6.0")]
[assembly: AssemblyVersion("4.0.7.0")]
[assembly: AssemblyFileVersion("4.0.7.0")]
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@

## 使用说明

> 使用前请先鼠标指向各个组件查看 **相关提示说明!**
* 方式一:拖放 **应用程序、快捷方式、开始菜单磁贴(非UWP)** 到软件内,即可编辑磁贴样式
* 方式二:右键 **应用程序、快捷方式** 选择 [自定义并固定到"开始"屏幕],即可编辑磁贴样式
—— 方式二需要勾选软件中的 **[添加右键菜单]**
Expand Down
21 changes: 21 additions & 0 deletions Registry.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using Microsoft.Win32;

namespace Registry_cs
Expand Down Expand Up @@ -243,4 +244,24 @@ public static string Get_CurrentBuildNumber()
return (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentBuildNumber", null);
}
}
public class Registry_Other
{
/// <summary>
/// 获取系统是否开启 2004 新版磁贴样式
/// </summary>
/// <returns>开启返回 true 关闭返回 false</returns>
public static bool Get_NewTileState()
{
if (Convert.ToInt32(Registry_SystemVersion.Get_ReleaseId()) < 2004)
{
return false;
}
Debug.Print(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FeatureManagement\Overrides\0\2093230218", "EnabledState", null).ToString());
if (Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FeatureManagement\Overrides\0\2093230218", "EnabledState", null) == null)
{
return false;
}
return true;
}
}
}

0 comments on commit 097beee

Please sign in to comment.