/[RomCheater]/trunk/RomCheater/Docking/FloatingPIDSelector.cs
ViewVC logotype

Contents of /trunk/RomCheater/Docking/FloatingPIDSelector.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 381 - (show annotations) (download)
Tue Jun 19 08:42:19 2012 UTC (9 years, 7 months ago) by william
File size: 16826 byte(s)
+ add support to show a generic icon for applications that have no icon

1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Diagnostics;
9 using RomCheater.PluginFramework.Interfaces;
10 using RomCheater.PluginFramework.Core;
11 using System.IO;
12 using RomCheater.Logging;
13 using WeifenLuo.WinFormsUI.Docking;
14 using RomCheater.PluginFramework.Events;
15 using System.Management;
16 using System.Reflection;
17
18 namespace RomCheater.Docking
19 {
20 public partial class FloatingPIDSelector : DockContent, IAcceptsPlugin<IConfigPlugin>, IAcceptsChangedProcess, IAcceptsProcessPID
21 {
22 private const int ProcessRefershInterval = 5000; // refresh interval in milliseconds
23 private bool isUserInteracting = false;
24 private bool isRefreshing = false;
25 private bool isAutoRefreshing = false;
26 private bool isAttachingToProcess = false;
27 //private IConfigPlugin plugin = null;
28 //bool isInError = false;
29 public FloatingPIDSelector() { InitializeComponent(); this.ProcessPID = -1; OnSelectedProcessChanged = null; this.AcceptedPlugin = null; }
30 public FloatingPIDSelector(IConfigPlugin plugin) : this() { this.AcceptedPlugin = plugin; }
31 // unsued construtor (it's not implmented, may not be)
32 [Obsolete("constructor PIDSelector(int pid) is not implemented", false)]
33 public FloatingPIDSelector(int pid) : this() { this.ProcessPID = pid; }
34 [Obsolete("constructor PIDSelector(IConfigPlugin plugin, int pid) is not implemented", false)]
35 public FloatingPIDSelector(IConfigPlugin plugin, int pid) : this(plugin) { this.ProcessPID = pid; }
36
37 #region IAcceptsProcessPID
38 public int ProcessPID { get; set; }
39 #endregion
40 #region IAcceptsPlugin<IConfigPlugin> members
41 private IConfigPlugin _AcceptedPlugin;
42 public IConfigPlugin AcceptedPlugin {
43 get { return _AcceptedPlugin; }
44 set { _AcceptedPlugin = value; ProcessWatcherTimer.Enabled = (value != null); } }
45 #endregion
46 #region IAcceptsChangedProcess members
47 public event BaseEventHandler<ProcessChangedEventArgs> OnSelectedProcessChanged;
48 #endregion
49
50
51
52 public new void Show() { this.Show(null); }
53 //public new void Show(IWin32Window owner) { this.PreInitShow(owner); }
54 public new void Show(DockPanel panel) { this.PreInitShow(panel); }
55 //public new DialogResult ShowDialog() { return this.ShowDialog(null); }
56 //public new DialogResult ShowDialog(IWin32Window owner) { return this.PreInitDialog(owner); }
57 //private void PreInitShow(IWin32Window owner)
58 //{
59 // if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.",plugin.ToString())); }
60 // else { if (owner == null) { base.Show(); } else { base.Show(owner); } }
61 //}
62 private void PreInitShow(DockPanel panel)
63 {
64 //if (panel == null) return;
65 if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.", AcceptedPlugin.ToString())); }
66 else { if (panel == null) { base.Show(); } else { base.Show(panel); } }
67 }
68 //private DialogResult PreInitDialog(IWin32Window owner)
69 //{
70 // if (!this.RefreshList()) { MessageBox.Show(string.Format("Could not find any Processes for plugin {0}. Please start an instance of one.", plugin.ToString())); return DialogResult.Cancel; }
71 // else { if (owner == null) { return base.ShowDialog(); } else { return base.ShowDialog(owner); } }
72 //}
73 //private void SetControlBackColor(Control control, KnownColor color) { SetControlBackColor(control, Color.FromKnownColor(color)); }
74 //private void SetControlBackColor(Control control, Color color) { control.BackColor = color; }
75 private void SetControlBackColor<T>(T component, KnownColor color) { SetControlBackColor(component, Color.FromKnownColor(color)); }
76 private void SetControlBackColor<T>(T component, Color color)
77 {
78 Type ComponentType = component.GetType();
79 PropertyInfo pBackColor = ComponentType.GetProperty("BackColor");
80 pBackColor.SetValue(component, color, null);
81 }
82 private void SetViewForProcessListView(View view)
83 {
84 Color baseColor = Color.FromKnownColor(KnownColor.GradientActiveCaption);
85 int GB_OFFSET = 10;
86 Color c = Color.FromArgb(baseColor.A, baseColor.R, baseColor.G + GB_OFFSET, baseColor.B + GB_OFFSET);
87 ResetViewButtonControls();
88 switch (view)
89 {
90 case View.Details: SetControlBackColor(btnDetails, c); break;
91 case View.LargeIcon: SetControlBackColor(btnLargeIcon, c); break;
92 case View.List: SetControlBackColor(btnList, c); break;
93 case View.SmallIcon: SetControlBackColor(btnSmallIcon, c); break;
94 case View.Tile: SetControlBackColor(btnTile, c); break;
95 }
96 lstProcessList.View = view;
97 }
98 private void ResetViewButtonControls()
99 {
100 KnownColor DefaultColor = KnownColor.Control;
101 SetControlBackColor(btnLargeIcon, DefaultColor);
102 SetControlBackColor(btnDetails, DefaultColor);
103 SetControlBackColor(btnSmallIcon, DefaultColor);
104 SetControlBackColor(btnList, DefaultColor);
105 SetControlBackColor(btnTile, DefaultColor);
106 }
107 private void btnLargeIcon_Click(object sender, EventArgs e) { SetViewForProcessListView(View.LargeIcon); }
108 private void btnDetails_Click(object sender, EventArgs e) { SetViewForProcessListView(View.Details); }
109 private void btnSmallIcon_Click(object sender, EventArgs e) { SetViewForProcessListView(View.SmallIcon); }
110 private void btnList_Click(object sender, EventArgs e) { SetViewForProcessListView(View.List); }
111 private void btnTile_Click(object sender, EventArgs e) { SetViewForProcessListView(View.Tile); }
112
113 private void PerformListViewItemSelect(ListViewItem li)
114 {
115 if (isAutoRefreshing && !isAttachingToProcess) return;
116 this.ProcessPID = Convert.ToInt32(li.SubItems[1].Text);
117 Process SelectedProcess = Process.GetProcessById(this.ProcessPID);
118
119 txtstatus_Pid.Text = SelectedProcess.Id.ToString();
120 txtstatus_Filename.Text = SelectedProcess.MainModule.FileName;
121
122 logger.Debug.WriteLine("Using Process: ({0} : {1})", SelectedProcess.Id, SelectedProcess.ProcessName);
123 //this.Close();
124 if (OnSelectedProcessChanged != null)
125 OnSelectedProcessChanged(new ProcessChangedEventArgs(this, this.ProcessPID));
126 if (isAttachingToProcess)
127 isAttachingToProcess = false;
128 }
129 private void btnOK_Click(object sender, EventArgs e)
130 {
131
132 }
133
134 //private void btnCancel_Click(object sender, EventArgs e)
135 //{
136 // this.SelectedPid = -1;
137 // //this.Close();
138 //}
139
140 private void btnRefresh_Click(object sender, EventArgs e)
141 {
142 //this.RefreshList();
143 }
144
145 private bool RefreshList()
146 {
147 lstProcessList.BeginUpdate();
148 //if (this.AcceptedPlugin == null) { return false; };
149 isRefreshing = true;
150 lstProcessList.Items.Clear();
151 //logger.VerboseDebug.WriteLine(System.Environment.StackTrace);
152 this.AcceptedPlugin.Reload(true);
153 // create a selection of PCSX2 processes
154 List<ProcContainer> proc_list = AcceptedPlugin.ValidProcessesForPlugin;
155
156 if (!(proc_list.Count > 0))
157 { //MessageBox.Show("Could not find any PCSX2 Processes. Please start an instance of one.");
158 return false;
159 }
160
161 List<ListViewItem> items = new List<ListViewItem>();
162 ImageList small_image_list = new ImageList();
163 ImageList large_image_list = new ImageList();
164
165 int small_width = 24;
166 int large_width = 32;
167
168 small_image_list.ImageSize = new Size(small_width, small_width);
169 large_image_list.ImageSize = new Size(large_width, large_width);
170 foreach (ProcContainer p in proc_list)
171 {
172 Bitmap small_image = null;
173 Bitmap large_image = null;
174 string image_key = string.Empty;
175 this.AddProcessItem(p, small_image_list.ImageSize, large_image_list.ImageSize, out image_key, out small_image, out large_image);
176 if (image_key != string.Empty)
177 {
178 if (small_image != null) { small_image_list.Images.Add(image_key, small_image); }
179 if (large_image != null) { large_image_list.Images.Add(image_key, large_image); }
180 ListViewItem item = new ListViewItem();
181 item.Name = p.GetHashCode().ToString();
182 // process name
183 item.Text = p.Name;
184 // pid
185 item.SubItems.Add(p.ProcessInfo.Id.ToString());
186 // FullPath
187 item.SubItems.Add(p.FileName);
188 // image key
189 item.ImageKey = p.FileName;
190 if (!items.Contains(item)) items.Add(item);
191 }
192 else
193 {
194 //logger.Error.WriteLine("image_key is an empty string!");
195 //if (small_image != null) { logger.Error.WriteLine(" [however small_image is not null]"); }
196 //if (large_image != null) { logger.Error.WriteLine(" [however large_image is not null]\n"); }
197 ListViewItem item = new ListViewItem();
198 item.Name = p.GetHashCode().ToString();
199 // process name
200 item.Text = p.Name;
201 // pid
202 item.SubItems.Add(p.ProcessInfo.Id.ToString());
203 // FullPath
204 item.SubItems.Add(p.FileName);
205 // image key
206 item.ImageKey = p.FileName;
207 if (!items.Contains(item)) items.Add(item);
208 }
209 }
210 lstProcessList.SmallImageList = small_image_list;
211 lstProcessList.LargeImageList = large_image_list;
212 this.lstProcessList.Items.AddRange(items.ToArray());
213
214 if (lstProcessList.Items.Count > 1 && !isAttachingToProcess)
215 {
216 lstProcessList.Items[0].Selected = true;
217 }
218 isRefreshing = false;
219 lstProcessList.EndUpdate();
220 return true;
221 }
222
223 private void AddProcessItem(ProcContainer p, Size small, Size large, out string image_key, out Bitmap small_image, out Bitmap large_image)
224 {
225 image_key = "";
226 small_image = null;
227 large_image = null;
228 ////small_image_list = new ImageList();
229 ////large_image_list = new ImageList();
230 //ListViewItem item = new ListViewItem();
231 //// process
232 //item.Text = p.Name;
233 //if (p.ProcessIcon != null)
234 //{
235 // //small_image_list.Images.Add(
236 // //large_image_list.Images.Add(p.FileName, p.ProcessIcon);
237 // item.ImageKey = p.FileName;
238 //}
239 //// pid
240 //item.SubItems.Add(p.ProcessInfo.Id.ToString());
241 //// FullPath
242 //item.SubItems.Add(p.FileName);
243 //if (!items.Contains(item)) items.Add(item);
244 if (p != null)
245 {
246 if (p.ProcessIcon != null)
247 {
248 image_key = p.FileName;
249 small_image = new Bitmap(p.ProcessIcon, small);
250 large_image = new Bitmap(p.ProcessIcon, large);
251 }
252 else
253 {
254 Bitmap bmp = RomCheater.Properties.Resources.DefaulApplicationIcon.ToBitmap();
255 image_key = p.FileName;
256 small_image = new Bitmap(bmp, small);
257 large_image = new Bitmap(bmp, large);
258 }
259 }
260
261 }
262
263 private void PIDSelector_Load(object sender, EventArgs e)
264 {
265 if (AcceptedPlugin == null || this.DesignMode) return;
266 ProcessWatcherTimer.Interval = ProcessRefershInterval;
267 txtstatus_Pid.Text = "";
268 txtstatus_Filename.Text = "";
269 SetViewForProcessListView(View.LargeIcon);
270 this.RefreshList();
271 }
272
273
274 private void lstProcessList_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
275 {
276 if (!e.IsSelected) return;
277 PerformListViewItemSelect(e.Item);
278 }
279
280 private void lstProcessList_ItemActivate(object sender, EventArgs e)
281 {
282 ListViewItem Item = lstProcessList.SelectedItems[0];
283 PerformListViewItemSelect(Item);
284 }
285
286 private void btnLaunchAndAttach_Click(object sender, EventArgs e)
287 {
288 DialogResult result = EXESelector.ShowDialog();
289 if (result != DialogResult.OK) return;
290 FileInfo fi = new FileInfo(EXESelector.FileName);
291 if (!fi.Exists) { logger.Warn.WriteLine("Cannot load and attach to non-existing exe file: {0}", fi.FullName); return; }
292 Process p = Process.Start(fi.FullName);
293 this.ProcessPID = p.Id;
294 isAttachingToProcess = true;
295 }
296
297 private void ProcessWatcherTimer_Tick(object sender, EventArgs e)
298 {
299 if (isRefreshing || isAutoRefreshing || isUserInteracting) return;
300 //if (lstProcessList.Items.Count == 0) return;
301 isAutoRefreshing = true;
302 int pid = this.ProcessPID;
303 this.RefreshList();
304 bool pid_still_exists = false;
305 int item_index = -1;
306 foreach (ListViewItem li2 in lstProcessList.Items)
307 {
308 int _pid = Convert.ToInt32(li2.SubItems[1].Text);
309 if (_pid == pid)
310 {
311 item_index = lstProcessList.Items.IndexOf(li2);
312 pid_still_exists = true;
313 break;
314 }
315 }
316 if (pid_still_exists)
317 {
318 //logger.Debug.WriteLine("Pid: {0} still exists", pid);
319 if (lstProcessList.SelectedItems.Count > 0)
320 {
321 if (lstProcessList.SelectedItems[0] != null)
322 if (lstProcessList.SelectedItems[0].Index != -1)
323 lstProcessList.Items[lstProcessList.SelectedItems[0].Index].Selected = false;
324 }
325 lstProcessList.Items[item_index].Selected = true;
326 isAutoRefreshing = false;
327 }
328 else
329 {
330 if (lstProcessList.SelectedItems.Count > 0)
331 {
332 if (lstProcessList.SelectedItems[0] != null)
333 {
334 int missing_pid = this.ProcessPID;
335 logger.Warn.WriteLine("Connection to selected process ({0}) has been lost...Selecting the first available process.", missing_pid);
336 MessageBox.Show(string.Format("Connection to selected process ({0}) has been lost...Selecting the first available process.", missing_pid), "Connection to selected process lost", MessageBoxButtons.OK, MessageBoxIcon.Warning);
337 }
338 isAutoRefreshing = false;
339 lstProcessList.Items[0].Selected = false;
340 lstProcessList.Items[0].Selected = true;
341 }
342 }
343 }
344
345
346 private void lstProcessList_MouseMove(object sender, MouseEventArgs e)
347 {
348 isUserInteracting = true;
349 }
350
351 private void lstProcessList_MouseLeave(object sender, EventArgs e)
352 {
353 isUserInteracting = false;
354 }
355 }
356 }
357

  ViewVC Help
Powered by ViewVC 1.1.22