Should I use SendDlgItemMessage or is there a wrapper for this in WTL?

I added a Listbox control to a dialog resource called IDC_LIST1. Should I interact with this control using SendDlgItemMessage(), or is there a better way with WTL? Here are my event handlers. Nothing has been invented yet!

LRESULT OnAddItem(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    SendDlgItemMessage(IDC_LIST1, LB_INSERTSTRING, (WPARAM) 0, (LPARAM)_T("Hi"));
    return 0;
}

LRESULT OnRemoveItem(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    // Get selected item
    int item = SendDlgItemMessage(IDC_LIST1, LB_GETCURSEL, (WPARAM) 0, (LPARAM) 0);
    // Remove the item at the index of the selected item
    SendDlgItemMessage(IDC_LIST1, LB_DELETESTRING, (WPARAM) 0, (LPARAM)item);
    return 0;
}
+3
source share
2 answers

The proposed WTL method is as follows:

class CMyDlg : public CDialogImpl<CMyDlg>
{
public:
    enum {IDD = IDD_MYDLG};
    CListBox m_lb1;
// ...
    BEGIN_MSG_MAP(CMyDlg)
        MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
        COMMAND_ID_HANDLER(ID_ADDITEM, OnAddItem)
        COMMAND_ID_HANDLER(ID_REMOVEITEM, OnRemoveItem)
        // ...
    END_MSG_MAP()
// ...
    LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
        m_lb1.Attach(GetDlgItem(IDC_LIST1));
        // ...
    }
    LRESULT OnAddItem(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        return m_lb1.AddString(_T("Hi"));
    }
    LRESULT OnRemoveItem(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
        return m_lb1.DeleteString(m_lb1.GetCurSel());
    }
// ...
};

WTL support classes for regular and Windows controls are in atlctrls.h, you can also see WTL for MFC programmers, part IV - dialog boxes and controls .

+1
source

WTL:: CListBoxT Win32... HWND, GetDlgItem.

CListBoxT InsertString DeleteString.

0

Source: https://habr.com/ru/post/1789902/


All Articles