一ComboBox函数简单介绍
1SendMessage函数向窗口发送消息
LRESULTSendMessage(
HWNDhWnd,//handletodestinationwindow
UINTMsg,//message
WPARAMwParam,//firstmessageparameter
LPARAMlParam//secondmessageparameter
);
2向ComboBox添加数据
HWNDhWndComboBox=GetDlgItem(hWnd,IDC_COMBO1);
TCHARszMessage[20]="Hello";
SendMessage(hWndComboBox,CB_ADDRSTRING,0,(LPARAM)szMessage);
3向ComboBox插入数据
HWNDhWndComboBox=GetDlgItem(hWnd,IDC_COMBO1);
TCHARszMessage[20]="World";
SendMessage(hWndComboBox,CB_INSERTRSTRING,0,(LPARAM)szMessage);
4向ComboBox删除数据
SendMessage(hWndComboBox,CB_DELETESTRING,1,0);//删除第二项数据
5清除ComboBox所有数据
SendMessage(hWndComboBox,CB_RESETCONTENT,0,0);
6获取ComboBox数据项目的数量
UINTuCount;
uCount=SendMessage(hWndComboBox,CB_GETCOUNT,0,0):
7获取ComboBox某项的值
TCHARszMessage[200];
ZeroMessage(szMessage,sizeof(szMessage)):
SendMessage(hWndComboBox,CB_GETLBTEXT,1,(LPARAM)szMessage);//获取第二项的数据
MessageBox(NULL,szMessage," ",MB_OK);
二ComboBox简单使用
1界面设计如下图
2功能实现代码(建的是简单的Win32工程)
//ComboBox.cpp
#include "stdafx.h"
#include "resource.h"
LRESULT CALLBACK Dialog(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
DialogBox(hInstance, (LPCTSTR)IDD_DIALOG1, NULL, (DLGPROC)Dialog);
return 0;
}
LRESULT CALLBACK Dialog(HWND hWnd, UINT uMessage, WPARAM wParam, LPARAM lParam)
{
switch(uMessage)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
UINT uSender;
uSender = LOWORD(wParam);
HWND hWndComboBox;
hWndComboBox = GetDlgItem(hWnd, IDC_COMBO1);
TCHAR szBuff[200];
ZeroMemory(szBuff, sizeof(szBuff));
switch(uSender)
{
//CB_ADDSTRING是在最后添加数据
case IDC_BUTTON1:
GetDlgItemText(hWnd, IDC_EDIT1, szBuff, sizeof(szBuff));
SendMessage(hWndComboBox, CB_ADDSTRING, 0, (LPARAM)szBuff);
break;
//CB_ADDSTRING是在指定位置添加数据
case IDC_BUTTON2:
GetDlgItemText(hWnd, IDC_EDIT1, szBuff, sizeof(szBuff));
SendMessage(hWndComboBox, CB_INSERTSTRING, 0, (LPARAM)szBuff);
break;
case IDC_BUTTON3:
SendMessage(hWndComboBox, CB_RESETCONTENT, 0, 0);
break;
case IDC_BUTTON4:
UINT uCount;
uCount = SendMessage(hWndComboBox, CB_GETCOUNT, 0, 0);
SetDlgItemInt(hWnd, IDC_EDIT2, uCount, TRUE);
break;
case IDC_BUTTON5:
UINT uSelect;
uSelect = GetDlgItemInt(hWnd, IDC_EDIT2, NULL, TRUE);
SendMessage(hWndComboBox, CB_GETLBTEXT, uSelect, (LPARAM)szBuff);
MessageBox(hWnd, szBuff, "SHOW", MB_OK|MB_ICONINFORMATION);
break;
case IDOK:
EndDialog(hWnd, lParam);
break;
}
break;
case WM_CLOSE:
EndDialog(hWnd, lParam);
break;
}
return FALSE;
}