0 c++ listbox visual-studio-2013
我想创建这样简单的允许多重选择的列表框,为此,我做了以下操作:顺便说一句,我正在使用 Visual Studio 2013 Ultimate
#include <iostream>
#include "Windows.h"
#include "Strsafe.h"
#include "Afxres.h"
typedef struct
{
TCHAR achName[MAX_PATH];
TCHAR achPosition[12];
int nGamesPlayed;
int nGoalsScored;
} Player;
Player Roster[] =
{
{TEXT("Haas, Jonathan"), TEXT("Midfield"), 18, 4 },
{TEXT("Pai, Jyothi"), TEXT("Forward"), 36, 12 },
{TEXT("Hanif, Kerim"), TEXT("Back"), 26, 0 },
{TEXT("Anderberg, Michael"), TEXT("Back"), 24, 2 },
{TEXT("Jelitto, Jacek"), TEXT("Midfield"), 26, 3 },
{TEXT("Raposo, Rui"), TEXT("Back"), 24, 3},
{TEXT("Joseph, Brad"), TEXT("Forward"), 13, 3 },
{TEXT("Bouchard, Thomas"), TEXT("Forward"), 28, 5 },
{TEXT("Salmre, Ivo "), TEXT("Midfield"), 27, 7 },
{TEXT("Camp, David"), TEXT("Midfield"), 22, 3 },
{TEXT("Kohl, Franz"), TEXT("Goalkeeper"), 17, 0 },
};
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_LISTBOX_EXAMPLE);
for (int i = 0; i < ARRAYSIZE(Roster); i++)
{
int pos = (int)SendMessage(hwndList, LB_ADDSTRING, 0,
(LPARAM) Roster[i].achName);
// Set the array index of the player as item data.
// This enables us to retrieve the item from the array
// even after the items are sorted by the list box.
SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM) i);
}
// 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_LISTBOX_EXAMPLE:
{
switch (HIWORD(wParam))
{
case LBN_SELCHANGE:
{
HWND hwndList = GetDlgItem(hDlg, IDC_LISTBOX_EXAMPLE);
// Get selected index.
int lbItem = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0);
// Get item data.
int i = (int)SendMessage(hwndList, LB_GETITEMDATA, lbItem, 0);
// Do something with the data from Roster[i]
TCHAR buff[MAX_PATH];
StringCbPrintf (buff, ARRAYSIZE(buff),
TEXT("Position: %s\nGames played: %d\nGoals: %d"),
Roster[i].achPosition, Roster[i].nGamesPlayed,
Roster[i].nGoalsScored);
SetDlgItemText(hDlg, IDC_STATISTICS, buff);
return TRUE;
}
}
}
return TRUE;
}
}
return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
尽管我已经包含了我需要的所有内容,但我收到以下错误:
Error:identifer "IDC_LISTBOX_EXAMPLE" is undefined.
Run Code Online (Sandbox Code Playgroud)
为什么我会收到这样的错误?有人可以帮助我吗?
编辑
我没有使用 MFC 模板,我必须使用吗?
好吧,所以,除了对话过程之外,你什么都没有。好的。文件 -> 新建... -> 项目:
然后确定->下一步。您需要创建基于对话框的应用程序。为此,创建空应用程序:
并单击“完成”。
现在你有一个空的项目。您需要添加对话框资源。右键单击项目 -> 添加 -> 资源。然后对话框 -> 新建:
您将看到空对话框。右键单击它们 - >属性。记住 ID 的值(在我的例子中为 IDD_DIALOG1):
然后你需要添加列表框控件。转到工具箱窗口(视图 -> 工具箱,Ctrl+Alt+X),选择列表框并拖放到对话框中:
好的。然后您需要知道列表框的 ID(与对话框相同:右键单击列表框控件 -> 属性 -> ID)。在我的例子中是 IDC_LIST2。对于您的示例,您需要以相同的方式添加静态文本控件(与列表框相同的方式:工具箱 -> 静态文本 -> 拖放到对话框窗体)。为此静态控件提供另一个 ID(右键单击 -> 属性 -> Id -> 更改为 IDC_STATISTICS):

这就是您需要对对话框执行的所有操作:)
现在 - 代码。将新的 cpp 文件添加到项目中:
#include <Windows.h>
#include <tchar.h>
#include <strsafe.h>
#include "resource.h"
INT_PTR CALLBACK ListBoxExampleProc(HWND hDlg, UINT message,
WPARAM wParam, LPARAM lParam);
int CALLBACK _tWinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
::DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, ListBoxExampleProc);
return 0;
}
typedef struct
{
TCHAR achName[MAX_PATH];
TCHAR achPosition[12];
int nGamesPlayed;
int nGoalsScored;
} Player;
Player Roster[] =
{
{TEXT("Haas, Jonathan"), TEXT("Midfield"), 18, 4 },
{TEXT("Pai, Jyothi"), TEXT("Forward"), 36, 12 },
{TEXT("Hanif, Kerim"), TEXT("Back"), 26, 0 },
{TEXT("Anderberg, Michael"), TEXT("Back"), 24, 2 },
{TEXT("Jelitto, Jacek"), TEXT("Midfield"), 26, 3 },
{TEXT("Raposo, Rui"), TEXT("Back"), 24, 3},
{TEXT("Joseph, Brad"), TEXT("Forward"), 13, 3 },
{TEXT("Bouchard, Thomas"), TEXT("Forward"), 28, 5 },
{TEXT("Salmre, Ivo "), TEXT("Midfield"), 27, 7 },
{TEXT("Camp, David"), TEXT("Midfield"), 22, 3 },
{TEXT("Kohl, Franz"), TEXT("Goalkeeper"), 17, 0 },
};
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_LIST2);
for (int i = 0; i < ARRAYSIZE(Roster); i++)
{
int pos = (int)SendMessage(hwndList, LB_ADDSTRING, 0,
(LPARAM) Roster[i].achName);
// Set the array index of the player as item data.
// This enables us to retrieve the item from the array
// even after the items are sorted by the list box.
SendMessage(hwndList, LB_SETITEMDATA, pos, (LPARAM) i);
}
// 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_LIST2:
{
switch (HIWORD(wParam))
{
case LBN_SELCHANGE:
{
HWND hwndList = GetDlgItem(hDlg, IDC_LIST2);
// Get selected index.
int lbItem = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0);
// Get item data.
int i = (int)SendMessage(hwndList, LB_GETITEMDATA, lbItem, 0);
// Do something with the data from Roster[i]
TCHAR buff[MAX_PATH];
StringCbPrintf (buff, ARRAYSIZE(buff),
TEXT("Position: %s\nGames played: %d\nGoals: %d"),
Roster[i].achPosition, Roster[i].nGamesPlayed,
Roster[i].nGoalsScored);
SetDlgItemText(hDlg, IDC_STATISTICS, buff);
return TRUE;
}
}
}
return TRUE;
}
}
return FALSE;
}
Run Code Online (Sandbox Code Playgroud)
编译并运行!请记住 -切勿使用 WInAPI(或 MFC,无论如何)来编写 UI。使用Qt!
以及您应用程序的屏幕:

| 归档时间: |
|
| 查看次数: |
3004 次 |
| 最近记录: |