VC++中listbox问题

在VC++中有,hwndList=CreateWindow(TEXT("listbox"),NULL,WS_CHILDWINDOW|WS_VISIBLE|LBS_STAN... 在VC++中有,
hwndList = CreateWindow (TEXT ("listbox"), NULL,
WS_CHILDWINDOW | WS_VISIBLE | LBS_STANDARD,
cxChar, cyChar * 3,
cxChar * 13 + GetSystemMetrics (SM_CXVSCROLL),
cyChar * 10,
hwnd, (HMENU) ID_LIST,
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;
请问该怎么向listbox中添加字符串,还有怎么取出字符串,最好给出listbox的详细使用说明.谢谢.
我不想用MFC,能不能给出API函数的方法,就是用SendMessage()来做,谢谢
展开
 我来答
silvergingko
推荐于2016-02-01 · TA获得超过5945个赞
知道小有建树答主
回答量:688
采纳率:0%
帮助的人:869万
展开全部
不想用MFC的话,但用SendMessage会很麻烦的,因为消息太多,不便记忆。

楼主可以看下这个标准头文件<windowsx.h>,在这个头文件中,已经为各种控件预定义了各种宏,这些宏的取名是精心设计的,方便记忆。楼主在需要的地方,可以以类似调用MFC控件类成员函数的方式来调用这些宏,这些宏最终会转换成SendMessage。<windowsx.h>是个纯win32头文件。
譬如需要添加字符串,代码为:
/*-------------------------------------------------
#include <windowsx.h>
ListBox_AddString(hwndList, _T("新加的字符串");
-------------------------------------------------*/
譬如需要取出第三行(索引为2)中的文本,代码为:
/*-------------------------------------------------
LPTSTR pText = NULL;
INT len = 0;
len = ListBox_GetTextLen(hwndList, 2);
if (len > 0)
{
pText = (LPTSTR)new TCHAR[len + 1];
ListBox_GetText(hwndList, 2, pText);
}
-------------------------------------------------*/
不过里面没有通用控件的宏,只有标准控件的宏。
在这里贴上List Box控件的消息宏:

/****** ListBox control message APIs *****************************************/

#define ListBox_Enable(hwndCtl, fEnable) EnableWindow((hwndCtl), (fEnable))

#define ListBox_GetCount(hwndCtl) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETCOUNT, 0L, 0L))
#define ListBox_ResetContent(hwndCtl) ((BOOL)(DWORD)SNDMSG((hwndCtl), LB_RESETCONTENT, 0L, 0L))

#define ListBox_AddString(hwndCtl, lpsz) ((int)(DWORD)SNDMSG((hwndCtl), LB_ADDSTRING, 0L, (LPARAM)(LPCTSTR)(lpsz)))
#define ListBox_InsertString(hwndCtl, index, lpsz) ((int)(DWORD)SNDMSG((hwndCtl), LB_INSERTSTRING, (WPARAM)(int)(index), (LPARAM)(LPCTSTR)(lpsz)))

#define ListBox_AddItemData(hwndCtl, data) ((int)(DWORD)SNDMSG((hwndCtl), LB_ADDSTRING, 0L, (LPARAM)(data)))
#define ListBox_InsertItemData(hwndCtl, index, data) ((int)(DWORD)SNDMSG((hwndCtl), LB_INSERTSTRING, (WPARAM)(int)(index), (LPARAM)(data)))

#define ListBox_DeleteString(hwndCtl, index) ((int)(DWORD)SNDMSG((hwndCtl), LB_DELETESTRING, (WPARAM)(int)(index), 0L))

#define ListBox_GetTextLen(hwndCtl, index) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETTEXTLEN, (WPARAM)(int)(index), 0L))
#define ListBox_GetText(hwndCtl, index, lpszBuffer) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETTEXT, (WPARAM)(int)(index), (LPARAM)(LPCTSTR)(lpszBuffer)))

#define ListBox_GetItemData(hwndCtl, index) ((LRESULT)(ULONG_PTR)SNDMSG((hwndCtl), LB_GETITEMDATA, (WPARAM)(int)(index), 0L))
#define ListBox_SetItemData(hwndCtl, index, data) ((int)(DWORD)SNDMSG((hwndCtl), LB_SETITEMDATA, (WPARAM)(int)(index), (LPARAM)(data)))

#if (WINVER >= 0x030a)
#define ListBox_FindString(hwndCtl, indexStart, lpszFind) ((int)(DWORD)SNDMSG((hwndCtl), LB_FINDSTRING, (WPARAM)(int)(indexStart), (LPARAM)(LPCTSTR)(lpszFind)))
#define ListBox_FindItemData(hwndCtl, indexStart, data) ((int)(DWORD)SNDMSG((hwndCtl), LB_FINDSTRING, (WPARAM)(int)(indexStart), (LPARAM)(data)))

#define ListBox_SetSel(hwndCtl, fSelect, index) ((int)(DWORD)SNDMSG((hwndCtl), LB_SETSEL, (WPARAM)(BOOL)(fSelect), (LPARAM)(index)))
#define ListBox_SelItemRange(hwndCtl, fSelect, first, last) ((int)(DWORD)SNDMSG((hwndCtl), LB_SELITEMRANGE, (WPARAM)(BOOL)(fSelect), MAKELPARAM((first), (last))))

#define ListBox_GetCurSel(hwndCtl) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETCURSEL, 0L, 0L))
#define ListBox_SetCurSel(hwndCtl, index) ((int)(DWORD)SNDMSG((hwndCtl), LB_SETCURSEL, (WPARAM)(int)(index), 0L))

#define ListBox_SelectString(hwndCtl, indexStart, lpszFind) ((int)(DWORD)SNDMSG((hwndCtl), LB_SELECTSTRING, (WPARAM)(int)(indexStart), (LPARAM)(LPCTSTR)(lpszFind)))
#define ListBox_SelectItemData(hwndCtl, indexStart, data) ((int)(DWORD)SNDMSG((hwndCtl), LB_SELECTSTRING, (WPARAM)(int)(indexStart), (LPARAM)(data)))

#define ListBox_GetSel(hwndCtl, index) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETSEL, (WPARAM)(int)(index), 0L))
#define ListBox_GetSelCount(hwndCtl) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETSELCOUNT, 0L, 0L))
#define ListBox_GetTopIndex(hwndCtl) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETTOPINDEX, 0L, 0L))
#define ListBox_GetSelItems(hwndCtl, cItems, lpItems) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETSELITEMS, (WPARAM)(int)(cItems), (LPARAM)(int *)(lpItems)))

#define ListBox_SetTopIndex(hwndCtl, indexTop) ((int)(DWORD)SNDMSG((hwndCtl), LB_SETTOPINDEX, (WPARAM)(int)(indexTop), 0L))

#define ListBox_SetColumnWidth(hwndCtl, cxColumn) ((void)SNDMSG((hwndCtl), LB_SETCOLUMNWIDTH, (WPARAM)(int)(cxColumn), 0L))
#define ListBox_GetHorizontalExtent(hwndCtl) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETHORIZONTALEXTENT, 0L, 0L))
#define ListBox_SetHorizontalExtent(hwndCtl, cxExtent) ((void)SNDMSG((hwndCtl), LB_SETHORIZONTALEXTENT, (WPARAM)(int)(cxExtent), 0L))

#define ListBox_SetTabStops(hwndCtl, cTabs, lpTabs) ((BOOL)(DWORD)SNDMSG((hwndCtl), LB_SETTABSTOPS, (WPARAM)(int)(cTabs), (LPARAM)(int *)(lpTabs)))

#define ListBox_GetItemRect(hwndCtl, index, lprc) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETITEMRECT, (WPARAM)(int)(index), (LPARAM)(RECT *)(lprc)))

#define ListBox_SetCaretIndex(hwndCtl, index) ((int)(DWORD)SNDMSG((hwndCtl), LB_SETCARETINDEX, (WPARAM)(int)(index), 0L))
#define ListBox_GetCaretIndex(hwndCtl) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETCARETINDEX, 0L, 0L))

#define ListBox_FindStringExact(hwndCtl, indexStart, lpszFind) ((int)(DWORD)SNDMSG((hwndCtl), LB_FINDSTRINGEXACT, (WPARAM)(int)(indexStart), (LPARAM)(LPCTSTR)(lpszFind)))

#define ListBox_SetItemHeight(hwndCtl, index, cy) ((int)(DWORD)SNDMSG((hwndCtl), LB_SETITEMHEIGHT, (WPARAM)(int)(index), MAKELPARAM((cy), 0)))
#define ListBox_GetItemHeight(hwndCtl, index) ((int)(DWORD)SNDMSG((hwndCtl), LB_GETITEMHEIGHT, (WPARAM)(int)(index), 0L))
#endif /* WINVER >= 0x030a */

#define ListBox_Dir(hwndCtl, attrs, lpszFileSpec) ((int)(DWORD)SNDMSG((hwndCtl), LB_DIR, (WPARAM)(UINT)(attrs), (LPARAM)(LPCTSTR)(lpszFileSpec)))
nullai
2010-04-16 · TA获得超过117个赞
知道小有建树答主
回答量:134
采纳率:0%
帮助的人:215万
展开全部
//设置字符串SendMessage(hwndList ,LB_ADDSTRING,0,(LPARAM)TEXT("字符串"));

//获取字符串,index为位置
TCHAR pText[MAX_PATH];
SendMessage(hwndList ,LB_GETTEXT,index,(long)pText);

//ListBox消息
LB_ADDFILE
LB_ADDSTRING
LB_DELETESTRING
LB_DIR
LB_FINDSTRING
LB_FINDSTRINGEXACT
LB_GETANCHORINDEX
LB_GETCARETINDEX
LB_GETCOUNT
LB_GETCURSEL
LB_GETHORIZONTALEXTENT
LB_GETITEMDATA
LB_GETITEMHEIGHT
LB_GETITEMRECT
LB_GETLISTBOXINFO
LB_GETLOCALE
LB_GETSEL
LB_GETSELCOUNT
LB_GETSELITEMS
LB_GETTEXT
LB_GETTEXTLEN
LB_GETTOPINDEX
LB_INITSTORAGE
LB_INSERTSTRING
LB_ITEMFROMPOINT
LB_RESETCONTENT
LB_SELECTSTRING
LB_SELITEMRANGE
LB_SELITEMRANGEEX
LB_SETANCHORINDEX
LB_SETCARETINDEX
LB_SETCOLUMNWIDTH
LB_SETCOUNT
LB_SETCURSEL
LB_SETHORIZONTALEXTENT
LB_SETITEMDATA
LB_SETITEMHEIGHT
LB_SETLOCALE
LB_SETSEL
LB_SETTABSTOPS
LB_SETTOPINDEX
LBN_DBLCLK
LBN_ERRSPACE
LBN_KILLFOCUS
LBN_SELCANCEL
LBN_SELCHANGE
LBN_SETFOCUS
WM_CHARTOITEM
WM_CTLCOLORLISTBOX
WM_DELETEITEM
WM_VKEYTOITEM

//详细使用请查MSDN
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
yvhpfiipfi
2010-04-16 · TA获得超过2912个赞
知道小有建树答主
回答量:880
采纳率:50%
帮助的人:692万
展开全部
楼上朋友给出的怎么是.net的,好像是C#的。楼主问的是在C++当中的。

如果在C++中,用SendMessage()来做,我没试过。不过,ListBox控件是MFC程序的标准控件,脱离了MFC,我真还不知道怎么做。

我这有两段代码,分别放在两个按钮事件代码中,作用是遍历当前进程,关闭所选进程。要向窗体上放两个按钮, 和一个listbox。

void CMy5Dlg::OnButton1()
{
// TODO: Add your control notification handler code here
HANDLE snap;
snap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
PROCESSENTRY32 processlist;
processlist.dwSize=sizeof(PROCESSENTRY32);
bool next;
next=Process32Next(snap,&processlist);
int index=0;
CListBox *list;
list=(CListBox*)GetDlgItem(IDC_LIST1);
list->ResetContent();
CString str;
while(next)
{

processId[index]=processlist.th32ProcessID;
str.Format("%d",processId[index]);
str+=":";
str+=processlist.szExeFile;
list->InsertString(index,str);

next=Process32Next(snap,&processlist);
index++;
}
}

void CMy5Dlg::OnButton2()
{
// TODO: Add your control notification handler code here

CListBox* list;
list=(CListBox*)GetDlgItem(IDC_LIST1);
int select=list->GetCurSel();

if(select!=LB_ERR)
{

CString file;
list->GetText(select,file);
file.Insert(0,"终止这个进程吗?\n");
if(AfxMessageBox(file,MB_OKCANCEL,0)==IDOK)
{

HANDLE hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, processId[select] );

if(hProcess)
{
TerminateProcess(hProcess,0);
AfxMessageBox("进程已终止!");
OnOK();
}
}
}
}

其中,list->InsertString(index,str);
这一句,就是向listbox里添加字符串,而list->GetText(select,file);这一句就是取出字符串。

SendMessage()的方法我来试一下。

希望对你有所帮助,谢谢!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友6a144c333
2010-04-15 · 超过14用户采纳过TA的回答
知道答主
回答量:31
采纳率:0%
帮助的人:0
展开全部
参考资料

参考资料: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.listbox(VS.80).aspx

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式