跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

IconSelect.cs

193 行 · 5.5 KB历史下载
1using Core.Librarys;
2using System;
3using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using System.Windows;
8using System.Windows.Controls;
9using System.Windows.Input;
10
11namespace UI.Controls.Base
12{
13 public class IconSelect : Control
14 {
15 public List<string> Icons
16 {
17 get { return (List<string>)GetValue(DaysProperty); }
18 set { SetValue(DaysProperty, value); }
19 }
20 public static readonly DependencyProperty DaysProperty =
21 DependencyProperty.Register("Icons",
22 typeof(List<string>),
23 typeof(IconSelect)
24 );
25
26 /// <summary>
27 /// 图片链接
28 /// </summary>
29 public string URL
30 {
31 get { return (string)GetValue(URLProperty); }
32 set { SetValue(URLProperty, value); }
33 }
34 public static readonly DependencyProperty URLProperty =
35 DependencyProperty.Register("URL",
36 typeof(string),
37 typeof(IconSelect), new PropertyMetadata(new PropertyChangedCallback(OnURLPropertyChanged)));
38
39 private static void OnURLPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
40 {
41 var control = d as IconSelect;
42 if (string.IsNullOrEmpty(control.URL))
43 {
44 control.Reset();
45 }
46 }
47
48 public bool IsOpen
49 {
50 get { return (bool)GetValue(IsOpenProperty); }
51 set { SetValue(IsOpenProperty, value); }
52 }
53 public static readonly DependencyProperty IsOpenProperty =
54 DependencyProperty.Register("IsOpen",
55 typeof(bool),
56 typeof(IconSelect), new PropertyMetadata(new PropertyChangedCallback(OnPropertyChanged)));
57
58 private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
59 {
60 var control = d as IconSelect;
61 if (e.Property == IsOpenProperty)
62 {
63 control.HandleHook();
64 }
65 }
66 private Win32API.LowLevelKeyboardProc mouseProc;
67 private static IntPtr hookMouseID = IntPtr.Zero;
68 private IntPtr hook;
69 private bool IsFirstClick = false;
70 private Border SelectContainer;
71
72
73 #region hook
74 private void HandleHook()
75 {
76 if (IsOpen)
77 {
78 // 设置键盘钩子
79 hook = Win32API.SetMouseHook(mouseProc);
80 }
81 else
82 {
83 Win32API.UnhookWindowsHookEx(hook);
84 }
85 }
86 private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
87 {
88
89 if (nCode >= 0)
90 {
91 if (wParam == (IntPtr)Win32API.WM_LBUTTONDBLCLK || wParam == (IntPtr)Win32API.WM_WHEEL)
92 {
93 if (!IsFirstClick)
94 {
95 bool isInControl = IsInControl();
96
97 if (!isInControl)
98 {
99 IsOpen = false;
100 }
101 }
102 IsFirstClick = false;
103 }
104
105 }
106
107 return Win32API.CallNextHookEx(hookMouseID, nCode, wParam, lParam);
108 }
109 private bool IsInControl()
110 {
111 var p = Mouse.GetPosition(SelectContainer);
112 if (p.X < 0 || p.Y < 0 || p.X > SelectContainer.ActualWidth || p.Y > SelectContainer.ActualHeight)
113 {
114 return false;
115 }
116
117
118 return true;
119 }
120 #endregion
121
122 public Command ShowSelectCommand { get; set; }
123 public Command FileSelectCommand { get; set; }
124
125 public IconSelect()
126 {
127 DefaultStyleKey = typeof(IconSelect);
128 URL = "pack://application:,,,/Tai;component/Resources/Emoji/(1).png";
129 ShowSelectCommand = new Command(new Action<object>(OnShowSelect));
130 FileSelectCommand = new Command(new Action<object>(OnFileSelect));
131 mouseProc = HookCallback;
132
133 Unloaded += IconSelect_Unloaded;
134 LoadIcons();
135 }
136
137 private void LoadIcons()
138 {
139 var list = new List<string>();
140 for (int i = 1; i < 45; i++)
141 {
142 list.Add($"pack://application:,,,/Tai;component/Resources/Emoji/({i}).png");
143 }
144 Icons = list;
145 }
146
147 private void Reset()
148 {
149 URL = Icons[0];
150 }
151 private void OnShowSelect(object obj)
152 {
153 IsOpen = true;
154 IsFirstClick = true;
155 }
156
157 private void OnFileSelect(object obj)
158 {
159 Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
160
161
162 dlg.Filter = "PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg";
163
164
165 bool? result = dlg.ShowDialog();
166
167
168 if (result == true)
169 {
170 URL = dlg.FileName;
171 }
172 }
173
174 public override void OnApplyTemplate()
175 {
176 base.OnApplyTemplate();
177
178 SelectContainer = GetTemplateChild("SelectContainer") as Border;
179 }
180
181 private void IconSelect_Unloaded(object sender, RoutedEventArgs e)
182 {
183 Win32API.UnhookWindowsHookEx(hook);
184 }
185
186 protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
187 {
188 base.OnMouseLeftButtonDown(e);
189 }
190
191
192 }
193}