how to Get And Send Msg from PopUp ListBox to Parant->Parant class

Rocky Shr 41 Reputation points
2020-09-24T17:54:37.763+00:00

Dear All,

trying to make a custom property grid. with the help of SM-4238 i made PopUp ListBox and it comes fine.

But
in my custom Listbox, I use SelChange. and it is not getting any msg when I select items. so how do I get it? and how to send rise event to Parant->Parant class from ListBox.

void x_ListBox::OnLbnSelchange()
{
    try
    {
        auto sel_Idx = GetCurSel();
        ReleaseCapture();
        GetParent()->DestroyWindow();
    }
    catch (CException* p)
    {
        p->Delete();
    }
}

I use that list box in the PopUp Window class, and PopUp window class in the Dialog box.

if possible, I like to use it as a normal CListbox in DialogBox made in MFC. like when we double click it will give event and msg by the wizard. Like this

BEGIN_MESSAGE_MAP(x_ListBox, CListBox)
ON_CONTROL_REFLECT(LBN_SELCHANGE, &x_ListBox::OnLbnSelchange)
END_MESSAGE_MAP()

I need to get data from the Listbox to DialobBox class. looking for help.

Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. SM 416 Reputation points
    2020-09-28T18:41:14.89+00:00

    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);
    
    }
    
    1 person found this answer helpful.
    0 comments No comments

8 additional answers

Sort by: Most helpful
  1. SM 416 Reputation points
    2020-09-24T22:50:48.607+00:00

    1.Create a dialog with no borders and Popup Style in your resource editor.
    2.Add a listbox inside the dialog and make its edges flush with the edges of the dialog.

    IDD_DIALOG1 DIALOGEX 0, 0, 133, 129
    STYLE DS_SETFONT | DS_FIXEDSYS | WS_POPUP | WS_SYSMENU
    FONT 8, "MS Shell Dlg", 400, 0, 0x1
    BEGIN
        LISTBOX         IDC_LIST1,0,0,133,129,LBS_SORT | LBS_NOINTEGRALHEIGHT | WS_VSCROLL | WS_TABSTOP
    END
    

    3. Create a dialog class for this IDD_DIALOG1, say for eg. PopupDlg and add the following message handler.

    BEGIN_MESSAGE_MAP(PopupDlg, CDialogEx)
        ON_LBN_SELCHANGE(IDC_LIST1, OnSelChange)
    END_MESSAGE_MAP()
    
    
    void PopupDlg::OnSelChange()
    {
        // TODO: Add your control notification handler code here
    
    }
    
    1. When you want to show this popup, simply create the modeless dialog and manage its positioning and lifecycle.

    -SM

    0 comments No comments

  2. Rocky Shr 41 Reputation points
    2020-09-25T05:06:14.453+00:00

    thanks for your reply. well, yes. it might work well. But I am looking for Some Message Map or any subclass windows or any win32 way to do it.

    want to learn runtime dynamic creating control.

    0 comments No comments

  3. Seetharam Misro 1 Reputation point
    2020-09-26T19:43:14.447+00:00
    0 comments No comments

  4. Rocky Shr 41 Reputation points
    2020-09-27T03:51:49.667+00:00

    yes, I have that sample. and I follow it.
    in that code sample

    BEGIN_MESSAGE_MAP(MyListCtrl, CListCtrl)
        ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, &MyListCtrl::OnLvnItemchanged)
    END_MESSAGE_MAP()
    

    what is for ListBox? when I use the same in ListBox it does not work. and how to use it.

    here is my MessageMap

    BEGIN_MESSAGE_MAP(x_ListBox, CListBox)
        ON_WM_DRAWITEM_REFLECT()
        ON_WM_MEASUREITEM_REFLECT()
        ON_WM_COMPAREITEM_REFLECT()
         ON_NOTIFY_REFLECT(LVN_ITEMCHANGED, &x_ListBox::OnLbnSelchange)
    END_MESSAGE_MAP()
    

    thanks for your help

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.