0

I sent a WM_ACTIVE message using postmessage api to some programs. When a program is deactivated, sending a message does not actually activate the window, but the program thinks it is active. ( It actually succeeded. ) However, I think it is very inefficient to send postmessage regularly. If I want to check the WM_ACTIVE value of the program and it is inactivated, I try to send a WM_ACTIVE message again using the POSTMESSAGE API to confuse the program itself with being active, but I can't think of a way. Although there is an idea that hooking would be easy to use, C# did not support other types of global hooking except for the keyboard and mouse. Can anyone come up with any other ideas? please help me.

최현석
  • 11
  • 2

1 Answers1

0

To check if a process is focused you should use GetForegroundWindow to get the focused window handle and then use GetWindowThreadProcessId to get the process from that window handle:

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

    [DllImport("user32.dll")]
    static extern uint GetWindowThreadProcessId(int hWnd, out int ProcessId);

    IntPtr focusedWindow = GetForegroundWindow(); //get the focused window
    int focusedProcessID = 0;
    GetWindowThreadProcessID(focusedWindow, out focusedProcessID); //get it's process id
    Process focusedProcess = Process.GetProcessById(focusedProcessID);//get the focused process
    Console.WriteLine("Current Focused Process:" + focusedProcess.ProcessName);