Exemplo n.º 1
0
        public static WindowDetails ParseString(string s)
        {
            // Console.WriteLine("Parsing w '" + s + "'");
            // was \\(([-0-9]*),([-0-9]*)\\)-\\(([-0-9]*),([-0-9]*)\\)
            // was Match m = Regex.Match(s, "^([0-9]+) ([0-9]+) \\[([0-9]+)\\] (.*) '(.*)' '(.*)'$");
            Match m = Regex.Match(s, "^([0-9]+) ([0-9]+) \\[([0-9]+)\\] \\(([-0-9]*),([-0-9]*)\\)-\\(([-0-9]*),([-0-9]*)\\) '(.*)' '(.*)'$");

            if (m.Success)
            {
                WindowDetails wd = new WindowDetails();
                wd.pid    = Convert.ToInt32(m.Groups[1].Value);
                wd.hwnd   = Convert.ToInt32(m.Groups[2].Value);
                wd.iconId = Convert.ToInt32(m.Groups[3].Value);
                // was  "(" + m.Groups[4].Value + "," + m.Groups[5].Value + ")-(" + m.Groups[6].Value + "," + m.Groups[7].Value + ")";
                // wd.dimensions = m.Groups[4].Value;
                wd.dimensions     = "(" + m.Groups[4].Value + "," + m.Groups[5].Value + ")-(" + m.Groups[6].Value + "," + m.Groups[7].Value + ")";
                wd.dimensionsRect = new Rectangle(
                    Convert.ToInt32(m.Groups[4].Value),
                    Convert.ToInt32(m.Groups[5].Value),
                    Convert.ToInt32(m.Groups[6].Value) - Convert.ToInt32(m.Groups[4].Value),
                    Convert.ToInt32(m.Groups[7].Value) - Convert.ToInt32(m.Groups[5].Value));


                wd.module = m.Groups[8].Value;
                wd.title  = m.Groups[9].Value;
                return(wd);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        public override bool Equals(object other)
        {
            if (!(other is WindowDetails))
            {
                return(false);
            }
            WindowDetails o = (WindowDetails)other;

            return(o.pid == pid && o.hwnd == hwnd &&
                   o.dimensions.Equals(dimensions) &&
                   ((o.module == null && module == null) || o.module.Equals(module)) &
                   ((o.title == null && title == null) || o.title.Equals(title)));
        }
Exemplo n.º 3
0
 private void lstProcess_SelectedIndexChanged(object sender, EventArgs e)
 {
     if (lstProcess.SelectedItems.Count > 0)
     {
         ListViewItem  lvi = lstProcess.SelectedItems[0];
         WindowDetails wd  = (WindowDetails)lvi.Tag;
         lblStatusBar.Text = "Window " + wd.title + ": dimensions = " + wd.dimensions;
         desktopView.setHighlight(wd.dimensionsRect);
     }
     else
     {
         lblStatusBar.Text = "No window selected";
     }
 }
Exemplo n.º 4
0
        private void showTime(DateTime dateTime)
        {
            // get desktop
            string jpgFile = dataDir + "\\" + dateTime.ToString("yyyy-MM-dd") + "\\desktop-" +
                             dateTime.ToString("HH.mm.ss") + ".1234.jpg";

            backImage = Image.FromFile(jpgFile);

            desktopView.setImage(backImage);
            // pbxScreen.Invalidate();

            shownDate = dateTime;

            // get process list / windows
            // Open the file and read it back.
            List <ProcessDetails> pds = new List <ProcessDetails>();
            List <WindowDetails>  wds = new List <WindowDetails>();
            string datafile           = dataDir + "\\" + dateTime.ToString("yyyy-MM-dd") + "\\windowList.log";

            using (System.IO.StreamReader sr = System.IO.File.OpenText(datafile)) {
                string   s           = "";
                string   country     = sr.ReadLine();
                DateTime parsingDate = new DateTime();
                Boolean  eof         = false;

                while ((!eof) && (s = sr.ReadLine()) != null)
                {
                    // s = s.Trim();
                    if (s.StartsWith("*** Begin snapshot "))
                    {
                        string      d           = s.Substring(19, 8);
                        CultureInfo cultureInfo = CultureInfo.CurrentCulture;
                        parsingDate = DateTime.ParseExact(d, "HH.mm.ss", cultureInfo);
                        parsingDate = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day,
                                                   parsingDate.Hour, parsingDate.Minute, parsingDate.Second);
                    }
                    else if (s.StartsWith("P + "))
                    {
                        ProcessDetails pd = ProcessDetails.ParseString(s.Substring(4));
                        if (pd != null)
                        {
                            pds.Add(pd);
                        }
                        else
                        {
                            Console.WriteLine("Could not parse P+: " + s);
                        }
                    }
                    else if (s.StartsWith("W + "))
                    {
                        WindowDetails wd = WindowDetails.ParseString(s.Substring(4));
                        if (wd != null)
                        {
                            wds.Add(wd);
                        }
                        else
                        {
                            Console.WriteLine("Could not parse w+: " + s);
                        }
                    }
                    else if (s.StartsWith("WO ! "))
                    {
                        List <int>      newOrder = new List <int>();
                        StringTokenizer st       = new StringTokenizer(s.Substring(5), " ");
                        while (st.HasMoreTokens())
                        {
                            newOrder.Add(Convert.ToInt32(st.NextToken()));
                        }
                    }

                    // read next line
                    s = sr.ReadLine();
                }
            }

            lstProcess.BeginUpdate();
            lstProcess.Items.Clear();
            foreach (WindowDetails wd in wds)
            {
                // @TODO check if window is visible
                Console.WriteLine("Adding wd '" + wd.title + "'");
                ListViewItem lvi = new ListViewItem(new string[] { wd.title }, Convert.ToString(wd.iconId));
                lvi.Text = wd.title;
                lvi.Tag  = wd;
                // lvi.ImageIndex = lstProcess.SmallImageList.Images.IndexOfKey();
                lstProcess.Items.Add(lvi);
            }
            lstProcess.EndUpdate();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Create a map of hwnd's to WindowDetails objects, as well as an ordered list of
        /// windows.
        /// </summary>
        ///
        /// <param name="newHwndOrder">a list which will be modified by this method to contain the
        /// window list</param>
        /// <returns>a map as described above</returns>
        public Hashtable getWindowMap(List <int> newHwndOrder)
        {
            Hashtable newMap = new Hashtable();

            Win32.RECT rc   = new Win32.RECT();
            IntPtr     hwnd = Win32.GetDesktopWindow();

            hwnd = Win32.GetTopWindow(hwnd);  // get first window

            int pid;

            if (IntPtr.Zero == hwnd)
            {
                return(null);
            }
            StringBuilder title  = new StringBuilder(1000);
            StringBuilder module = new StringBuilder(1000);

            //StringBuilder path = new StringBuilder(1000);
            while (IntPtr.Zero != hwnd)
            {
                IntPtr threadId = Win32.GetWindowThreadProcessId(hwnd, out pid);
                if (Win32.IsWindowVisible(hwnd) != 0)
                {
                    newHwndOrder.Add(hwnd.ToInt32());
                    IntPtr processHwnd = Win32.OpenProcess(0, false, pid);  // pid ?

                    //path.Length = 0;
                    module.Length = 0;
                    title.Length  = 0;
                    // Win32.GetModuleFileName(processHwnd, path, 1000); // just gets this .exe's filename
                    Win32.GetWindowModuleFileName(hwnd, module, 1000);   // seems to be empty most of the time ?
                    Win32.GetWindowText(hwnd, title, 1000);
                    Win32.GetWindowRect(hwnd, ref rc);

                    // Console.WriteLine("{0} {1} ({2},{3})-({4},{5}) '{6}' '{7}'", pid, hwnd.ToInt32(), rc.left, rc.top, rc.right, rc.bottom, module, title);
                    WindowDetails wd = new WindowDetails();
                    wd.pid        = pid;
                    wd.hwnd       = hwnd.ToInt32();
                    wd.dimensions = String.Format("({0},{1})-({2},{3})", rc.left, rc.top, rc.right, rc.bottom);
                    wd.title      = title.ToString();
                    wd.module     = module.ToString();
                    // wd.path = path.ToString();
                    newMap.Add(wd.pid + ":" + wd.hwnd, wd);

                    /* skip icon saving; check http://www.kennyandkarin.com/Kenny/CodeCorner/Tools/IconBrowser/
                     * later for some ideas
                     * - has memory addresss issues ...
                     * IntPtr hIcon = (IntPtr)Win32.SendMessage(hwnd, 0x007f, 0, 0);   // WM_GETICON
                     * if (IntPtr.Zero != hIcon) {
                     *  Icon ic = Icon.FromHandle(hwnd);
                     *  Bitmap b = ic.ToBitmap();
                     *  ic.Dispose();
                     *  FileStream fs = new FileStream(String.Format(@"c:\temp\icons\{0}.ico", iconNum), FileMode.Create, FileAccess.Write);
                     *  // ic.Save(fs);
                     *  b.Save(fs, System.Drawing.Imaging.ImageFormat.Icon);
                     *  fs.Close();
                     *  iconNum++;
                     * }
                     */

                    // other code from  http://www.codeproject.com/csharp/taskbarsorter.asp
                    UInt32 hIcon = 0;
                    if (hIcon == 0)
                    {
                        hIcon = Win32.SendMessage(hwnd, WM.GETICON, ICON.SMALL2, 0);
                    }
                    if (hIcon == 0)
                    {
                        hIcon = Win32.GetClassLong(hwnd, GCL.HICONSM);
                    }
                    if (hIcon == 0)
                    {
                        hIcon = Win32.GetClassLong(hwnd, GCL.HICON);
                    }
                    if (hIcon != 0)
                    {
                        Bitmap bitmap = null;
                        try {
                            Int32 hIcon2 = unchecked ((Int32)hIcon);
                            bitmap = Bitmap.FromHicon(new IntPtr(hIcon2));
                        } catch (ArgumentException) { continue; }
                        if (bitmap != null)
                        {
                            // hash bitmap; if it's new, save it.
                            string hash = getBitmapHash(bitmap);
                            if (iconMap.ContainsKey(hash))
                            {
                                // exists
                                wd.iconId = (int)iconMap[hash];
                            }
                            else
                            {
                                wd.iconId     = iconNum;
                                iconMap[hash] = iconNum;
                                if (!Directory.Exists(SaveDirectory + "\\iconCache"))
                                {
                                    Directory.CreateDirectory(SaveDirectory + "\\iconCache");
                                }
                                FileStream fs = new FileStream(String.Format(SaveDirectory + "\\iconCache\\{0}.png", iconNum), FileMode.Create, FileAccess.Write);
                                bitmap.Save(fs, System.Drawing.Imaging.ImageFormat.Png);
                                bitmap.Dispose();
                                iconNum++;
                            }
                        }
                    }
                }
                hwnd = Win32.GetNextWindow(hwnd, Win32.GW_HWNDNEXT);
            }
            return(newMap);
        }