C# Force Form Focus

前端 未结 8 1920
不思量自难忘°
不思量自难忘° 2020-12-02 14:46

So, I did search google and SO prior to asking this question. Basically I have a DLL that has a form compiled into it. The form will be used to display information to the sc

相关标签:
8条回答
  • 2020-12-02 15:15

    Here is some code that I've used on one form or another for a few years. There are a few gotchas to making a window in another app pop up. Once you have the window handle do this:

          if (IsIconic(hWnd))
            ShowWindowAsync(hWnd, SW_RESTORE);
    
          ShowWindowAsync(hWnd, SW_SHOW);
    
          SetForegroundWindow(hWnd);
    
          // Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
          // Converted to Delphi by Ray Lischner
          // Published in The Delphi Magazine 55, page 16
          // Converted to C# by Kevin Gale
          IntPtr foregroundWindow = GetForegroundWindow();
          IntPtr Dummy = IntPtr.Zero;
    
          uint foregroundThreadId = GetWindowThreadProcessId(foregroundWindow, Dummy);
          uint thisThreadId       = GetWindowThreadProcessId(hWnd, Dummy);
    
          if (AttachThreadInput(thisThreadId, foregroundThreadId, true))
          {
            BringWindowToTop(hWnd); // IE 5.5 related hack
            SetForegroundWindow(hWnd);
            AttachThreadInput(thisThreadId, foregroundThreadId, false);
          }
    
          if (GetForegroundWindow() != hWnd)
          {
            // Code by Daniel P. Stasinski
            // Converted to C# by Kevin Gale
            IntPtr Timeout = IntPtr.Zero;
            SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, Timeout, 0);
            SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Dummy, SPIF_SENDCHANGE);
            BringWindowToTop(hWnd); // IE 5.5 related hack
            SetForegroundWindow(hWnd);
            SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Timeout, SPIF_SENDCHANGE);
          }
    

    I won't post the whole unit since since it does other things that aren't relevant but here are the constants and imports for the above code.

    //Win32 API calls necesary to raise an unowned processs main window
    
    [DllImport("user32.dll")]
    
    private static extern bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")]
    private static extern bool IsIconic(IntPtr hWnd);
    [DllImport("user32.dll", SetLastError = true)]
    private static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
    [DllImport("user32.dll", SetLastError = true)]
    private static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr lpdwProcessId);
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();
    [DllImport("user32.dll")]
    private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
    [DllImport("user32.dll")]
    static extern bool BringWindowToTop(IntPtr hWnd);
    
    [DllImport("user32.dll")] 
    private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, Int32 nMaxCount);
    [DllImport("user32.dll")]
    private static extern int GetWindowThreadProcessId(IntPtr hWnd, ref Int32 lpdwProcessId);
    [DllImport("User32.dll")]
    public static extern IntPtr GetParent(IntPtr hWnd); 
    
    private const int SW_HIDE = 0;
    private const int SW_SHOWNORMAL = 1;
    private const int SW_NORMAL = 1;
    private const int SW_SHOWMINIMIZED = 2;
    private const int SW_SHOWMAXIMIZED = 3;
    private const int SW_MAXIMIZE = 3;
    private const int SW_SHOWNOACTIVATE = 4;
    private const int SW_SHOW = 5;
    private const int SW_MINIMIZE = 6;
    private const int SW_SHOWMINNOACTIVE = 7;
    private const int SW_SHOWNA = 8;
    private const int SW_RESTORE = 9;
    private const int SW_SHOWDEFAULT = 10;
    private const int SW_MAX = 10;
    
    private const uint SPI_GETFOREGROUNDLOCKTIMEOUT = 0x2000;
    private const uint SPI_SETFOREGROUNDLOCKTIMEOUT = 0x2001;
    private const int  SPIF_SENDCHANGE = 0x2;
    
    0 讨论(0)
  • 2020-12-02 15:16

    TopMost = true; .Activate() ?

    Either of those any good?

    Splitting it out into its own thread is a bit evil as it wont work properly if you don't call it with Application.Run and that will swallow up the thread. In the worst case scenario I guess you could separate it out into a different process and communicate via the disk or WCF.

    0 讨论(0)
  • 2020-12-02 15:18

    The following solution should meet your requirements:

    1. Assembly can be loaded into PowerShell and main class instantiated
    2. When ShowMessage method on this instance is called, a new window is shown and activated
    3. If you call ShowMessage multiple times, this same window updates its title text and is activated
    4. To stop using the window, call Dispose method

    Step 1: Let's create a temporary working directory (you can naturally use your own dir)

    (powershell.exe)
    mkdir C:\TEMP\PshWindow
    cd C:\TEMP\PshWindow
    

    Step 2: Now let's define class that we will be interacting with in PowerShell:

    // file 'InfoProvider.cs' in C:\TEMP\PshWindow
    using System;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace PshWindow
    {
        public sealed class InfoProvider : IDisposable
        {
            public void Dispose()
            {
                GC.SuppressFinalize(this);
                lock (this._sync)
                {
                    if (!this._disposed)
                    {
                        this._disposed = true;
                        if (null != this._worker)
                        {
                            if (null != this._form)
                            {
                                this._form.Invoke(new Action(() => this._form.Close()));
                            }
                            this._worker.Join();
                            this._form = null;
                            this._worker = null;
                        }
                    }
                }
            }
    
            public void ShowMessage(string msg)
            {
                lock (this._sync)
                {
                    // make sure worker is up and running
                    if (this._disposed) { throw new ObjectDisposedException("InfoProvider"); }
                    if (null == this._worker)
                    {
                        this._worker = new Thread(() => (this._form = new MyForm(this._sync)).ShowDialog()) { IsBackground = true };
                        this._worker.Start();
                        while (this._form == null || !this._form.Created)
                        {
                            Monitor.Wait(this._sync);
                        }
                    }
    
                    // update the text
                    this._form.Invoke(new Action(delegate
                    {
                        this._form.Text = msg;
                        this._form.Activate();
                    }));
                }
            }
    
            private bool _disposed;
            private Form _form;
            private Thread _worker;
            private readonly object _sync = new object();
        }
    }
    

    As well as the Form that will be shown:

    // file 'MyForm.cs' in C:\TEMP\PshWindow
    using System;
    using System.Drawing;
    using System.Threading;
    using System.Windows.Forms;
    
    namespace PshWindow
    {
        internal sealed class MyForm : Form
        {
            public MyForm(object sync)
            {
                this._sync = sync;
                this.BackColor = Color.LightGreen;
                this.Width = 200;
                this.Height = 80;
                this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
            }
    
            protected override void OnShown(EventArgs e)
            {
                base.OnShown(e);
                this.TopMost = true;
    
                lock (this._sync)
                {
                    Monitor.PulseAll(this._sync);
                }
            }
    
            private readonly object _sync;
        }
    }
    

    Step 3: Let's compile the assembly...

    (powershell.exe)
    csc /out:PshWindow.dll /target:library InfoProvider.cs MyForm.cs
    

    Step 4: ... and load the assembly in PowerShell to have fun with it:

    (powershell.exe)
    [System.Reflection.Assembly]::LoadFile('C:\TEMP\PshWindow\PshWindow.dll')
    $a = New-Object PshWindow.InfoProvider
    $a.ShowMessage('Hello, world')
    

    A green-ish window with title 'Hello, world' should now pop-up and be active. If you reactivate the PowerShell window and enter:

    $a.ShowMessage('Stack overflow')
    

    The Window's title should change to 'Stack overflow' and the window should be active again.

    To stop working with our window, dispose the object:

    $a.Dispose()
    

    This solution works as expected in both Windows XP SP3, x86 and Windows Vista SP1, x64. If there are question about how this solution works I can update this entry with detailed discussion. For now I'm hoping the code if self-explanatory.

    0 讨论(0)
  • 2020-12-02 15:21

    Don't you just want the dialog to be a child of the calling form?

    To do that you'll need the pass in the calling window and use the ShowDialog( IWin32Window owner ) method.

    0 讨论(0)
  • 2020-12-02 15:22

    Doesn't ShowDialog() have different window behavior than just Show()?

    What if you tried:

    msgFrm.Show();
    msgFrm.BringToFront();
    msgFrm.Focus();
    
    0 讨论(0)
  • 2020-12-02 15:22

    You shouldn't need to import any win32 functions for this. If .Focus() isn't enough the form should also have a .BringToFront() method you can use. If that fails, you can set it's .TopMost property to true. You don't want to leave it true forever, so then call Application.DoEvents so the form can process that message and set it back to false.

    0 讨论(0)
提交回复
热议问题