Try this. I can see it working with these changes below.
BOOL CGenericPopup::iCreatePopup(CButton* pParent)
{
m_pParent = pParent;
// Get the class name and create the window
CString szClassName = AfxRegisterWndClass(CS_CLASSDC | CS_SAVEBITS | CS_HREDRAW | CS_VREDRAW,0,NULL,0);
CRect rcBtn;
pParent->GetWindowRect(rcBtn);
if (!CWnd::CreateEx(WS_EX_PALETTEWINDOW, szClassName, _T(""), WS_POPUP, rcBtn.right, rcBtn.bottom, 400, 200, pParent->GetSafeHwnd(), 0, NULL))
return FALSE;
m_listBox = new MyListBox;
m_listBox->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | LBS_NOTIFY, CRect(0, 0, 100, 120), this, 1);
m_listBox->AddString(L"One");
m_listBox->AddString(L"Two");
m_listBox->AddString(L"Three");
m_listBox->SetFont(pParent->GetFont());
m_listBox->SetFocus();
ShowWindow(SW_SHOWNA);
// Capture all mouse events for the listbox
if (m_listBox)
m_listBox->SetCapture();
return TRUE;
}
//---------- MYListBox.cpp ------
BEGIN_MESSAGE_MAP(MyListBox, CListBox)
ON_CONTROL_REFLECT(LBN_SELCHANGE, &MyListBox::OnLbnSelchange)
END_MESSAGE_MAP()
void MyListBox::OnLbnSelchange()
{
// TODO: Add your control notification handler code here
int index = GetCurSel();
CString str;
GetText(index, str);
//CString msg;
//msg.Format(L"item select: %s", str);
//::AfxMessageBox(msg);
}
LRESULT MyListBox::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if (message == WM_LBUTTONUP )
{
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
CPoint pt(xPos, yPos);
ClientToScreen(&pt);
CRect rc;
GetWindowRect(rc);
if (!rc.PtInRect(pt))
{
ReleaseCapture();
GetParent()->DestroyWindow();
return 1;
}
}
else if (message == WM_KEYDOWN && wParam == VK_ESCAPE)
{
ReleaseCapture();
GetParent()->DestroyWindow();
return 1;
}
else if (message == WM_CAPTURECHANGED)
{
SetCapture();
}
return CListBox::WindowProc(message, wParam, lParam);
}