/[DXTViewerLoader]/trunk/DXTViewerLoader/DXTViewer.cs
ViewVC logotype

Contents of /trunk/DXTViewerLoader/DXTViewer.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (show annotations) (download)
Thu Dec 12 12:25:39 2013 UTC (6 years, 5 months ago) by william
File size: 35770 byte(s)

1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Diagnostics;
6 using System.Windows.Forms;
7 using System.IO;
8 using System.Runtime.InteropServices;
9 using System.Collections;
10 using System.Drawing;
11 using System.Threading;
12 using System.Xml.Linq;
13 using Enterprise.Logging;
14
15 namespace DXTViewerLoader
16 {
17 internal static class DXTViewer
18 {
19 #region Win32Api
20 [StructLayout(LayoutKind.Sequential)]
21 struct RECT
22 {
23 public int left, top, right, bottom;
24 public RECT(Point location, Size size) : this(new Rectangle(location, size)) { }
25 public RECT(int x, int y, int width, int height) : this(new Rectangle(x, y, width, height)) { }
26 public RECT(Rectangle r)
27 {
28 left = r.Left;
29 top = r.Top;
30 right = r.Right;
31 bottom = r.Bottom;
32 }
33 }
34 [StructLayout(LayoutKind.Sequential)]
35 public struct POINT
36 {
37 public int X;
38 public int Y;
39
40 public POINT(int x, int y)
41 {
42 this.X = x;
43 this.Y = y;
44 }
45
46 public static implicit operator System.Drawing.Point(POINT p)
47 {
48 return new System.Drawing.Point(p.X, p.Y);
49 }
50
51 public static implicit operator POINT(System.Drawing.Point p)
52 {
53 return new POINT(p.X, p.Y);
54 }
55 }
56
57 private static IntPtr MakeLParam(int LoWord, int HiWord)
58 {
59 return (IntPtr)((HiWord << 16) | (LoWord & 0xffff));
60 }
61 const uint WM_KEYDOWN = 0x100;
62 const uint WM_KEYUP = 0x101;
63 const uint WM_CHAR = 0x102;
64 const uint WM_PAINT = 0xF;
65
66 const int VK_MENU = 0x12;
67
68 static bool RefreshWindow(IntPtr hWnd) { return RefreshWindow(hWnd, IntPtr.Zero, false); }
69 static bool RefreshWindow(IntPtr hWnd, IntPtr lpRect, bool bErase)
70 {
71 if (!InvalidateRect(hWnd, lpRect, bErase))
72 {
73 gLog.Error.WriteLine("An Error ocurred while calling: InvalidateRect on handle: 0x{0:x8}", hWnd.ToInt32());
74 return false;
75 }
76 if (!UpdateWindow(hWnd))
77 {
78 gLog.Error.WriteLine("An Error ocurred while calling: UpdateWindow on handle: 0x{0:x8}", hWnd.ToInt32());
79 return false;
80 }
81 return true;
82 }
83
84 ////public static void sendKey(IntPtr hwnd, char key, bool extended)
85 ////{
86 //// PostMessage(hwnd, WM_CHAR, (IntPtr)key, (IntPtr)0);
87 ////}
88 //public static void sendKey(IntPtr hwnd, char key, bool extended)
89 //{
90 // //uint scanCode = MapVirtualKey((uint)key, 0);
91 // uint lParam;
92 // //KEY DOWN
93 // lParam = (0x00000001);// | (scanCode << 16));
94 // if (extended)
95 // {
96 // lParam = lParam | 0x01000000;
97 // }
98 // PostMessage(hwnd, WM_KEYDOWN, (IntPtr)key, (IntPtr)lParam);
99 // //PostMessage(hwnd, (UInt32)WM_KEYUP, (IntPtr)key, (IntPtr)lParam);
100 //}
101 [DllImport("user32.dll")]
102 static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
103 [DllImport("user32.dll")]
104 static extern bool UpdateWindow(IntPtr hWnd);
105
106 [DllImport("user32.dll")]
107 static extern byte VkKeyScan(char ch);
108
109 [DllImport("user32.dll")]
110 static extern uint MapVirtualKey(uint uCode, uint uMapType);
111
112 [return: MarshalAs(UnmanagedType.Bool)]
113 [DllImport("user32.dll", SetLastError = true)]
114 static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
115
116 [DllImport("user32.dll", EntryPoint = "SendMessage")]
117 private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);
118 [DllImport("user32.dll", CharSet = CharSet.Auto)]
119 static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, StringBuilder lParam);
120 //If you use '[Out] StringBuilder', initialize the string builder with proper length first.
121
122 [DllImport("user32.dll", EntryPoint = "SendMessage")]
123 static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, ref RECT lParam);
124 [DllImport("user32.dll", EntryPoint = "SendMessage")]
125 static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, ref POINT lParam);
126 [DllImport("user32.dll", EntryPoint = "SendMessage")]
127 static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, Int32 wParam, Int32 lParam);
128 /*
129 // Select All
130 SendMessage(listBox.Handle, 0x185, (IntPtr)1, (IntPtr)(-1));
131 // Unselect All
132 SendMessage(listBox.Handle, 0x185, (IntPtr)0, (IntPtr)(-1));
133 */
134
135
136 [DllImport("user32.dll")]
137 public static extern bool SetForegroundWindow(IntPtr hWnd);
138
139 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
140 private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
141 [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
142 private static extern int GetWindowTextLength(IntPtr hWnd);
143
144 [DllImport("user32.dll", SetLastError = true)]
145 static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
146 private delegate bool EnumedWindow(IntPtr handleWindow, ArrayList handles);
147 [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
148 [return: MarshalAs(UnmanagedType.Bool)]
149 private static extern bool EnumWindows(EnumedWindow lpEnumFunc, ArrayList lParam);
150
151 [DllImport("user32")]
152 [return: MarshalAs(UnmanagedType.Bool)]
153 private static extern bool EnumChildWindows(IntPtr window, EnumedWindow callback, ArrayList lParam);
154 private static bool GetWindowHandle(IntPtr windowHandle, ArrayList windowHandles)
155 {
156 windowHandles.Add(windowHandle);
157 return true;
158 }
159 #endregion
160 internal interface IInitializationParameters
161 {
162 string TargetImage { get; set; }
163 string ToString();
164 }
165 internal class InitializationParameters : IInitializationParameters
166 {
167 public InitializationParameters() : this(string.Empty) { }
168 public InitializationParameters(string image) { this.TargetImage = image; }
169 [Obsolete("This tool will only load one image ... any extras will be ignored",false)]
170 public InitializationParameters(List<string> images) : this(images == null || images.Count == 0 ? string.Empty : images.First()) { }
171 #region IInitializationParameters
172 public String TargetImage { get; set; }
173 #endregion
174 public override string ToString()
175 {
176 StringBuilder builder = new StringBuilder();
177 if (TargetImage == string.Empty)
178 {
179 builder.AppendLine("\tTargetImage: none");
180 }
181 else
182 {
183 builder.AppendLine(string.Format("TargetImage:{0}", TargetImage));
184 }
185 return builder.ToString();
186 }
187 }
188 internal static class Consts
189 {
190 #if DEV_DEBUG
191 internal const string dxtviewer_exe = @"C:\Program Files (x86)\oZone3D\DXTViewer\DXTViewer.exe";
192 #else
193 internal static string dxtviewer_exe="";
194 #endif
195 #if DEV_DEBUG
196 internal const string dxtviewer_window_title = "DXTViewer - Compressed Texture Viewer - www.oZone3D.Net";
197 #else
198 internal static string dxtviewer_window_title = "";
199 #endif
200 #if DEV_DEBUG
201 internal const string dxtviewer_openfiledialog_title = "Load an RGB Byte Image";
202 #else
203 internal static string dxtviewer_openfiledialog_title = "";
204 #endif
205 //internal const string ProcessName = "DXTViewer";
206 internal static string ProcessExecutable
207 {
208 get { return string.Format("{0}", dxtviewer_exe); }
209 }
210 internal const string ConfigName = "DXTViewer.xml";
211 }
212
213 #region Internal Helpers
214 private static string APP_PATH
215 {
216 get { return Application.StartupPath; }
217 }
218 private static string GetConfigparameter(string name)
219 {
220 try
221 {
222 FileInfo fi = GetConfig();
223 XDocument doc = XDocument.Load(fi.FullName);
224 if (doc.Descendants(name).Count() == 0)
225 {
226 gLog.Debug.WriteLine("Could not find config option: {0}", name);
227 throw new Exceptions.ConfigParameterNotFoundException(true, name);
228 }
229 var config_option = doc.Descendants(name).First();
230 if (config_option.Attributes().Count() == 0)
231 {
232
233 gLog.Debug.WriteLine("Found config option: {0} but it does not have any attributes", name);
234 throw new Exceptions.ConfigParameterNotFoundException(true, name);
235 }
236 else
237 {
238 if (config_option.Attributes("value").Count() == 0)
239 {
240 gLog.Debug.WriteLine("Found config option: {0} but it does not have a 'value' attribute", name);
241 throw new Exceptions.ConfigParameterNotFoundException(true, name);
242 }
243 else
244 {
245 var config_value = config_option.Attributes("value").First();
246
247 if (config_value.Value == string.Empty)
248 {
249 gLog.Debug.WriteLine("Found config option: {0} but it was an empty string", name);
250 throw new Exceptions.ConfigParameterNotFoundException(true, name);
251 }
252 else
253 {
254 return config_value.Value;
255 }
256 }
257 }
258 }
259 catch (Exception ex)
260 {
261 throw ex;
262 }
263 }
264 #endregion
265
266 private static IInitializationParameters InitParams
267 {
268 get;
269 set;
270 }
271 internal static void Initialize() { Initialize(new InitializationParameters()); }
272 internal static void Initialize(IInitializationParameters parameters)
273 {
274 InitParams = parameters;
275 gLog.Debug.WriteLine("InitParams:");
276 gLog.Debug.WriteLine(InitParams.ToString());
277 if (InitParams.TargetImage == string.Empty)
278 {
279 MessageBox.Show("No DDS Image File Given." + System.Environment.NewLine+ "Please try again.", "No DDS Image File Given", MessageBoxButtons.OK, MessageBoxIcon.Error);
280 return;
281 }
282 //MessageBox.Show(InitParams.TargetImage);
283 StartProcess();
284 }
285 #region GetProcess()
286 //private static Process GetProcess(bool DisplayError)
287 //{
288 // var procs = Process.GetProcessesByName(Consts.ProcessName);
289 // //var proc = procs.FirstOrDefault();
290 // var proc = procs[0];
291 // if (proc == null) { throw new Exceptions.ProcessInstanceNotFoundException(DisplayError); }
292 // return proc;
293 //}
294 //internal static Process GetProcess()
295 //{
296 // return GetProcess(true);
297 //}
298 #endregion
299
300 #region GetConfig()
301 internal static FileInfo GetConfig()
302 {
303 FileInfo fi = new FileInfo(string.Format(@"{0}\{1}", APP_PATH, Consts.ConfigName));
304 if (!fi.Exists) { throw new Exceptions.ConfigNotFoundException(true); }
305 else { return fi; }
306 }
307 #endregion
308
309 #region GetModule()
310 private static FileInfo GetModule(string path)
311 {
312 FileInfo fi = null;
313 if (path == string.Empty)
314 {
315 try
316 {
317 string t_path = GetConfigparameter("dxtviewer_exe");
318 if (string.IsNullOrEmpty(t_path))
319 {
320 string path_missing_message = string.Format("Config: '{0}' is either missing the 'path' parameter or it has not been configured.", Consts.ConfigName);
321 gLog.Warn.WriteLine(path_missing_message);
322 Exception path_missing = new Exception(path_missing_message);
323 throw path_missing;
324 }
325 #if !DEV_DEBUG
326 Consts.dxtviewer_exe = t_path;
327 #endif
328 gLog.Debug.WriteLine("Consts.ProcessExecutable: {0}", Consts.ProcessExecutable);
329 fi = new FileInfo(string.Format(@"{0}", Consts.ProcessExecutable));
330 gLog.Info.WriteLine("Using '{0}' from: {1}", Consts.ProcessExecutable, fi.FullName);
331 }
332 catch (Exception ex)
333 {
334 throw new Exceptions.ModuleNotFoundException(true, ex);
335 }
336 }
337 else
338 {
339 #if !DEV_DEBUG
340 string t_path = GetConfigparameter("dxtviewer_exe");
341 Consts.dxtviewer_exe = t_path;
342 #endif
343 gLog.Debug.WriteLine("Consts.ProcessExecutable: {0}", Consts.ProcessExecutable);
344 fi = new FileInfo(string.Format(@"{0}", Consts.ProcessExecutable));
345 gLog.Info.WriteLine("Using '{0}' from: {1}", Consts.ProcessExecutable, fi.FullName);
346 }
347 return fi;
348 }
349 private static FileInfo GetModule()
350 {
351 #if DEV_DEBUG
352 return GetModule(Consts.dxtviewer_exe);
353 #else
354 return GetModule(string.Empty);
355 #endif
356 }
357
358 //private static bool IsRunning(out Process proc)
359 //{
360 // proc = null;
361 // try
362 // {
363 // proc = GetProcess(false);
364 // return true;
365 // }
366 // catch (Exception) { return false; }
367 //}
368 //private static bool IsRunning()
369 //{
370 // Process p = null;
371 // return IsRunning(out p);
372 //}
373 private static Process _instance;
374 internal static Process Instance
375 {
376 get
377 {
378 return _instance;
379 }
380 private set { _instance = value; }
381 }
382
383 private static void StartProcess()
384 {
385 // determine if process is running
386 try
387 {
388 //if (InitParams.TargetPlugins.Count == 0)
389 //{
390 // gLog.Warn.WriteLine("No plugins have been selected for editing...exiting");
391 // return;
392 //}
393 Process p = null;
394 #if !ALWAYS_START_NEW_PROCESS_INSTANCE
395 if (IsRunning(out p))
396 {
397 gLog.Debug.WriteLine("{0} is already running: id=0x{1:x8} windowhandle=0x{2:x8}", p.ProcessName, p.Id, p.MainWindowHandle.ToInt32());
398 Instance = p;
399 return;
400 }
401 else
402 {
403 #endif
404 FileInfo module = GetModule();
405 #if ALWAYS_START_NEW_PROCESS_INSTANCE
406 gLog.Debug.Write("staring new instance of {0}...", module.FullName);
407 #else
408 gLog.Debug.Write("{0} not running staring new instance...", module.FullName);
409 #endif
410 p = new Process();
411 p.StartInfo.FileName = module.FullName;
412 p.StartInfo.Arguments = string.Empty;
413 p.StartInfo.UseShellExecute = false;
414 p.StartInfo.WorkingDirectory = module.Directory.FullName;
415 p.Start();
416 while (p.MainWindowHandle == IntPtr.Zero)
417 {
418 Application.DoEvents();
419 }
420 gLog.Debug.WriteLine("id=0x{0:x8} windowhandle=0x{1:x8} ", p.Id, p.MainWindowHandle.ToInt32());
421 Instance = p;
422 WaitForWindowInitialization();
423 return;
424 #if !ALWAYS_START_NEW_PROCESS_INSTANCE
425 }
426 #endif
427 }
428 catch (Exception ex)
429 {
430 throw new Exceptions.UnableToStartProcessException(true, ex);
431 }
432 }
433
434 #endregion
435
436
437 #region DoWork()
438 private static void WaitForWindowInitialization()
439 {
440 var windowHandles = new ArrayList();
441 EnumedWindow callBackPtr = GetWindowHandle;
442 //string lookup_title = Consts.PluginSelectionWindowTile;
443 #if !DEV_DEBUG
444 var t = GetConfigparameter("dxtviewer_window_title");
445 Consts.dxtviewer_window_title = t;
446 #endif
447 gLog.Debug.WriteLine("Consts.dxtviewer_window_title: {0}", Consts.dxtviewer_window_title);
448 string found_title = "";
449 while (found_title != Consts.dxtviewer_window_title)
450 {
451 if (!EnumWindows(callBackPtr, windowHandles))
452 {
453 gLog.Error.WriteLine("Failed to enumerate windows...");
454 return;
455 }
456 else
457 {
458 foreach (IntPtr windowhandle in windowHandles)
459 {
460 try
461 {
462 uint pid = 0;
463 GetWindowThreadProcessId(windowhandle, out pid);
464 int length = GetWindowTextLength(windowhandle);
465 StringBuilder windowTitle = new StringBuilder(length + 1);
466 GetWindowText(windowhandle, windowTitle, windowTitle.Capacity);
467 if (windowTitle.ToString() == Consts.dxtviewer_window_title && pid == Instance.Id)
468 {
469 found_title = windowTitle.ToString();
470 break;
471 }
472 }
473 catch (Exception ex)
474 {
475 gLog.Error.WriteLine(ex.ToString());
476 }
477
478
479 }
480 }
481 }
482 }
483 internal static void DoWork()
484 {
485 if (Instance == null)// && InitParams.TargetPlugins.Count > 0)
486 {
487 NullReferenceException ex = new NullReferenceException("DXTViewer Instance is null, did you call Initialize()?");
488 gLog.Error.WriteLine(ex.ToString());
489 throw ex;
490 }
491 if (InitParams.TargetImage == string.Empty) { return; }
492 #if !DEV_DEBUG
493 var t = GetConfigparameter("dxtviewer_window_title");
494 Consts.dxtviewer_window_title = t;
495 #endif
496 gLog.Debug.WriteLine("Consts.dxtviewer_window_title: {0}", Consts.dxtviewer_window_title);
497 List<IntPtr> WindowHandles = new List<IntPtr>();
498 var windowHandles = new ArrayList();
499 EnumedWindow callBackPtr = GetWindowHandle;
500
501 if (!EnumWindows(callBackPtr, windowHandles))
502 {
503 gLog.Error.WriteLine("Failed to enumerate windows...");
504 return;
505 }
506 else
507 {
508 foreach (IntPtr windowhandle in windowHandles)
509 {
510 try
511 {
512 uint pid = 0;
513 GetWindowThreadProcessId(windowhandle, out pid);
514
515 int length = GetWindowTextLength(windowhandle);
516 StringBuilder windowTitle = new StringBuilder(length + 1);
517 GetWindowText(windowhandle, windowTitle, windowTitle.Capacity);
518 GetWindowThreadProcessId(windowhandle, out pid);
519 if (windowTitle.ToString() == Consts.dxtviewer_window_title && pid == Instance.Id)
520 {
521 WindowHandles.Add(windowhandle);
522 gLog.Debug.WriteLine("Process: {0} (id=0x{1:x8} windowhandle=0x{2:x8}) title='{3}'", Consts.ProcessExecutable, pid, windowhandle.ToInt32(), windowTitle);
523 //break;
524 }
525 }
526 catch (Exception ex)
527 {
528 gLog.Error.WriteLine(ex.ToString());
529 }
530 }
531 }
532 if (WindowHandles.Count == 0)
533 {
534 gLog.Error.WriteLine("Failed to get Window Handle for Process: {0} (id=0x{1:x8})", Consts.ProcessExecutable);
535 return;
536 }
537 else
538 {
539 // DXTViewer only needs the mainwindowhandle
540 IntPtr handle = IntPtr.Zero;
541 if (WindowHandles.Count > 1)
542 {
543 gLog.Warn.WriteLine("Found more than one window handle: count={0}", WindowHandles.Count);
544 throw new InvalidOperationException(string.Format("Found more than one window handle: count={0}", WindowHandles.Count));
545 }
546 else if (WindowHandles.Count == 0)
547 {
548 gLog.Warn.WriteLine("Found no window handles: count={0}", WindowHandles.Count);
549 throw new InvalidOperationException(string.Format("Found no window handles: count={0}", WindowHandles.Count));
550 }
551 else
552 {
553 handle = WindowHandles.First();
554 }
555 if (handle == IntPtr.Zero)
556 {
557 gLog.Warn.WriteLine("Found Window Handle but it was IntPtr.Zero");
558 throw new InvalidOperationException("Found Window Handle but it was IntPtr.Zero");
559 }
560 int length = GetWindowTextLength(handle);
561 StringBuilder windowTitle = new StringBuilder(length + 1);
562 GetWindowText(handle, windowTitle, windowTitle.Capacity);
563 gLog.Debug.WriteLine("Windowhandle: 0x{0:x8} ... Title: '{1}'", handle, windowTitle.ToString());
564
565 var dialog_handle = OpenFileDialog(handle);
566
567 if (dialog_handle == IntPtr.Zero)
568 {
569 gLog.Error.WriteLine("Unable to find the DXTViewer's Open File Dialog...");
570 throw new InvalidOperationException("Unable to find the DXTViewer's Open File Dialog...");
571 }
572 else
573 {
574 gLog.Debug.WriteLine("Found DXTViewer's OpenFileDialog. Handle=0x{0:x8}", dialog_handle);
575 OpenFile(handle, dialog_handle, InitParams.TargetImage);
576 }
577 }
578
579 }
580
581 private static void SendKey(IntPtr handle, string keys)
582 {
583 SetForegroundWindow(handle);
584 SendKeys.SendWait(keys);
585 }
586 private static IntPtr OpenFileDialog(IntPtr handle)
587 {
588 #if !DEV_DEBUG
589 var t = GetConfigparameter("dxtviewer_openfiledialog_title");
590 Consts.dxtviewer_openfiledialog_title = t;
591 #endif
592 gLog.Debug.WriteLine("Consts.dxtviewer_openfiledialog_title: {0}", Consts.dxtviewer_openfiledialog_title);
593 SendKey(handle, "%F");
594 SendKey(handle, "{ENTER}");
595 // get openfiledialog handle
596 var openfiledialog_handle = IntPtr.Zero;
597 var windowHandles = new ArrayList();
598 EnumedWindow callBackPtr = GetWindowHandle;
599 if (!EnumWindows(callBackPtr, windowHandles))
600 {
601 gLog.Error.WriteLine("Failed to enumerate windows...");
602 }
603 else
604 {
605 foreach (IntPtr windowhandle in windowHandles)
606 {
607 try
608 {
609 uint pid = 0;
610 GetWindowThreadProcessId(windowhandle, out pid);
611
612 int length = GetWindowTextLength(windowhandle);
613 StringBuilder windowTitle = new StringBuilder(length + 1);
614 GetWindowText(windowhandle, windowTitle, windowTitle.Capacity);
615 GetWindowThreadProcessId(windowhandle, out pid);
616 if (windowTitle.ToString() == Consts.dxtviewer_openfiledialog_title && pid == Instance.Id)
617 {
618 openfiledialog_handle = windowhandle;
619 gLog.Debug.WriteLine("Process: {0} (id=0x{1:x8} windowhandle=0x{2:x8}) title='{3}'", Consts.ProcessExecutable, pid, windowhandle.ToInt32(), windowTitle);
620 //break;
621 }
622 }
623 catch (Exception ex)
624 {
625 gLog.Error.WriteLine(ex.ToString());
626 }
627 }
628 }
629 return openfiledialog_handle;
630 }
631 private static void OpenFile(IntPtr MainHandle, IntPtr dialog_handle, string file)
632 {
633
634 gLog.Debug.WriteLine("dialog_handle::0x{0:x8}", dialog_handle);
635 // get handle to filename TextBox control
636 const int filename_index = 0x17;
637 IntPtr txtFilename_handle = IntPtr.Zero;
638
639 var windowHandles = new ArrayList();
640 EnumedWindow callBackPtr = GetWindowHandle;
641 if (!EnumChildWindows(dialog_handle, callBackPtr, windowHandles))
642 {
643 gLog.Error.WriteLine("Failed to enumerate child windows...MainWindowHandle=0x{0:x8}", dialog_handle);
644 return;
645 }
646 else
647 {
648 gLog.Debug.WriteLine("Found: {0} child window handles for MainWindowHandle=0x{1:x8}", windowHandles.Count, dialog_handle);
649 }
650 var handles = windowHandles.Cast<IntPtr>().ToList();
651 txtFilename_handle = handles[filename_index];
652 SendKey(txtFilename_handle, file);
653 SendKey(dialog_handle, "{ENTER}");
654 }
655 #endregion
656 #region Exceptions
657 internal class Exceptions
658 {
659 #region ProcessInstanceNotFoundException
660 internal class ProcessInstanceNotFoundException : Exception
661 {
662 static private string InternalMessage
663 {
664 get { return string.Format("Unable to find a running instance of: {0}", DXTViewer.Consts.ProcessExecutable); }
665 }
666 static private string InternalMessageCaption
667 {
668 get { return string.Format("{0} instance not found", DXTViewer.Consts.ProcessExecutable); }
669 }
670 public ProcessInstanceNotFoundException() : this(null) { }
671 public ProcessInstanceNotFoundException(Exception innerException) : this(false, innerException) { }
672 public ProcessInstanceNotFoundException(bool DisplayMessage) : this(DisplayMessage, null) { }
673 public ProcessInstanceNotFoundException(bool DisplayMessage, Exception innerException)
674 : base(InternalMessage, innerException)
675 {
676 gLog.Error.WriteLine(this.ToString());
677 if (DisplayMessage)
678 {
679 MessageBox.Show(InternalMessage, InternalMessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
680 }
681 }
682 }
683 #endregion
684
685 #region ModuleNotFoundException
686 internal class ModuleNotFoundException : Exception
687 {
688 static private string InternalMessage
689 {
690 get { return string.Format("Unable to find module: {0}", Consts.ProcessExecutable); }
691 }
692 static private string InternalMessageCaption
693 {
694 get { return string.Format("{0} module not found", Consts.ProcessExecutable); }
695 }
696 public ModuleNotFoundException() : this(null) { }
697 public ModuleNotFoundException(Exception innerException) : this(false, innerException) { }
698 public ModuleNotFoundException(bool DisplayMessage) : this(DisplayMessage, null) { }
699 public ModuleNotFoundException(bool DisplayMessage, Exception innerException)
700 : base(InternalMessage, innerException)
701 {
702 gLog.Error.WriteLine(this.ToString());
703 if (DisplayMessage)
704 {
705 MessageBox.Show(InternalMessage, InternalMessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
706 }
707 }
708 }
709 #endregion
710
711 #region ConfigNotFoundException
712 internal class ConfigNotFoundException : Exception
713 {
714 static private string InternalMessage
715 {
716 get { return string.Format("Unable to find config: {0}", Consts.ConfigName); }
717 }
718 static private string InternalMessageCaption
719 {
720 get { return string.Format("{0} config not found", Consts.ConfigName); }
721 }
722 public ConfigNotFoundException() : this(null) { }
723 public ConfigNotFoundException(Exception innerException) : this(false, innerException) { }
724 public ConfigNotFoundException(bool DisplayMessage) : this(DisplayMessage, null) { }
725 public ConfigNotFoundException(bool DisplayMessage, Exception innerException)
726 : base(InternalMessage, innerException)
727 {
728 gLog.Error.WriteLine(this.ToString());
729 if (DisplayMessage)
730 {
731 MessageBox.Show(InternalMessage, InternalMessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
732 }
733 }
734 }
735 #endregion
736
737 #region ConfigParameterNotFoundException
738 internal class ConfigParameterNotFoundException : Exception
739 {
740 static private string InternalMessage(string parameterName)
741 {
742 return string.Format("Unable to find config parameter: {0}", parameterName);
743 }
744 static private string InternalMessageCaption(string parameterName)
745 {
746 return string.Format("{0} parameter not found", parameterName);
747 }
748 public ConfigParameterNotFoundException(string parameterName) : this(parameterName, null) { }
749 public ConfigParameterNotFoundException(string parameterName, Exception innerException) : this(false, parameterName, innerException) { }
750 public ConfigParameterNotFoundException(bool DisplayMessage, string parameterName) : this(DisplayMessage, parameterName, null) { }
751 public ConfigParameterNotFoundException(bool DisplayMessage, string parameterName, Exception innerException)
752 : base(InternalMessage(parameterName), innerException)
753 {
754 gLog.Error.WriteLine(this.ToString());
755 if (DisplayMessage)
756 {
757 MessageBox.Show(InternalMessage(parameterName), InternalMessageCaption(parameterName), MessageBoxButtons.OK, MessageBoxIcon.Error);
758 }
759 }
760 }
761 #endregion
762
763 #region UnableToStartProcessException
764 internal class UnableToStartProcessException : Exception
765 {
766 static private string InternalMessage
767 {
768 get { return string.Format("Unable to start process: {0}", Consts.ProcessExecutable); }
769 }
770 static private string InternalMessageCaption
771 {
772 get { return string.Format("{0} process not started", Consts.ProcessExecutable); }
773 }
774 public UnableToStartProcessException() : this(null) { }
775 public UnableToStartProcessException(Exception innerException) : this(false, innerException) { }
776 public UnableToStartProcessException(bool DisplayMessage) : this(DisplayMessage, null) { }
777 public UnableToStartProcessException(bool DisplayMessage, Exception innerException)
778 : base(InternalMessage, innerException)
779 {
780 gLog.Error.WriteLine(this.ToString());
781 if (DisplayMessage)
782 {
783 MessageBox.Show(InternalMessage, InternalMessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
784 }
785 }
786 }
787 #endregion
788
789 #region PluginNotFoundException
790 internal class ImageNotFoundException : Exception
791 {
792 static private string InternalMessage(string pluginfile)
793 {
794 return string.Format("Unable to find plugin: {0}", pluginfile);
795 }
796 static private string InternalMessageCaption(string pluginfile)
797 {
798 return string.Format("{0} plugin not found", pluginfile);
799 }
800 public ImageNotFoundException(string pluginfile) : this(pluginfile, null) { }
801 public ImageNotFoundException(string pluginfile, Exception innerException) : this(false, pluginfile, innerException) { }
802 public ImageNotFoundException(bool DisplayMessage, string pluginfile) : this(DisplayMessage, pluginfile, null) { }
803 public ImageNotFoundException(bool DisplayMessage, string pluginfile, Exception innerException)
804 : base(InternalMessage(pluginfile), innerException)
805 {
806 gLog.Error.WriteLine(this.ToString());
807 if (DisplayMessage)
808 {
809 MessageBox.Show(InternalMessage(pluginfile), InternalMessageCaption(pluginfile), MessageBoxButtons.OK, MessageBoxIcon.Error);
810 }
811 }
812 }
813 #endregion
814
815 #endregion
816 }
817 }
818
819 }

  ViewVC Help
Powered by ViewVC 1.1.22