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.
            Asked
            
        
        
            Active
            
        
            Viewed 83 times
        
    0
            
            
        - 
                    Do you just want to give a process the focus? – TheGeneral Feb 17 '20 at 05:46
 - 
                    I don't think I need to know if the process is focused. But.... I think that could be helpful. Is there any way to make sure it's focused? – 최현석 Feb 17 '20 at 05:50
 - 
                    https://stackoverflow.com/questions/2315561/correct-way-in-net-to-switch-the-focus-to-another-application – TheGeneral Feb 17 '20 at 06:06
 - 
                    I'll refer to it. Thank you. – 최현석 Feb 17 '20 at 06:18
 
1 Answers
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);