确定激活会当窗体被关闭 [英] Determine Where Activation Is Going When A Form Is Deactivated

查看:257
本文介绍了确定激活会当窗体被关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道一种方法来确定哪个窗口是要接收焦点当窗体被停用?

Does anyone know of a way to determine which window is going to receive focus when a form is deactivated?

推荐答案

我找到了答案。相反,订阅激活和停用事件,处理 WM_ACTIVATE 消息(同时用于激活和失活)的WndProc中。由于报告的窗口的句柄被激活,我可以在手柄比较我的形式处理和确定的重点正在发生变化,以其中任何一个。

I found the answer. Instead of subscribing to the activated and deactivate events, handle the WM_ACTIVATE message (used for both activation and deactivation) in WndProc. Since it reports the handle of the window being activated, I can compare that handle to the handles of my forms and determine if focus is changing to any of them.

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);  
} 

编辑:此方法可以适用于(例如,超出在弹出窗口)的窗口之外使用

This method can be used outside the window it applies to (e.g. outside in a popup window).

您可以使用的NativeWindow附加到基于其处理任何窗口,并查看其消息循环。见下面code例如:

You can use NativeWindow to attach to any window based on its handle and view its message loop. See code example below:

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);
        } 
    }
}

这篇关于确定激活会当窗体被关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆