int HandledWidget::showPopup()
{
int nRet(-1);
show(SW_SHOWNOACTIVATE);
BOOL bMenuDestroyed(FALSE);
BOOL bMsgQuit(FALSE);
HWND m_hWndOwner = GetWindow(m_hWnd, GW_OWNER);
assert(GetForegroundWindow() == m_hWndOwner);
while(TRUE)
{
if(GetProp(m_hWnd, MENU_EXIT_NOTIFY) != 0)
{
nRet = (int)GetProp(m_hWnd, MENU_EXIT_COMMAND_ID);
break;
}
if(GetActiveWindow() != m_hWndOwner)
{
break;
}
m_bIsPopingUp = true;
MSG msg = {0};
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if(msg.message == WM_KEYDOWN
|| msg.message == WM_SYSKEYDOWN
|| msg.message == WM_KEYUP
|| msg.message == WM_SYSKEYUP
|| msg.message == WM_CHAR
|| msg.message == WM_IME_CHAR
|| msg.message == WM_MOUSEWHEEL
)
{
//transfer the message to menu window
msg.hwnd = m_hWnd;
}
else if(msg.message == WM_LBUTTONDOWN
|| msg.message == WM_RBUTTONDOWN
|| msg.message == WM_NCLBUTTONDOWN
|| msg.message == WM_NCRBUTTONDOWN)
{
//click on other window
if(msg.hwnd != m_hWnd)
{
m_bIsPopingUp = false;
DestroyWindow(m_hWnd);
bMenuDestroyed = TRUE;
}
}
else if(msg.message == WM_QUIT)
{
bMsgQuit = TRUE;
}
TranslateMessage (&msg);
DispatchMessageW (&msg);
}
else
{
MsgWaitForMultipleObjects (0, 0, 0, 10, QS_ALLINPUT);
}
if(bMenuDestroyed) break;
if(bMsgQuit)
{
PostQuitMessage(msg.wParam);
break;
}
}
m_bIsPopingUp = false;
if(!bMenuDestroyed) DestroyWindow(m_hWnd);
return nRet;
}
bool HandledWidget::exitPopup(int nCommandId)
{
BOOL bRet = SetProp(m_hWnd, MENU_EXIT_NOTIFY, (HANDLE)1);
SetProp(m_hWnd, MENU_EXIT_COMMAND_ID, (HANDLE)nCommandId);
return bRet;
}
et poignée WM_MOUSEACTIVATE:
LRESULT ComboxList::onMouseActivate(WPARAM wParam, LPARAM lParam, bool &bHandled)
{
bHandled = true;
return MA_NOACTIVATE;
}
rappeler ne pas activer la nouvelle fenêtre à l'aide SetFocus/SetWindowPos (SW_SHOWWINDOW)/ShowWindow (SW_SHOW)/SetActivate/SetForegroundWindow etc.
Ensuite, vous obtenez une fenêtre non active, tout comme le menu du système.
Référence: http://www.cppblog.com/weiym/archive/2013/04/07/199189.html
était inattentif :-(Lire "FakeMenu" par R.Chen (SDK) – kero
Kero, thx pour votre réponse, je trouve fakemenu aide vraiment, mais pourriez-vous développer votre réponse plus. détails. – Jichao