ListBox Dialog
A Win32 dialog application demonstrating ListBox message handling — inserting and deleting items via LB_INSERTSTRING and LB_DELETESTRING.
A compact working example of driving a ListBox control from a modal dialog. The Add button copies the text from an edit box into the list with LB_INSERTSTRING (position 0 = top), the Delete button removes whatever is currently selected by asking for LB_GETCURSEL first, and Ok closes the dialog. It also shows a few nice-to-haves: single-instance enforcement with OpenMutex, adding custom entries to the system menu in WM_INITDIALOG, and setting the window icon. Use it as a template whenever you need the basic add/remove/selected-item triad for a list control.
/////////////////////////////////////////////////////////////////////
#include <Windows.H>
#include <WindowsX.H>
#include <ShellAPI.H>
#include <Stdio.H>
#include <Stdlib.H>
#include "Headers/Definitions.H"
#include "Headers/Functions.H"
#include "Dialogs/MainDialog.H"
#include "Dialogs/AboutDialog.H"
/////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevious, LPSTR CmdLine, int CmdShow)
{
if(OpenMutex(SYNCHRONIZE, FALSE, TitleCaption))
{
MessageBox (NULL, "Only One Instance Can Be Run At A Time!" , TitleCaption, 0 + MB_ICONASTERISK + MB_SYSTEMMODAL);
return 0;
}
CreateMutex(NULL, FALSE, TitleCaption);
MainInstance = hInstance;
DialogBox(MainInstance,MAKEINTRESOURCE(100),0,MainDialog);
return 0;
}
The dialog procedure:
char Temp[1024];
BOOL CALLBACK MainDialog(HWND hDlg, UINT Msg, WPARAM wParam, LPARAM lParam)
{
static HWND InputText_hWnd;
static HWND ListBox;
static HMENU SystemMenu;
while(1) // Loop Forever
{
if(Msg==WM_INITDIALOG)
{
MainDialog_hWnd=hDlg;
SystemMenu = GetSystemMenu(MainDialog_hWnd,FALSE);
AppendMenu(SystemMenu,MF_SEPARATOR,0,0);
AppendMenu(SystemMenu,MF_STRING,1000,"www.NetWorkDLS.Com");
AppendMenu(SystemMenu,MF_STRING,1001,"Version");
SendMessage(MainDialog_hWnd,(UINT)WM_SETTEXT,(WPARAM)0,(LPARAM)TitleCaption);
SendMessage(MainDialog_hWnd,WM_SETICON,TRUE,(LPARAM)LoadIcon(GetWindowInstance(MainDialog_hWnd),MAKEINTRESOURCE(300)));
InputText_hWnd = GetDlgItem(MainDialog_hWnd, 1500);
ListBox = GetDlgItem(MainDialog_hWnd, 2000);
CenterWindow(MainDialog_hWnd);
break;
}
if(Msg==WM_COMMAND)
{
if(wParam==100) // Add Button
{
sprintf(Temp,"%s", GetText(InputText_hWnd));
SendMessage(ListBox,LB_INSERTSTRING,0,(LPARAM)(LPSTR)Temp);
SetText(InputText_hWnd,"");
}
if(wParam==101) // Delete Button
{
DWORD LineNumber;
LineNumber = SendMessage(ListBox,LB_GETCURSEL,0,0);
SendMessage(ListBox,LB_DELETESTRING,LineNumber,0);
}
if(wParam==102) // Ok Button
{
EndDialog(MainDialog_hWnd,0);
}
break;
}
if(Msg==WM_SYSCOMMAND)
{
if(LOWORD(wParam)==1000) //- NetWorkDLS.Com (SysMenu Command)
{
ShellExecute(0,"Open","Http://www.NetWorkDLS.com",NULL,NULL,SW_SHOWNORMAL);
}
if(LOWORD(wParam) == 1001) //- Version (SysMenu Command)
{
MessageBox(MainDialog_hWnd, FileVersion, TitleCaption, 0 + MB_ICONASTERISK + MB_TASKMODAL);
}
}
if(Msg==WM_CLOSE) //Exit (Windows Message)
{
KillTimer(MainDialog_hWnd,0);
EndDialog(MainDialog_hWnd,0);
}
if(Msg==WM_TIMER) //Timer (Windows Message)
{
}
break;
}
return 0;
}
One caution carried over from the original: LB_DELETESTRING with no selection passes LB_ERR (-1) as the index, which on some Windows versions deletes the last item rather than doing nothing - check the return of LB_GETCURSEL before deleting if that matters to you.