Practical
Learning: Programmatically Creating the Control
|
|
- Change the
Exercise.cpp source file as
follows:
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
CreateWindowEx(0,
L"LISTBOX",
TEXT("FamilyListing"),
WS_BORDER | WS_CHILD | WS_VISIBLE,
10,10,140,120,
hWndDlg,
NULL,
hInstGlobal,
NULL);
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
case WM_CLOSE:
PostQuitMessage(WM_QUIT);
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
- Execute the application to see the result
- Close the dialog box and return to your programming environment
Practical
Learning: Manually Creating a List Box
|
|
- Access the Resource.h file
- Add the following line at the end and press Enter:
#define IDC_PRIMARYFAMILY_LB 1014
- Access the Exercise.
rc file and change it as follows:
#include <afxres.h>
#include "Resource.h"
IDD_PRIMARYFAMILY_DLG DIALOG 260, 200, 110, 105
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Family Affairs"
FONT 8, "MS Shell Dlg"
BEGIN
DEFPUSHBUTTON "OK", IDOK, 48, 84, 50, 14
LISTBOX IDC_PRIMARYFAMILY_LB, 10, 10, 90, 80
END
- Access the Exercise.cpp
- Delete the code in the WM_INITDIALOG
event:
#include windows.h
#include "Resource.h"
//---------------------------------------------------------------------------
HWND hWnd;
HINSTANCE hInstGlobal;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
hInstGlobal = hInstance;
DialogBox(hInstGlobal, MAKEINTRESOURCE(IDD_PRIMARYFAMILY_DLG),
hWnd, reinterpret_cast(DlgProc));
return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
case WM_CLOSE:
PostQuitMessage(WM_QUIT);
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
- Execute the application to see the result
- Close the dialog box and return to your programming environment
Practical
Learning: Adding Items to the List Box
|
|
- Change the WN the Exercise.rc file and change it as follows (it also
shows different ways to add an item):
#include <windows.h>
#include <Windowsx.h>
#include "Resource.h"
//---------------------------------------------------------------------------
HWND hWnd;
HINSTANCE hInstGlobal;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
hInstGlobal = hInstance;
DialogBox(hInstGlobal, MAKEINTRESOURCE(IDD_PRIMARYFAMILY_DLG),
hWnd, reinterpret_cast<DLGPROC>(DlgProc));
return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND lbxPrimaryFamily;
lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
switch(msg)
{
case WM_INITDIALOG:
ListBox_AddString(lbxPrimaryFamily, L"Father");
ListBox_AddString(lbxPrimaryFamily, L"Mother");
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)L"Daughter");
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)TEXT("Son"));
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)TEXT("Bother"));
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)TEXT("Sister"));
return TRUE;
case WM_COMMAND:
switch(wParam)
{
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
case WM_CLOSE:
PostQuitMessage(WM_QUIT);
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
- Execute the application to see the result
- Close the dialog box and return to your programming environment
Practical
Learning: Handling Events
|
|
- Access the Exercise.
rc file
-
To prepare to handle events, change the file as follows:
#include <afxres.h>
#include "Resource.h"
IDD_PRIMARYFAMILY_DLG DIALOG 260, 200, 110, 105
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Family Affairs"
FONT 8, "MS Shell Dlg"
BEGIN
DEFPUSHBUTTON "OK", IDOK, 48, 84, 50, 14
LISTBOX IDC_PRIMARYFAMILY_LB, 10, 10, 90, 80, LBS_NOTIFY
END
- Access the Exercise.cpp
- To handle events, change the procedure as follows:
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND lbxPrimaryFamily;
lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
switch(msg)
{
case WM_INITDIALOG:
ListBox_AddString(lbxPrimaryFamily, L"Father");
ListBox_AddString(lbxPrimaryFamily, L"Mother");
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)L"Daughter");
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Son"));
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Bother"));
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Sister"));
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_PRIMARYFAMILY_LB:
{
switch (HIWORD(wParam))
return TRUE;
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
case WM_CLOSE:
PostQuitMessage(WM_QUIT);
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
Practical
Learning: Double-Clicking an Item
|
|
- When the user double-clicks an item, to get the index of the item that was
double-clicked, type the following:
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND lbxPrimaryFamily;
lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
switch(msg)
{
case WM_INITDIALOG:
ListBox_AddString(lbxPrimaryFamily, L"Father");
ListBox_AddString(lbxPrimaryFamily, L"Mother");
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)L"Daughter");
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Son"));
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Bother"));
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Sister"));
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_PRIMARYFAMILY_LB:
{
switch (HIWORD(wParam))
{
case LBN_DBLCLK:
int iSelectedIndex;
iSelectedIndex = ListBox_GetCurSel(lbxPrimaryFamily);
return TRUE;
}
}
return TRUE;
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
case WM_CLOSE:
PostQuitMessage(WM_QUIT);
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
- To get the text of the item that was double-clicked, change the
code as follows:
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND lbxPrimaryFamily;
lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
switch(msg)
{
case WM_INITDIALOG:
ListBox_AddString(lbxPrimaryFamily, L"Father");
ListBox_AddString(lbxPrimaryFamily, L"Mother");
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)L"Daughter");
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Son"));
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Bother"));
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Sister"));
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_PRIMARYFAMILY_LB:
{
switch (HIWORD(wParam))
{
case LBN_DBLCLK:
int iSelectedIndex;
TCHAR strSelectedItem[32];
iSelectedIndex = ListBox_GetCurSel(lbxPrimaryFamily);
ListBox_GetText(lbxPrimaryFamily, iSelectedIndex, (LPCTSTR)strSelectedItem);
MessageBox(NULL, strSelectedItem, L"Exercise", 0);
return TRUE;
}
}
return TRUE;
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
case WM_CLOSE:
PostQuitMessage(WM_QUIT);
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
- Execute the application to see the result
- Close the dialog box and return to your programming environment
Practical
Learning: Deleting an Item
|
|
- When the user double-clicks an item, to delete that item, type the following:
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND lbxPrimaryFamily;
lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
switch(msg)
{
case WM_INITDIALOG:
ListBox_AddString(lbxPrimaryFamily, L"Father");
ListBox_AddString(lbxPrimaryFamily, L"Mother");
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)L"Daughter");
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Son"));
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Bother"));
SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
(LPARAM)(LPCTSTR)TEXT("Sister"));
return TRUE;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_PRIMARYFAMILY_LB:
{
switch (HIWORD(wParam))
{
case LBN_DBLCLK:
// Getting the index of the item that was double-clicked
int iSelectedIndex;
iSelectedIndex = ListBox_GetCurSel(lbxPrimaryFamily);
// Identifying the string that was double-clicked
/*
TCHAR strSelectedItem[32];
ListBox_GetText(lbxPrimaryFamily,
iSelectedIndex,
(LPCTSTR)strSelectedItem);
MessageBox(NULL, strSelectedItem, L"Exercise", 0);*/
// Deleting the item that was double-clicked
ListBox_DeleteString(lbxPrimaryFamily, iSelectedIndex);
return TRUE;
}
}
return TRUE;
case IDOK:
EndDialog(hWndDlg, 0);
return TRUE;
}
break;
case WM_CLOSE:
PostQuitMessage(WM_QUIT);
break;
}
return FALSE;
}
//---------------------------------------------------------------------------
- Execute the application to see the result
- Double-click an item in the list box
- Close the dialog box and return to your programming environment
|
|