2011-02-04 43 views
3

我添加了一個Listbox控件到一個名爲IDC_LIST1的對話框資源。我應該使用SendDlgItemMessage()與此控件進行交互,還是WTL有更好的方法?這是我的事件處理程序。它沒什麼特別的!我應該使用SendDlgItemMessage還是在WTL中有這個包裝?

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; 
} 

回答

0

您可以使用WTL :: CListBoxT圍繞一個Win32列表框的包裝......爲此,你需要在列表框的HWND,你可以使用獲取函數GetDlgItem。

CListBoxT提供了InsertString和DeleteString方法。

1

的WTL 建議方法是如下:

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()); 
    } 
// ... 
}; 

常見和Windows控件WTL支持類是atlctrls.h,你也可以看看WTL for MFC Programmers, Part IV - Dialogs and Controls

相關問題