【问题标题】:Get the current active application name获取当前活动的应用程序名称
【发布时间】:2013-06-25 01:48:58
【问题描述】:

我想在计时器停止时获取当前活动应用程序的名称。录制 20 秒后,它应该会显示当前活动的应用程序名称。我尝试了一些代码。你可以在这里看到。但是在计时器停止后它没有向我显示任何内容。

C#代码:

public class Win32wrapper 
{
    private System.Timers.Timer pingTimer;
    private Timer recordTimer;

    private List<HarvestApp.ProcessInformation> ProcessList = new List<HarvestApp.ProcessInformation>();

    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll")]
    public static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint processId);

    private DateTime recordStartTime;

    public void startTimer(int pingTimerValue=5000, int recordTimerValue=20000)
    {

        pingTimer = new System.Timers.Timer(pingTimerValue);
        pingTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
        pingTimer.Interval = pingTimerValue;
        pingTimer.Enabled = true;

        recordTimer = new Timer(recordTimerValue);
        recordTimer.Elapsed += new ElapsedEventHandler(OnRecordEvent);
        recordTimer.Interval = recordTimerValue;
        recordTimer.Enabled = true;

        recordStartTime = DateTime.Now;
    }

    private void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Ping Elapsed event was raised at {0}", e.SignalTime);


        //Record through win32dll the application foreground caption

        GetActiveFileNameTitle();

        //Store into collection object, Push into ArrayList, Push into process id
    }


    public String GetActiveFileNameTitle()
    {
        IntPtr hWnd = GetForegroundWindow();
        uint processId;
        GetWindowThreadProcessId(hWnd, out processId);
        Process p = Process.GetProcessById((int)processId);
        //p.MainModule.FileName.Dump();
        return p.ProcessName;
    }

        private void OnRecordEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Record Elapsed event was raised at {0}", e.SignalTime);

        ProcessInformation procTemp = GetMaxRunTimeForApplicationsBetween(recordStartTime, DateTime.Now);
        Harvest_TimeSheetEntry tempEntry = new Harvest_TimeSheetEntry(procTemp, recordStartTime, DateTime.Now);

        //Add to the list of the specific day Only not the entire
       // Globals._globalController.harvestManager._TIMESHEETENTRYDICTIONARY[recordStartTime.Date].Add(tempEntry);


        Globals._globalController.getDayViewWindow.Dispatcher.BeginInvoke(new Action(delegate()
        {
            Globals._globalController.getDayViewWindow.addHarvestEntrytoView(tempEntry);
         }));
        //Clean Out the ProcessList?

        recordStartTime = DateTime.Now;
    }


    public void stopTimer()
    {
        pingTimer.Stop();
        recordTimer.Stop();
    }

【问题讨论】:

  • 你想知道当前窗口的标题吗? stackoverflow.com/questions/115868/…
  • 我想要应用程序的名称。如果记事本在后台运行,它应该在那里显示我的名字为“记事本”。
  • 问题是:你要前台app的窗口标题还是进程名?
  • 这和 WPF 有什么关系?
  • @Zicore 我不想要窗口标题。我想要当前处于活动状态的进程的名称。当我运行我的应用程序时,如果 Paint 处于活动状态,那么它的名称(即 Paint)应该在计时器启动 20 秒后出现。

标签: c# wpf timer application-name


【解决方案1】:

编辑:标题具有误导性。

观看您的代码:

private void OnTimedEvent(object source, ElapsedEventArgs e)
{
    Console.WriteLine("The Ping Elapsed event was raised at {0}", e.SignalTime);

    //Record through win32dll the application foreground caption

    // You miss the output or usage of the return value here.
    GetActiveFileNameTitle();
    // Try 
    var procName = GetActiveFileNameTitle();
    Console.WriteLine(procName);

    //Store into collection object, Push into ArrayList, Push into process id
}

工作示例:

[DllImport("user32.dll")]
public static extern IntPtr GetForegroundWindow();

获取前景窗口和:

[DllImport("user32.dll", SetLastError = true)]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

确定ProcessID。使用 ProcessID,您可以获得进程的名称。 使用进程类:http://msdn.microsoft.com/de-de/library/system.diagnostics.process.mainmodule.aspx

class Program
{
    static void Main(string[] args)
    {
        Thread.Sleep(5000); // Test it with 5 Seconds, set a window to foreground, and you see it works!
        IntPtr hWnd = GetForegroundWindow();
        uint procId = 0;
        GetWindowThreadProcessId(hWnd, out procId);
        var proc = Process.GetProcessById((int)procId);
        Console.WriteLine(proc.MainModule);
        Console.ReadKey();
    }

    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();

    [DllImport("user32.dll", SetLastError = true)]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
}

【讨论】:

    猜你喜欢
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 1970-01-01
    • 2022-12-30
    • 1970-01-01
    • 2011-04-21
    • 1970-01-01
    相关资源
    最近更新 更多