PowerToys/WinAlfred/ResultItem.xaml.cs

88 lines
2.6 KiB
C#
Raw Normal View History

2013-12-22 12:35:21 +01:00
using System;
2014-01-04 13:26:13 +01:00
using System.Drawing;
2014-01-03 16:52:36 +01:00
using System.IO;
2014-01-04 13:26:13 +01:00
using System.Windows;
2013-12-22 12:35:21 +01:00
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using WinAlfred.Plugin;
2014-01-04 13:26:13 +01:00
using Brush = System.Windows.Media.Brush;
2013-12-22 12:35:21 +01:00
namespace WinAlfred
{
public partial class ResultItem : UserControl
{
private bool selected;
public Result Result { get; private set; }
public bool Selected
{
get
{
return selected;
}
set
{
selected = value;
2013-12-22 17:10:46 +01:00
BrushConverter bc = new BrushConverter();
Background = selected ? (Brush)(bc.ConvertFrom("#d1d1d1")) : (Brush)(bc.ConvertFrom("#ebebeb"));
2014-01-07 16:27:05 +01:00
if (selected)
{
img.Visibility = Visibility.Visible;
img.Source = new BitmapImage(new Uri(Directory.GetCurrentDirectory()+"\\Images\\enter.png"));
}
else
{
img.Visibility = Visibility.Hidden;
}
2013-12-22 12:35:21 +01:00
}
}
2013-12-22 17:10:46 +01:00
public void SetIndex(int index)
{
tbIndex.Text = index.ToString();
}
2013-12-22 12:35:21 +01:00
public ResultItem(Result result)
{
InitializeComponent();
Result = result;
tbTitle.Text = result.Title;
tbSubTitle.Text = result.SubTitle;
2014-01-04 13:26:13 +01:00
string path = string.Empty;
if (!string.IsNullOrEmpty(result.IcoPath) && result.IcoPath.Contains(":\\") && File.Exists(result.IcoPath))
2013-12-22 12:35:21 +01:00
{
2014-01-04 13:26:13 +01:00
path = result.IcoPath;
2013-12-22 12:35:21 +01:00
}
2014-01-04 13:26:13 +01:00
else if (!string.IsNullOrEmpty(result.IcoPath) && File.Exists(result.PluginDirectory + result.IcoPath))
{
path = result.PluginDirectory + result.IcoPath;
}
if (!string.IsNullOrEmpty(path))
{
if (path.ToLower().EndsWith(".exe") || path.ToLower().EndsWith(".lnk"))
{
imgIco.Source = GetIcon(path);
}
else
{
imgIco.Source = new BitmapImage(new Uri(path));
}
}
}
public static ImageSource GetIcon(string fileName)
{
Icon icon = Icon.ExtractAssociatedIcon(fileName);
return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
new Int32Rect(0, 0, icon.Width, icon.Height),
BitmapSizeOptions.FromEmptyOptions());
2013-12-22 12:35:21 +01:00
}
}
}