窗口设置为无效,文本框窗口无法获得焦点,无法输入文本

时间:2013-12-19 08:04:05

标签: c# wpf windows

我使用下面的代码来设置我的WPF窗口不要获得焦点,但是设置完成后,这个窗口无法在文本框中输入文本,如何使这个窗口既没有得到焦点,也可以在其中在文本框中输入文字?

#region set window no focus
public const int GWL_EXSTYLE = -20;
public const int WS_EX_NOACTIVATE = 0x8000000;
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr window, int index, int value);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr window, int index);
/// <summary>
/// set window no focus
/// </summary>
/// <param name="hwnd"></param>
public static void SetWindowNoFocus(IntPtr hwnd)
{
    SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_NOACTIVATE );
}
#endregion set window no focus

1 个答案:

答案 0 :(得分:0)

    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    public static void BringToFront(string title)
    {
        // Get a handle to the Calculator application.
        IntPtr handle = FindWindow(null, title);

        // Verify that Calculator is a running process.
        if (handle == IntPtr.Zero)
        {
            return;
        }

        // Make Calculator the foreground application
        SetForegroundWindow(handle);
    }