フォームが非アクティブ化されたときにアクティブ化が行われる場所を決定する

StackOverflow https://stackoverflow.com/questions/813356

質問

フォームが非アクティブ化されたときにどのウィンドウがフォーカスを取得するかを決定する方法を知っていますか?

役に立ちましたか?

解決

答えが見つかりました。アクティブ化および非アクティブ化イベントにサブスクライブする代わりに、 WM_ACTIVATE < WndProcの/ a>メッセージ(アクティブ化と非アクティブ化の両方に使用)。アクティブ化されているウィンドウのハンドルを報告するので、そのハンドルをフォームのハンドルと比較し、フォーカスがそれらのいずれかに変更されているかどうかを判断できます。

const int WM_ACTIVATE = 0x0006;
const int WA_INACTIVE = 0;
const int WA_ACTIVE = 1;  
const int WA_CLICKACTIVE = 2;  

protected override void WndProc(ref Message m)  
{  
    if (m.Msg == WM_ACTIVATE)  
    {  
         // When m.WParam is WA_INACTIVE, the window is being deactivated and
         // m.LParam is the handle of the window that will be activated.

         // When m.WParam is WA_ACTIVE or WA_CLICKACTIVE, the window is being 
         // activated and m.LParam is the handle of the window that has been 
         // deactivated.
    }  

    base.WndProc(ref m);  
} 

編集:このメソッドは、適用するウィンドウの外側(たとえば、ポップアップウィンドウの外側)で使用できます。

NativeWindowを使用して、ハンドルに基づいて任意のウィンドウにアタッチし、メッセージループを表示できます。以下のコード例を参照してください:

public class Popup : Form
{
    const int WM_ACTIVATE = 0x0006;
    const int WA_INACTIVE = 0;
    private ParentWindowIntercept parentWindowIntercept;

    public Popup(IntPtr hWndParent)
    {
        this.parentWindowIntercept = new ParentWindowIntercept(hWndParent);
    }

    private class ParentWindowIntercept : NativeWindow
    {
        public ParentWindowIntercept(IntPtr hWnd)
        {
            this.AssignHandle(hWnd);
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == WM_ACTIVATE)
            {
                if ((int)m.WParam == WA_INACTIVE)
                {
                    IntPtr windowFocusGoingTo = m.LParam;
                    // Compare handles here
                }
            }

            base.WndProc(ref m);
        } 
    }
}

他のヒント

Form.ActiveForm 静的プロパティを使用して、アクティブなフォームを決定できるはずです。

オートコンプリートポップアップウィンドウを実装するときに同じ問題が発生します(VSのIntellisenseウィンドウと同様)。

WndProc アプローチの問題は、ポップアップをホストするフォームにコードを追加する必要があることです。

代替アプローチは、タイマーを使用して、短い間隔の後にForm.ActiveControlをチェックすることです。このように、コードはエディターコントロールまたはポップアップフォーム内にカプセル化されます

Form _txPopup;

// Subscribe whenever convenient
public void IntializeControlWithPopup(Form _hostForm)
{
    _hostForm.Deactivate + OnHostFormDeactivate;
}

Timer _deactivateTimer;
Form _hostForm;
void OnHostFormDeactivate(object sender, EventArgs e)
{
    if (_deactivateTimer == null) 
    {
        _mainForm = sender as Form;
        _deactivateTimer = new Timer();
        _deactivateTimer.Interval = 10;
        _deactivateTimer.Tick += DeactivateTimerTick;
    }
    _deactivateTimer.Start();
}

void  DeactivateTimerTick(object sender, EventArgs e)
{
    _deactivateTimer.Stop();
    Form activeForm = Form.ActiveForm;
    if (_txPopup != null && 
        activeForm != _txPopup && 
        activeForm != _mainForm) 
    { 
        _txPopup.Hide(); 
    }
}
scroll top