exemple de code :
ListBox.cpp :
//************************************************// ListBox.cpp :
//
//************************************************
#include <windows.h>
#include "Resource.h"
HINSTANCE g_hAppInstance= NULL;
HWND g_hMainDlg = NULL;
static HWND s_hListBox= NULL;
static HWND s_hEdit= NULL;
static HWND s_hBtnAdd= NULL;
static HWND s_hBtnModify= NULL;
static HWND s_hBtnDelete= NULL;
static int s_nMaxWidth= 0;
static int GetMaxTextWidth();
static int CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
static BOOL OnInitDialog(HWND, WPARAM, LPARAM);
static void OnCommand(WPARAM, LPARAM);
static void OnBtnAdd ();
static void OnBtnModify();
static void OnBtnDelete();
//************************************************// WinMain :
//************************************************int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdine, int nCmdShow)
{
// ouverture boîte de dialogue principale.
g_hAppInstance = hInstance;
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAINDIALOG), NULL, DlgProc);
}
//************************************************// GetMaxTextWidth : recherche la largeur maximale d'une ligne de texte dans une ListBox.
// entrée : hLst : handle de la ListBox.
// retour : largeur maximale d'une ligne de texte (en pixel).
//************************************************int GetMaxTextWidth(HWND hLst)
{
// DC de la ListBox, nombre d'élément
HDC hdc = GetDC(hLst);
int nCount = SendMessage(hLst, LB_GETCOUNT, 0, 0);
// recherche du texte le plus large
int nMaxWidth = 0;
for(int i = 0; i < nCount; i++)
{
// texte de l'item
char szText[256];
SendMessage(hLst, LB_GETTEXT, i, (LPARAM)szText);
// largeur du texte
SIZE size;
GetTextExtentPoint32(hdc, szText, strlen(szText), &size);
if(size.cx > nMaxWidth) nMaxWidth = size.cx;
}
// retour
return nMaxWidth;
}
//************************************************// DlgProc :
//************************************************int CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
// en fonction du message.
switch(uMsg)
{
case WM_INITDIALOG : return OnInitDialog(hDlg, wParam, lParam);
case WM_COMMAND: OnCommand(wParam, lParam); break;
}
return 0;
}
//************************************************// OnInitDialog :
//************************************************BOOL OnInitDialog(HWND hDlg, WPARAM wParam, LPARAM lParam)
{
// affectation handles
g_hMainDlg= hDlg;
s_hListBox = GetDlgItem(hDlg, IDC_LISTBOX);
s_hEdit = GetDlgItem(hDlg, IDC_EDIT);
s_hBtnAdd = GetDlgItem(hDlg, IDC_BTN_ADD);
s_hBtnModify= GetDlgItem(hDlg, IDC_BTN_MODIFY);
s_hBtnDelete= GetDlgItem(hDlg, IDC_BTN_DELETE);
// initialisation paramètres du scrollbar de la listbox
s_nMaxWidth = 0;
SendMessage(s_hListBox, LB_SETHORIZONTALEXTENT, s_nMaxWidth, 0);
return TRUE;
}
//************************************************// OnCommand :
//************************************************void OnCommand(WPARAM wParam, LPARAM lParam)
{
// en fonction de l'Id du contrôle
int nIDCtl = LOWORD(wParam);
switch(nIDCtl)
{
case IDC_BTN_ADD : OnBtnAdd();break;
case IDC_BTN_MODIFY : OnBtnModify();break;
case IDC_BTN_DELETE : OnBtnDelete();break;
case IDCANCEL: EndDialog(g_hMainDlg, IDCANCEL);break;
}
}
//************************************************// OnBtnAdd :
//************************************************void OnBtnAdd()
{
// ajout d'un item
char szText[256];
GetWindowText(s_hEdit, szText, 256);
SendMessage(s_hListBox, LB_ADDSTRING, 0, (LPARAM)szText);
// taille du texte
SIZE size;
HDC hdc = GetDC(s_hListBox);
GetTextExtentPoint32(hdc, szText, strlen(szText), &size);
ReleaseDC(s_hListBox, hdc);
// si ce texte est plus grand que l'actuel max, mettre à jour le ScrollBar
if(size.cx > s_nMaxWidth)
{
s_nMaxWidth = size.cx;
SendMessage(s_hListBox, LB_SETHORIZONTALEXTENT, s_nMaxWidth, 0);
}
}
//************************************************// OnBtnModify :
//************************************************void OnBtnModify()
{
// modification del'item sélectionné
char szText[256];
GetWindowText(s_hEdit, szText, 256);
int index = SendMessage(s_hListBox, LB_GETCURSEL, 0, 0);
SendMessage(s_hListBox, LB_DELETESTRING, index, 0);
SendMessage(s_hListBox, LB_INSERTSTRING, index, (LPARAM) szText);
// taille du texte
SIZE size;
HDC hdc = GetDC(s_hListBox);
GetTextExtentPoint32(hdc, szText, strlen(szText), &size);
ReleaseDC(s_hListBox, hdc);
// mise à jour du ScrollBar
if(size.cx > s_nMaxWidth)
s_nMaxWidth = size.cx;
else
s_nMaxWidth = GetMaxTextWidth(s_hListBox);
SendMessage(s_hListBox, LB_SETHORIZONTALEXTENT, s_nMaxWidth, 0);
}
//************************************************// OnBtnDelete :
//************************************************void OnBtnDelete()
{
// supression de l'item sélectionné
int index = SendMessage(s_hListBox, LB_GETCURSEL, 0, 0);
SendMessage(s_hListBox, LB_DELETESTRING, index, 0);
// mise à jour du ScrollBar
s_nMaxWidth = GetMaxTextWidth(s_hListBox);
SendMessage(s_hListBox, LB_SETHORIZONTALEXTENT, s_nMaxWidth, 0);
}
"Resource.h"
//{{NO_DEPENDENCIES}}
// Microsoft Developer Studio generated include file.
// Used by ListBox.rc
//
#define IDD_MAINDIALOG 101
#define IDC_LISTBOX 1000
#define IDC_EDIT 1001
#define IDC_BTN_ADD 1002
#define IDC_BTN_MODIFY 1003
#define IDC_BTN_DELETE 1004
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 102
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1005
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
"ListBox.rc"
//Microsoft Developer Studio generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "afxres.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// French (France) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA)
#ifdef _WIN32
LANGUAGE LANG_FRENCH, SUBLANG_FRENCH
#pragma code_page(1252)
#endif //_WIN32
#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//
1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END
2 TEXTINCLUDE DISCARDABLE
BEGIN
"#include ""afxres.h""\r\n"
"\0"
END
3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END
#endif // APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//
IDD_MAINDIALOG DIALOG DISCARDABLE 0, 0, 280, 170
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Dialog"
FONT 8, "MS Sans Serif"
BEGIN
LISTBOX IDC_LISTBOX,10,10,110,150,LBS_NOINTEGRALHEIGHT |
LBS_DISABLENOSCROLL | WS_VSCROLL | WS_HSCROLL |
WS_TABSTOP
EDITTEXT IDC_EDIT,130,10,140,14,ES_AUTOHSCROLL
DEFPUSHBUTTON "Ajouter",IDC_BTN_ADD,130,40,50,14
PUSHBUTTON "Modifier",IDC_BTN_MODIFY,130,60,50,14
PUSHBUTTON "Supprimer",IDC_BTN_DELETE,130,80,50,14
END
/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//
#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO DISCARDABLE
BEGIN
IDD_MAINDIALOG, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 273
TOPMARGIN, 7
BOTTOMMARGIN, 163
END
END
#endif // APSTUDIO_INVOKED
#endif // French (France) resources
/////////////////////////////////////////////////////////////////////////////
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//
/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED