是否有人知道在停用表单时确定哪个窗口将获得焦点的方法?
答案 0 :(得分:7)
我找到了答案。在WndProc中处理WM_ACTIVATE消息(用于激活和取消激活),而不是订阅激活和取消激活事件。由于它报告了窗口的句柄被激活,我可以将该句柄与我的表单的句柄进行比较,并确定焦点是否正在改变它们中的任何一个。
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);
}
}
}
答案 1 :(得分:0)
您应该能够使用Form.ActiveForm
静态属性确定活动表单。
答案 2 :(得分:0)
我在实现自动完成弹出窗口时遇到了同样的问题(类似于VS中的智能感知窗口)。
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();
}
}