WPF how to focus the previous Window?

1

Good people,

I have a window in ToolBox WindowStyle="ToolWindow" which serves only to inform the user what is happening in BackgroundWorker

But I'm having a problem, for example I have this code that is in the Activated event:

private void WindowDocumentPriceCheck_Activated(object sender, EventArgs e)
{
    this.Activated -= WindowDocumentPriceCheck_Activated;
    BackgroundWorker workerFillTheLists = new BackgroundWorker();
    workerFillTheLists.DoWork += this.workerFillTheLists_DoWork;
    workerFillTheLists.RunWorkerCompleted += this.workerFillTheLists_RunWorkerCompleted;
    this.windowWaitForWorker = new WindowWaitForWorker("Informação", "Por favor aguarde!");
    this.windowWaitForWorker.WindowStartupLocation = WindowStartupLocation.CenterScreen;
    this.windowWaitForWorker.Closed += this.WindowWaitForWorker_Closed;
    workerFillTheLists.RunWorkerAsync();
    // Quero que o utilizador não tenha a hipotesses de carregar nos botões da janela anterior dai estar com ShowDialog();
    this.windowWaitForWorker.ShowDialog();
}

The problem is when the Window windowWaitForWorker quits it does not focus the previous window.

I have tried several things, this add the event Closed activate or give focus and nothing

private void WindowWaitForWorker_Closed(object sender, EventArgs e)
{
    this.Activate();
    this.Topmost = true;  // important
    this.Topmost = false; // important
    this.Focus();
    this.textBoxBarCode.Focus();
}

Here is the code that is behind WindowWaitForWorker

public partial class WindowWaitForWorker : Window
{
    private const int GWL_STYLE = -16;
    private const int WS_SYSMENU = 0x00080000;

    [DllImport("user32.dll", SetLastError = true)]
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [DllImport("user32.dll")]
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    /// <summary>
    /// Consctuctor that will receive the title and message to show
    /// </summary>
    /// <param name="title">Title for the window</param>
    /// <param name="message">Message for the user</param>
    public WindowWaitForWorker(string title, string message)
    {
        InitializeComponent();
        this.Title = title;
        this.textBoxMessage.Text = message;
        this.Loaded += this.WindowWaitForWorker_Loaded;
    }

    /// <summary>
    /// Updates the message for the user to see
    /// </summary>
    /// <param name="message">Message for the user</param>
    public void UpdateMessage(string message)
    {
        this.textBoxMessage.Text = message;
    }

    private void WindowWaitForWorker_Loaded(object sender, RoutedEventArgs e)
    {
        var hwnd = new WindowInteropHelper(this).Handle;
        SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU);
    }
}

Nothing you've tried worked out because you wanted the window to focus as soon as WindowWaitForWorker is closed AKA when BackgroundWorker

    
asked by anonymous 25.09.2018 / 17:27

1 answer

0

Well, the situation has been resolved, starting WindowWaitForWorker on your own Thread

try
{
    Thread newWindowThread = new Thread(new ThreadStart(() =>
    {
        Dispatcher.Invoke(new Action(() =>
        {
            this.windowWaitForWorker = new WindowWaitForWorker("Informação", "Por favor aguarde!");
            this.windowWaitForWorker.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            this.windowWaitForWorker.ShowDialog();
        }));
    }));
    newWindowThread.SetApartmentState(ApartmentState.STA);
    newWindowThread.IsBackground = false;
    newWindowThread.Start();
}
catch (Exception ex)
{
}

this must be within BackgroundWorker DoWork

I can not really understand why this happens, I've always used these methods in WinForm and always did everything as it should, but it looks like in WPF it has to be done like this.

This answer was given to me in another site that I asked, here is the link to where this information is located: #

    

26.09.2018 / 10:40