Need to activate a window

左心房为你撑大大i 提交于 2019-11-26 21:04:59

问题


I have a situation like this. I have the window handle of an application. I need to activate it. I tried all these functions but is not working always.(most of the time , it doesn't work the first time and I'll have to manually click on it to activate it. Second attempt onwards it works fine) The reason why I am doing this is because I have code written in the Form.Activate event of the form which I need to execute. Application is a single instance application. When a new instance is created , it first checks for the existence of any other process, If found, the handle of old process is passed to the these functions so that user can work on the old form. Application is called from a different C application. [DllImport("user32.dll")] public static extern int ShowWindow(IntPtr hWnd, int nCmdShow);

    [DllImport("user32.dll")]
    public static extern int SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32")]
    public static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);

回答1:


SetForgroundWindow only works if its process has input focus. This is what I use:

public static void forceSetForegroundWindow( IntPtr hWnd, IntPtr mainThreadId )
{
    IntPtr foregroundThreadID = GetWindowThreadProcessId( GetForegroundWindow(), IntPtr.Zero );
    if ( foregroundThreadID != mainThreadId )
    {
        AttachThreadInput( mainThreadId, foregroundThreadID, true );
        SetForegroundWindow( hWnd );
        AttachThreadInput( mainThreadId, foregroundThreadID, false );
    }
    else
        SetForegroundWindow( hWnd );
}



回答2:


You need to find the window using something like Window title and then active it as follows:

public class Win32 : IWin32
{
    //Import the FindWindow API to find our window
    [DllImport("User32.dll", EntryPoint = "FindWindow")]
    private static extern IntPtr FindWindowNative(string className, string windowName);

    //Import the SetForeground API to activate it
    [DllImport("User32.dll", EntryPoint = "SetForegroundWindow")]
    private static extern IntPtr SetForegroundWindowNative(IntPtr hWnd);

    public IntPtr FindWindow(string className, string windowName)
    {
        return FindWindowNative(className, windowName);
    }

    public IntPtr SetForegroundWindow(IntPtr hWnd)
    {
        return SetForegroundWindowNative(hWnd);
    }
}

public class SomeClass
{
    public void Activate(string title)
    {
        //Find the window, using the Window Title
        IntPtr hWnd = win32.FindWindow(null, title);
        if (hWnd.ToInt32() > 0) //If found
        {
            win32.SetForegroundWindow(hWnd); //Activate it
        }
    }
}



回答3:


You have to get the form using FromHandle:

f = Control.FromHandle(handle)

then you can can call Activate on the result:

 f.Activate()


来源:https://stackoverflow.com/questions/3787057/need-to-activate-a-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!