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

Diff of /trunk/DXTViewerLoader/DXTViewer.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/DXTViewerLoader/DXTViewer.cs	2013/12/12 09:35:42	7
+++ trunk/DXTViewerLoader/DXTViewer.cs	2013/12/12 11:55:39	8
@@ -81,19 +81,22 @@
             return true;
         }
 
-        //public static void sendKey(IntPtr hwnd, Keys keyCode, bool extended)
+        ////public static void sendKey(IntPtr hwnd, char key, bool extended)
+        ////{
+        ////    PostMessage(hwnd, WM_CHAR, (IntPtr)key, (IntPtr)0);
+        ////}
+        //public static void sendKey(IntPtr hwnd, char key, bool extended)
         //{
-        //    uint scanCode = MapVirtualKey((uint)keyCode, 0);
+        //    //uint scanCode = MapVirtualKey((uint)key, 0);
         //    uint lParam;
         //    //KEY DOWN
-        //    lParam = (0x00000001 | (scanCode << 16));
+        //    lParam = (0x00000001);// | (scanCode << 16));
         //    if (extended)
         //    {
         //        lParam = lParam | 0x01000000;
         //    }
-        //    PostMessage(hwnd, WM_KEYDOWN, (IntPtr)keyCode, (IntPtr)lParam);
-        //    //KEY UP
-        //    PostMessage(hwnd, WM_KEYUP, (IntPtr)keyCode, (IntPtr)lParam);
+        //    PostMessage(hwnd, WM_KEYDOWN, (IntPtr)key, (IntPtr)lParam);
+        //    //PostMessage(hwnd, (UInt32)WM_KEYUP, (IntPtr)key, (IntPtr)lParam);
         //}
         [DllImport("user32.dll")]
         static extern bool InvalidateRect(IntPtr hWnd, IntPtr lpRect, bool bErase);
@@ -194,6 +197,11 @@
 #else
              internal const string dxtviewer_window_title="";
 #endif
+#if DEV_DEBUG
+            internal const string dxtviewer_openfiledialog_title = "Load an RGB Byte Image";
+#else
+             internal const string dxtviewer_openfiledialog_title="";
+#endif
             //internal const string ProcessName = "DXTViewer";
             internal static string ProcessExecutable
             {
@@ -543,9 +551,19 @@
                 StringBuilder windowTitle = new StringBuilder(length + 1);
                 GetWindowText(handle, windowTitle, windowTitle.Capacity);
                 gLog.Debug.WriteLine("Windowhandle: 0x{0:x8} ... Title: '{1}'", handle, windowTitle.ToString());
-                
-                SendKey(handle, "%F");
-                SendKey(handle, "{ENTER}");
+
+                var dialog_handle = OpenFileDialog(handle);
+
+                if (dialog_handle == IntPtr.Zero)
+                {
+                    gLog.Error.WriteLine("Unable to find the DXTViewer's Open File Dialog...");
+                    throw new InvalidOperationException("Unable to find the DXTViewer's Open File Dialog...");
+                }
+                else
+                {
+                    gLog.Debug.WriteLine("Found DXTViewer's OpenFileDialog. Handle=0x{0:x8}", dialog_handle);
+                    OpenFile(handle, dialog_handle, InitParams.TargetImage);
+                }
                 #region old-code child windows
                 //IntPtr btnOKHandle = IntPtr.Zero;
                 //foreach (IntPtr WindowHandle in WindowHandles)
@@ -656,7 +674,79 @@
             SetForegroundWindow(handle);
             SendKeys.SendWait(keys);
         }
+        private static IntPtr OpenFileDialog(IntPtr handle)
+        {
+            SendKey(handle, "%F");
+            SendKey(handle, "{ENTER}");
+            // get openfiledialog handle
+            var openfiledialog_handle = IntPtr.Zero;
+            var windowHandles = new ArrayList();
+            EnumedWindow callBackPtr = GetWindowHandle;
+            if (!EnumWindows(callBackPtr, windowHandles))
+            {
+                gLog.Error.WriteLine("Failed to enumerate windows...");
+            }
+            else
+            {
+                foreach (IntPtr windowhandle in windowHandles)
+                {
+                    try
+                    {
+                        uint pid = 0;
+                        GetWindowThreadProcessId(windowhandle, out pid);
 
+                        int length = GetWindowTextLength(windowhandle);
+                        StringBuilder windowTitle = new StringBuilder(length + 1);
+                        GetWindowText(windowhandle, windowTitle, windowTitle.Capacity);
+                        GetWindowThreadProcessId(windowhandle, out pid);
+                        if (windowTitle.ToString() == Consts.dxtviewer_openfiledialog_title && pid == Instance.Id)
+                        {
+                            openfiledialog_handle = windowhandle;
+                            gLog.Debug.WriteLine("Process: {0} (id=0x{1:x8} windowhandle=0x{2:x8}) title='{3}'", Consts.ProcessExecutable, pid, windowhandle.ToInt32(), windowTitle);
+                            //break;
+                        }
+                    }
+                    catch (Exception ex)
+                    {
+                        gLog.Error.WriteLine(ex.ToString());
+                    }
+                }
+            }
+            return openfiledialog_handle;
+        }
+        private static void OpenFile(IntPtr MainHandle, IntPtr dialog_handle, string file)
+        {
+            // get handle to filename TextBox control
+            const int filename_index = 0x17;
+            IntPtr txtFilename_handle = IntPtr.Zero;
+
+            var windowHandles = new ArrayList();
+            EnumedWindow callBackPtr = GetWindowHandle;
+            if (!EnumChildWindows(dialog_handle, callBackPtr, windowHandles))
+            {
+                gLog.Error.WriteLine("Failed to enumerate child windows...MainWindowHandle=0x{0:x8}", dialog_handle.ToInt32());
+                return;
+            }
+            else
+            {
+                gLog.Debug.WriteLine("Found: {0} child window handles for MainWindowHandle=0x{1:x8}", windowHandles.Count, dialog_handle.ToInt32());
+            }
+            var handles = windowHandles.Cast<IntPtr>().ToList();
+            txtFilename_handle = handles[filename_index];
+
+            //// set the filename
+            //foreach (var ch in file.ToCharArray())
+            //{
+            //    SendKey(txtFilename_handle, ch.ToString());
+            //    //Thread.Sleep(100);
+            //}
+            SendKey(txtFilename_handle, file);
+            Thread.Sleep(100);
+            //Thread.Sleep(100);
+            // send enter to dialog handle
+            SendKey(dialog_handle, "{ENTER}");
+            //Thread.Sleep(100);
+        }
         #endregion
         #region Exceptions
         internal class Exceptions

 

  ViewVC Help
Powered by ViewVC 1.1.22