Beispiel #1
0
        public static WindowInfo GetActiveProcessWindow()
        {
            WindowInfo wi = new WindowInfo();

            try
            {
                IntPtr hWnd = GetForegroundWindow();
                if (hWnd != IntPtr.Zero)
                {
                    Process process = GetWindowProcess(hWnd);
                    if (process != null)
                    {
                        try
                        {
                            wi.ProcessId   = process.Id;
                            wi.ProcessName = process.ProcessName;
                            wi.FileName    = GetProcessFile(process.Id);
                        }
                        catch (Exception ex) { TraceLogger.Instance.WriteLineInfo("Open process fail:" + wi.ProcessName); TraceLogger.Instance.WriteException(ex); }
                        process.Dispose();
                    }

                    wi.ScreenWidth  = Screen.PrimaryScreen.Bounds.Width;
                    wi.ScreenHeight = Screen.PrimaryScreen.Bounds.Height;

                    wi.WindowHandle = hWnd.ToInt32();
                    wi.WindowRect   = WindowCaptureEngine.GetWindowRect(hWnd);
                    wi.WindowTitle  = TextCaptureEngine.GetWindowTitle(hWnd);
                    wi.WindowUrl    = TextCaptureEngine.GetWindowUrl(hWnd);

                    ///? GetControlText cause double click invalid.
                    //wi.ControlText = TextCaptureEngine.GetControlText(hWnd);
                }
            }
            catch (Exception ex) { TraceLogger.Instance.WriteException(ex); throw; }
            return(wi);
        }
Beispiel #2
0
        private void SnapshotWorker(object state)
        {
            TraceLogger.Instance.WriteEntracne();
            IRawEvent[] evts = state as IRawEvent[];
            Debug.Assert(evts != null && evts.Length > 0);
            var lastEvt = evts[evts.Length - 1];

            try
            {
                if (!IsRecording)
                {
                    return;
                }
                TraceLogger.Instance.WriteLineVerbos("SnapshotWorker do record at ticks: " + DateTime.Now.Ticks);

                WindowInfo wi    = SnapshotEngine.GetActiveProcessWindow();
                Snapshot   sshot = new Snapshot()
                {
                    SessionId  = _session.SessionId,
                    SnapshotId = Guid.NewGuid().ToString("n"),
                    SnapTime   = DateTime.Now,

                    ProcessId   = wi.ProcessId,
                    ProcessName = wi.ProcessName,
                    FileName    = wi.FileName,

                    ScreenWidth  = wi.ScreenWidth,
                    ScreenHeight = wi.ScreenHeight,

                    WindowHandle = wi.WindowHandle,
                    WindowRect   = wi.WindowRect,
                    WindowTitle  = wi.WindowTitle,
                    WindowUrl    = wi.WindowUrl,
                };
                // image
                if (Global.Config.RecordImage)
                {
                    Image img = WindowCaptureEngine.CaptureScreen();
                    if (Global.Config.DebugDumpOriginal)
                    {
                        try
                        {
                            string path = Path.Combine(CacheManager.CachePath, string.Format(@"{0}\{1}.png", SessionId, sshot.SnapshotId));
                            (img as Bitmap).Save(path, System.Drawing.Imaging.ImageFormat.Png);
                        }
                        catch (Exception ex) { TraceLogger.Instance.WriteException(ex); }
                    }
                    bool isGrayScale;
                    sshot.ImageData   = GetImageData(img, out isGrayScale);
                    sshot.IsGrayScale = isGrayScale;
                    sshot.Mouse       = GetMouseState(lastEvt.MouseEvent);
                }

                // text
                if (lastEvt.IsKeyEvent)
                {
                    sshot.ControlText = TextCaptureEngine.GetControlText(new IntPtr(wi.WindowHandle));
                }
                sshot.InputText  = _policy.GetText(evts);
                sshot.EventsData = RawInputEvent.ToBinary(evts);

                // flush
                _cacheManager.WriteSnapshot(_session, sshot);

                // set active
                _session.LastActiveTime = DateTime.Now;

                // debug
                if (Global.Config.DebugDumpText)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(sshot.InputText))
                        {
                            File.AppendAllText(Path.Combine(CacheManager.CachePath, "InputText.txt"), sshot.InputText + Environment.NewLine);
                        }
                        if (!string.IsNullOrEmpty(sshot.ControlText))
                        {
                            File.AppendAllText(Path.Combine(CacheManager.CachePath, "ControlText.txt"), sshot.ControlText + Environment.NewLine);
                        }
                        if (!string.IsNullOrEmpty(sshot.WindowUrl))
                        {
                            File.AppendAllText(Path.Combine(CacheManager.CachePath, "WindowUrl.txt"), sshot.WindowUrl + Environment.NewLine);
                        }
                    }
                    catch (Exception ex) { TraceLogger.Instance.WriteException(ex); }
                }
            }
            catch (Exception ex) { TraceLogger.Instance.WriteException(ex); }
            TraceLogger.Instance.WriteExit();
        }