Skip to content

Commit

Permalink
修复 磁贴图片长度/宽度低于 100 像素时报错的问题。
Browse files Browse the repository at this point in the history
修复 磁贴预览框的图标边缘显示锯齿的问题。
  • Loading branch information
XIU2 committed Sep 20, 2020
1 parent 29e20ae commit 644a301
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 45 deletions.
3 changes: 2 additions & 1 deletion File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static string Get_Shortcut_TargetPath_Array(string[] Shortcut_Path, strin
{
foreach (string Temp_Shortcut_Path in Shortcut_Path)
{
Debug.Print(Temp_Shortcut_Path);
//Debug.Print(Temp_Shortcut_Path);
if (Get_Shortcut_TargetPath(Temp_Shortcut_Path) == Path)
{
return Temp_Shortcut_Path;
Expand All @@ -42,6 +42,7 @@ public static string Get_Shortcut_TargetPath_Array(string[] Shortcut_Path, strin
}
return null;
}

/// <summary>
/// 创建快捷方式
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion Form.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 69 additions & 39 deletions Form.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,22 +140,21 @@ public Form(string[] args)
}
else
{
// 创建一份磁贴(预览框)背景颜色的 Bitmap,用来在遇到有透明图层(png)的图片时,透明的像素点以背景颜色为准,保证磁贴颜色识别准确
// 创建一份磁贴(预览框)背景颜色的 Bitmap,在遇到有透明图层(png)的图片时,透明的像素点以背景颜色为准,保证磁贴颜色识别准确
Bitmap BackColor = new Bitmap(100, 100);
Graphics Temp_BackColor = Graphics.FromImage(BackColor);
Temp_BackColor.Clear(PictureBox_磁贴图片预览.BackColor);
Temp_BackColor.Dispose();
Color Back_PixelColor = BackColor.GetPixel(50, 50);

Bitmap ImageColor = (Bitmap)PictureBox_磁贴图片预览.Image;
ImageColor.SetResolution(100, 100);
Bitmap ImageColor = new Bitmap((Bitmap)PictureBox_磁贴图片预览.Image, new Size(100, 100)); // 和图片框一样强制拉伸到 100*100 像素
double Red = 0, Green = 0, Blue = 0;
//double Alpha = 0;
for (int temp_y = 0; temp_y < 16; temp_y++)
for (int temp_y = 0; temp_y < 30; temp_y++)
{
for (int temp_x = 0; temp_x < 92; temp_x++)
{
Color Image_PixelColor = ImageColor.GetPixel(5 + temp_x, 79 + temp_y);
Color Image_PixelColor = ImageColor.GetPixel(5 + temp_x, 65 + temp_y);
if (Image_PixelColor.A == 0)
{
//Alpha += Back_PixelColor.A;
Expand All @@ -175,11 +174,11 @@ public Form(string[] args)
}
//BackColor.Dispose();
//ImageColor.Dispose();
//Alpha /= 1472;
Red /= 1472;
Green /= 1472;
Blue /= 1472;
//Debug.Print(Alpha.ToString() + " " + Red.ToString() + " " + Green.ToString() + " " + Blue.ToString() + " " + (Red * 0.299 + Green * 0.578 + Blue * 0.114).ToString());
//Alpha /= 2760;
Red /= 2760;
Green /= 2760;
Blue /= 2760;
//Debug.Print(Red.ToString() + " " + Green.ToString() + " " + Blue.ToString() + " " + (Red * 0.299 + Green * 0.578 + Blue * 0.114).ToString());
if (Red * 0.299 + Green * 0.578 + Blue * 0.114 >= 168)
{
Label_磁贴名称预览.ForeColor = Color.Black;
Expand Down Expand Up @@ -400,7 +399,7 @@ public Form(string[] args)
{
Temp_Square150x150Logo = Path.GetFileNameWithoutExtension(TextBox_程序路径.Text) + ".150x150Logo" + Path.GetExtension(TextBox_磁贴图片.Text);
}
Debug.Print(Temp_BackgroundColor + "|" + Temp_ShowNameOnSquare150x150Logo + "|" + Temp_ForegroundText + "|" + Temp_Square150x150Logo + "|" + TextBox_磁贴图标.Text);
//Debug.Print(Temp_BackgroundColor + "|" + Temp_ShowNameOnSquare150x150Logo + "|" + Temp_ForegroundText + "|" + Temp_Square150x150Logo + "|" + TextBox_磁贴图标.Text);
// 开始写入配置
XElement XML_Application = new XElement("Application",
new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"));
Expand Down Expand Up @@ -679,24 +678,7 @@ private void Button_显示文字_TextChanged(object sender, EventArgs e)
}
}

private void Label_程序路径_MouseClick(object sender, MouseEventArgs e) // 点击标签后,置焦点为输入框,下同
{
TextBox_程序路径.Focus();
}
private void Label_磁贴名称_MouseClick(object sender, MouseEventArgs e)
{
TextBox_磁贴名称.Focus();
}
private void Label_磁贴图片_MouseClick(object sender, MouseEventArgs e)
{
TextBox_磁贴图片.Focus();
}
private void Label_磁贴图标_MouseClick(object sender, MouseEventArgs e)
{
TextBox_磁贴图标.Focus();
}

private void TextBox_程序路径_TextChanged(object sender, EventArgs e) // 输入框内容更改后,置焦点为输入框
private void TextBox_程序路径_TextChanged(object sender, EventArgs e) // 内容更改后,读入程序路径
{
TextBox_程序路径_Leave_2();
if (TextBox_程序路径.Text != "")
Expand Down Expand Up @@ -759,17 +741,23 @@ private void TextBox_磁贴名称_TextChanged(object sender, EventArgs e)
Button_添加磁贴.Enabled = true; // 磁贴名称不等于空时,允许添加磁贴按钮
}
}
private void TextBox_磁贴图片_TextChanged(object sender, EventArgs e)
private void TextBox_磁贴图片_TextChanged(object sender, EventArgs e) // 内容更改后,读入图片
{
if (TextBox_磁贴图片.Text != "")
{
if (System.IO.File.Exists(TextBox_磁贴图片.Text))
{
if (PictureBox_磁贴图片预览.Image != null)
{
PictureBox_磁贴图片预览.Image.Dispose();
PictureBox_磁贴图片预览.Image.Dispose(); // 释放旧资源
}
PictureBox_磁贴图片预览.Image = Image.FromFile(TextBox_磁贴图片.Text);
FileStream Temp_ImageStream = new FileStream(TextBox_磁贴图片.Text, FileMode.Open, FileAccess.Read); // 打开文件流
int Temp_ImageByteLength = (int)Temp_ImageStream.Length;
byte[] Temp_ImageFileBytes = new byte[Temp_ImageByteLength];
Temp_ImageStream.Read(Temp_ImageFileBytes, 0, Temp_ImageByteLength);
Temp_ImageStream.Close(); // 文件流关闭,文件解除锁定
PictureBox_磁贴图片预览.Image = Image.FromStream(new MemoryStream(Temp_ImageFileBytes)); // 从流中读入图片
//PictureBox_磁贴图片预览.Image = Image.FromFile(TextBox_磁贴图片.Text); // 旧载入方式,会锁定文件
Recognize_Text_Color(); // 识别图片颜色
TextBox_磁贴图标.Text = "";
PictureBox_磁贴图标预览.Image = null;
Expand All @@ -790,19 +778,32 @@ private void TextBox_磁贴图片_TextChanged(object sender, EventArgs e)
TextBox_磁贴图片_Leave_2();
}
}
private void TextBox_磁贴图标_TextChanged(object sender, EventArgs e)
private void TextBox_磁贴图标_TextChanged(object sender, EventArgs e) // 内容更改后,读入图标
{
if(TextBox_磁贴图标.Text != "")
{
if (System.IO.File.Exists(TextBox_磁贴图标.Text))
{
if (PictureBox_磁贴图标预览.Image != null)
{
PictureBox_磁贴图标预览.Image.Dispose(); // 释放旧资源
}
if (Path.GetExtension(TextBox_磁贴图标.Text).ToLower() == ".exe")
{
PictureBox_磁贴图标预览.Image = Bitmap.FromHicon(System.Drawing.Icon.ExtractAssociatedIcon(TextBox_磁贴图标.Text).Handle);
PictureBox_磁贴图标预览.Image = System.Drawing.Icon.ExtractAssociatedIcon(TextBox_磁贴图标.Text).ToBitmap();
//PictureBox_磁贴图标预览.Image = Bitmap.FromHicon(System.Drawing.Icon.ExtractAssociatedIcon(TextBox_磁贴图标.Text).Handle); // 旧载入方式,有锯齿
}
else if(Path.GetExtension(TextBox_磁贴图标.Text).ToLower() == ".ico")
{
PictureBox_磁贴图标预览.Image = Bitmap.FromHicon(new Icon(TextBox_磁贴图标.Text, new Size(32, 32)).Handle);
FileStream Temp_IconStream = new FileStream(TextBox_磁贴图标.Text, FileMode.Open, FileAccess.Read); // 打开文件流
int Temp_IconByteLength = (int)Temp_IconStream.Length;
byte[] Temp_IconFileBytes = new byte[Temp_IconByteLength];
Temp_IconStream.Read(Temp_IconFileBytes, 0, Temp_IconByteLength);
Temp_IconStream.Close(); // 文件流关闭,文件解除锁定
PictureBox_磁贴图标预览.Image = Image.FromStream(new MemoryStream(Temp_IconFileBytes)); // 从流中读入图标

//PictureBox_磁贴图标预览.ImageLocation = TextBox_磁贴图标.Text; // 旧载入方式,无锯齿,但会锁定文件
//PictureBox_磁贴图标预览.Image = Bitmap.FromHicon(new Icon(TextBox_磁贴图标.Text, 32, 32).Handle); // 旧载入方式,有锯齿
}
TextBox_磁贴图片.Text = "";
PictureBox_磁贴图片预览.Image = null;
Expand Down Expand Up @@ -831,7 +832,7 @@ private void TextBox_磁贴图标_TextChanged(object sender, EventArgs e)
{
TextBox_磁贴图片.Text = null;
PictureBox_磁贴图片预览.Image = null;
if (TextBox_磁贴图标.Text == "") // 如果图标也是空的话,那就指定为程序文件
if (TextBox_磁贴图标.Text == "") // 如果图标路径也是空的话,那就指定为程序文件
{
TextBox_磁贴图标.Text = TextBox_程序路径.Text;
}
Expand All @@ -841,8 +842,15 @@ private void TextBox_磁贴图标_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 8)
{
TextBox_磁贴图标.Text = null;
PictureBox_磁贴图标预览.Image = null;
if (TextBox_磁贴图标.Text != TextBox_程序路径.Text) // 如果图标路径等于程序路径,那就没必要继续无意义动作了
{
TextBox_磁贴图标.Text = null;
PictureBox_磁贴图标预览.Image = null;
if (TextBox_磁贴图片.Text == "") // 如果图片路径也是空的话,那就指定图标为程序路径
{
TextBox_磁贴图标.Text = TextBox_程序路径.Text;
}
}
}
}
private void TextBox_程序路径_KeyPress(object sender, KeyPressEventArgs e)
Expand All @@ -852,6 +860,7 @@ private void TextBox_程序路径_KeyPress(object sender, KeyPressEventArgs e)
Init_Config();
}
}

private void TextBox_程序路径_Enter(object sender, EventArgs e) // 输入框获得焦点后,置顶并修改背景颜色,下同
{
TextBox_程序路径.BringToFront();
Expand Down Expand Up @@ -940,6 +949,10 @@ private void TextBox_磁贴图标_Leave_2()
if (TextBox_磁贴图标.Text == "")
{
TextBox_磁贴图标.SendToBack();
if (PictureBox_磁贴图标预览.Image != null)
{
PictureBox_磁贴图标预览.Image.Dispose();
}
}
else
{
Expand All @@ -948,7 +961,24 @@ private void TextBox_磁贴图标_Leave_2()
TextBox_磁贴图标.BackColor = Color.WhiteSmoke;
Label_磁贴图标.BackColor = Color.WhiteSmoke;
}


private void Label_程序路径_MouseClick(object sender, MouseEventArgs e) // 点击标签后,置焦点为输入框,下同
{
TextBox_程序路径.Focus();
}
private void Label_磁贴名称_MouseClick(object sender, MouseEventArgs e)
{
TextBox_磁贴名称.Focus();
}
private void Label_磁贴图片_MouseClick(object sender, MouseEventArgs e)
{
TextBox_磁贴图片.Focus();
}
private void Label_磁贴图标_MouseClick(object sender, MouseEventArgs e)
{
TextBox_磁贴图标.Focus();
}

private void PictureBox_磁贴图片预览_MouseDown(object sender, MouseEventArgs e) // 图片预览框 鼠标按下开始取色
{
if (e.Button == MouseButtons.Left)
Expand Down
7 changes: 6 additions & 1 deletion Other.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public class Color
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);
/// <summary>
/// 获取屏幕指定像素点HEX颜色值
/// </summary>
/// <param name="x">X</param>
/// <param name="y">Y</param>
/// <returns>HEX颜色值,不带 #</returns>
public static string Get_ScreenColor(int x, int y)
{
//Debug.Print(x + "," + y); // 把坐标显示到窗口上
Expand All @@ -34,7 +40,6 @@ public static string Get_ScreenColor(int x, int y)
//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'));
}
Expand Down
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.8.0")]
[assembly: AssemblyFileVersion("4.0.8.0")]
[assembly: AssemblyVersion("4.0.9.0")]
[assembly: AssemblyFileVersion("4.0.9.0")]
10 changes: 9 additions & 1 deletion Registry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static bool Check(string Name, string Extension = "")
return true;
}
}

/// <summary>
/// 添加右键菜单
/// </summary>
Expand Down Expand Up @@ -105,6 +106,7 @@ public static bool Add(string Name, string Path, bool NoIcon = true, string Icon
}
}
}

/// <summary>
/// 删除右键菜单
/// </summary>
Expand Down Expand Up @@ -161,6 +163,7 @@ public static string Get_SystemColor()
}
return ((int)Registry.GetValue(@"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\DWM", "ColorizationColor", 1052688)).ToString("x8").Substring(2);
}

/// <summary>
/// 获取系统主题模式(浅色/深色)
/// </summary>
Expand All @@ -178,6 +181,7 @@ public static bool Get_SystemUsesLightTheme()
}
return false;
}

/// <summary>
/// 获取开始菜单(磁贴)、任务栏、操作中心等是否使用主题色
/// </summary>
Expand All @@ -195,6 +199,7 @@ public static bool Get_SystemColorPrevalence()
}
return true;
}

/// <summary>
/// 获取系统是否开启透明效果
/// </summary>
Expand All @@ -219,6 +224,7 @@ public static string Get_DisplayVersion()
{
return (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "DisplayVersion", null);
}

/// <summary>
/// 获取系统发行ID(例:2004)
/// </summary>
Expand All @@ -227,6 +233,7 @@ public static string Get_ReleaseId()
{
return (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "ReleaseId", null);
}

/// <summary>
/// 获取系统当前版本号(例:19041)
/// </summary>
Expand All @@ -235,6 +242,7 @@ public static string Get_CurrentBuild()
{
return (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion", "CurrentBuild", null);
}

/// <summary>
/// 获取系统当前内部版本号(例:19041)
/// </summary>
Expand All @@ -256,7 +264,7 @@ public static bool Get_NewTileState()
{
return false;
}
Debug.Print(Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FeatureManagement\Overrides\0\2093230218", "EnabledState", null).ToString());
//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;
Expand Down

0 comments on commit 644a301

Please sign in to comment.