public void ChangeActiveProcess(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) { Process pr = ChangingProcess.GetActiveProcess(); if (FirstTime) { StartTime = DateTime.Now; CurrentProgram = GetObservedProgramFromProcess(pr); FirstTime = false; dT = new DispatcherTimer(new TimeSpan(0, 0, 0, 0, 50), DispatcherPriority.Background, t_Tick, Dispatcher.CurrentDispatcher); dT.IsEnabled = true; } else { PreviousProgram = CurrentProgram; TimeSpan ts = DateTime.Now - StartTime; PreviousProgram.AddTimeDuration(DateTime.Now - StartTime); ProgramsDataGrid.Items.Refresh(); StartTime = DateTime.Now; CurrentProgram = CurrentProgram = GetObservedProgramFromProcess(pr); } liveIcon.Source = CurrentProgram.IconSource; liveName.Text = CurrentProgram.Name; }
public static ObservedProgram GetObservedProgramFromProcess(Process pr) { string prName = pr.ProcessName; string filePath = pr.MainModule.FileName; Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filePath); ImageSource imgSr; using (Bitmap bmp = icon.ToBitmap()) { var stream = new MemoryStream(); bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png); imgSr = BitmapFrame.Create(stream); } ObservedProgram potential = new ObservedProgram(prName, imgSr); foreach (ObservedProgram cos in ProgramsDataGrid.Items) { if (potential == cos) { return(cos); } } ProgramsDataGrid.Items.Add(potential); return(potential); }