alexsendar
2017-02-15 08:06
采纳率: 80%
浏览 2.2k
已采纳

Win32 listbox 怎样获取行选中事件?

如下代码段, WndProc 哪个case里做处理?wm_command? 还是 wm_notify?
最好贴下case 处理代码。

 HWND hListBox=0;  
    hListBox = CreateWindow(WC_LISTBOX,  
        NULL,  
        WS_CHILD|WS_VSCROLL | WS_TABSTOP | LBS_STANDARD
        ,
        x, 23,
        100, rcClient.bottom - rcClient.top - 100,
        parentWnd, (HMENU)IDC_ListBox,   
        (HINSTANCE) GetWindowLong(parentWnd, GWL_HINSTANCE),   
        NULL);
    ShowWindow(hListBox,SW_SHOW);

    SendMessage(hListBox,LB_ADDSTRING ,0,(LPARAM)"Clear");
    SendMessage(hListBox,LB_ADDSTRING ,0,(LPARAM)"你好");
    SendMessage(hListBox,LB_ADDSTRING ,0,(LPARAM)"我好");
    SendMessage(hListBox,LB_ADDSTRING ,0,(LPARAM)"他好");

图片说明

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • oyljerry 2017-02-15 09:25
    已采纳

    http://stackoverflow.com/questions/20984949/c-getting-information-from-a-listbox-with-lb-getcursel-and-lb-gettext

    处理WM_COMMAND

     INT_PTR CALLBACK ListBoxExampleProc(HWND hDlg, UINT message, 
        WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
        case WM_INITDIALOG:
            {
                // Add items to list. 
                HWND hwndList = GetDlgItem(hDlg, IDC_LIST1);  
                for (int i = 0; i < ARRAYSIZE(Roster); i++) 
                { 
                    int pos = (int)SendMessage(hwndList, LB_ADDSTRING, 0, 
                        (LPARAM) Roster[i].achName); 
                    // we do not need the rest from Microsoft's example
                } 
                // Set input focus to the list box.
                SetFocus(hwndList); 
                return TRUE;               
            } 
    
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
            case IDOK:
            case IDCANCEL:
              EndDialog(hDlg, LOWORD(wParam));
              return TRUE;
    
            case IDC_LIST1:
                {
                    switch (HIWORD(wParam)) 
                    { 
                    case LBN_DBLCLK:
                        {
                            HWND hwndList = GetDlgItem(hDlg, IDC_LIST1); 
    
                            // Get selected index.
                            int lbItem = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0); 
    
                            // display item's text 
                            TCHAR buff[MAX_PATH];
                            SendMessage(hwndList, LB_GETTEXT, lbItem, (LPARAM)buff);
    
                            SetDlgItemText(hDlg, IDC_EDIT1, buff); 
                            return TRUE; 
                        } 
                    }
                }
                return TRUE;
            }
        }
        return FALSE;
    }
    
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题