添加图片显示功能

This commit is contained in:
尚美 2025-03-12 15:44:35 +08:00
parent b0ad38b447
commit beb88e236d

View File

@ -535,7 +535,7 @@ namespace QuickLauncher
var label = new Label() { Text = "新名称:", Location = new Point(10, 20) };
var textBox = new TextBox() { Text = currentCategory, Location = new Point(120, 17), Width = 180 };
var button = new Button() { Text = "确定", DialogResult = DialogResult.OK, Location = new Point(110, 60) };
var button = new RoundedButton() { Text = "确定", DialogResult = DialogResult.OK, Location = new Point(110, 60), CornerRadius = cornerRadius, BackColor = accentColor, ForeColor = Color.White };
dialog.Controls.AddRange(new Control[] { label, textBox, button });
dialog.AcceptButton = button;
@ -619,7 +619,7 @@ namespace QuickLauncher
var label = new Label() { Text = "新名称:", Location = new Point(10, 20) };
var textBox = new TextBox() { Text = item.Name, Location = new Point(120, 17), Width = 180 };
var button = new Button() { Text = "确定", DialogResult = DialogResult.OK, Location = new Point(110, 60) };
var button = new RoundedButton() { Text = "确定", DialogResult = DialogResult.OK, Location = new Point(110, 60), CornerRadius = cornerRadius, BackColor = accentColor, ForeColor = Color.White };
dialog.Controls.AddRange(new Control[] { label, textBox, button });
dialog.AcceptButton = button;
@ -666,7 +666,7 @@ namespace QuickLauncher
var usageLabel = new Label() { Text = "使用次数:", Location = new Point(20, 170), AutoSize = true };
var usageValue = new Label() { Text = item.UsageCount.ToString(), Location = new Point(120, 170), AutoSize = true };
var button = new Button() { Text = "确定", DialogResult = DialogResult.OK, Location = new Point(150, 220) };
var button = new RoundedButton() { Text = "确定", DialogResult = DialogResult.OK, Location = new Point(150, 220), CornerRadius = cornerRadius, BackColor = accentColor, ForeColor = Color.White };
dialog.Controls.AddRange(new Control[] { icon, nameLabel, nameValue, pathLabel, pathValue,
dateLabel, dateValue, usageLabel, usageValue, button });
@ -949,8 +949,8 @@ namespace QuickLauncher
Location = new Point(110, 60),
Width = 100,
Height = 30,
BackColor = Color.FromArgb(210, 210, 210, 200), // 自定义RGB颜色
ForeColor = Color.FromArgb(40, 40, 40),
BackColor = accentColor,
ForeColor = Color.White,
CornerRadius = cornerRadius
};
@ -1129,10 +1129,10 @@ namespace QuickLauncher
{
Size = new Size(iconSize, iconSize),
Location = new Point((panel.Width - iconSize) / 2, 5),
Image = item.Icon ?? SystemIcons.Application.ToBitmap(),
SizeMode = PictureBoxSizeMode.StretchImage,
Image = GetItemIcon(item),
SizeMode = PictureBoxSizeMode.Zoom,
Tag = item,
Cursor = Cursors.Hand // 鼠标悬停时显示手型光标
Cursor = Cursors.Hand
};
var label = new Label
@ -1144,7 +1144,7 @@ namespace QuickLauncher
Height = 30,
Location = new Point(0, iconSize + 10),
Tag = item,
Cursor = Cursors.Hand, // 鼠标悬停时显示手型光标
Cursor = Cursors.Hand,
ForeColor = isDarkMode ? Color.FromArgb(240, 240, 240) : Color.FromArgb(20, 20, 20)
};
@ -1165,15 +1165,22 @@ namespace QuickLauncher
}
}
// 移除旧的事件处理器并重新绑定
panel.DoubleClick -= null;
icon.DoubleClick -= null;
label.DoubleClick -= null;
// 双击事件 - 启动应用程序或预览图片
EventHandler doubleClickHandler = (s, e) =>
{
if (IsImageFile(item.Path))
{
ShowImagePreview(item.Path);
}
else
{
LaunchApplication(item.Path);
}
};
// 双击事件 - 启动应用程序
panel.DoubleClick += (s, e) => LaunchApplication(item.Path);
icon.DoubleClick += (s, e) => LaunchApplication(item.Path);
label.DoubleClick += (s, e) => LaunchApplication(item.Path);
panel.DoubleClick += doubleClickHandler;
icon.DoubleClick += doubleClickHandler;
label.DoubleClick += doubleClickHandler;
// 添加鼠标悬停效果
panel.MouseEnter += Panel_MouseEnter;
@ -1191,8 +1198,8 @@ namespace QuickLauncher
label.MouseDown += Panel_MouseDown;
label.MouseUp += Panel_MouseUp;
// 直接设置工具提示
string tipText = $"名称: {item.Name}\n路径: {item.Path}\n双击启动";
// 设置工具提示
string tipText = GetTooltipText(item);
toolTip1.SetToolTip(panel, tipText);
toolTip1.SetToolTip(icon, tipText);
toolTip1.SetToolTip(label, tipText);
@ -1200,6 +1207,95 @@ namespace QuickLauncher
shortcutsPanel.Controls.Add(panel);
}
private Image GetItemIcon(ShortcutItem item)
{
if (IsImageFile(item.Path))
{
try
{
// 为图片文件创建缩略图
using (var img = Image.FromFile(item.Path))
{
return new Bitmap(img, new Size(iconSize, iconSize));
}
}
catch
{
return item.Icon ?? SystemIcons.Application.ToBitmap();
}
}
return item.Icon ?? SystemIcons.Application.ToBitmap();
}
private bool IsImageFile(string path)
{
string ext = Path.GetExtension(path).ToLower();
return new[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".tiff", ".ico" }.Contains(ext);
}
private string GetTooltipText(ShortcutItem item)
{
string type = IsImageFile(item.Path) ? "图片文件" : "应用程序";
return $"名称: {item.Name}\n类型: {type}\n路径: {item.Path}\n" +
$"添加时间: {item.DateAdded:yyyy-MM-dd}\n使用次数: {item.UsageCount}";
}
private void ShowImagePreview(string imagePath)
{
try
{
using (var previewForm = new Form())
{
previewForm.Text = Path.GetFileName(imagePath);
previewForm.StartPosition = FormStartPosition.CenterScreen;
previewForm.BackColor = isDarkMode ? Color.FromArgb(32, 32, 32) : Color.FromArgb(243, 243, 243);
previewForm.WindowState = FormWindowState.Normal;
var pictureBox = new PictureBox
{
Dock = DockStyle.Fill,
SizeMode = PictureBoxSizeMode.Zoom,
Image = Image.FromFile(imagePath)
};
// 设置窗体大小为屏幕大小的80%
var screen = Screen.FromControl(this);
previewForm.Size = new Size(
(int)(screen.WorkingArea.Width * 0.8),
(int)(screen.WorkingArea.Height * 0.8)
);
// 添加ESC键关闭功能
previewForm.KeyPreview = true;
previewForm.KeyDown += (s, e) =>
{
if (e.KeyCode == Keys.Escape)
previewForm.Close();
};
// 添加鼠标滚轮缩放
pictureBox.MouseWheel += (s, e) =>
{
if (ModifierKeys == Keys.Control)
{
float zoom = e.Delta > 0 ? 1.1f : 0.9f;
pictureBox.Size = new Size(
(int)(pictureBox.Size.Width * zoom),
(int)(pictureBox.Size.Height * zoom)
);
}
};
previewForm.Controls.Add(pictureBox);
previewForm.ShowDialog();
}
}
catch (Exception ex)
{
MessageBox.Show($"无法预览图片: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
// 鼠标进入面板时的效果
private void Panel_MouseEnter(object sender, EventArgs e)
{