WPF how to give Focus to previous Window?

Asked

Viewed 42 times

1

Good staff,

I have a window in Toolbox WindowStyle="ToolWindow" which only serves to give information to the user what is happening on BackgroundWorker

But I am having a problem, I for example have this following 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 closes it not from the previous window.

I’ve tried several things, from 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’s the code that’s behind the 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 I’ve tried has worked, because I wanted the window to have Focus as soon as the WindowWaitForWorker is closed AKA when the work of BackgroundWorker

1 answer

0


Well the situation was resolved, initiating the WindowWaitForWorker in his 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 has to be inside the BackgroundWorker DoWork

Honestly I can not understand the reason of this happening, I always used these methods in Winform and always did everything as it should be, but it seems that in WPF have to be done like this.

This answer was given to me in another place I asked, here is the link to where this information is: Where Did the Answer came from

Browser other questions tagged

You are not signed in. Login or sign up in order to post.