<Win32_1> Listbox 1 —— Add Item & Delete Item
先看下程序的运行效果:

程序功能:选中左边ListBox某项后,点击按钮">>"将该项添加到右边的ListBox,并将其从左边ListBox中删除。按钮“<<”完成相似的功能。
下面是程序的源代码,在 WM_CREATE 中创建了2个按钮和2个Listbox 子控件,在 WM_SIZE 中改变子控件的大小和位置, 在 WM_COMMAND中处理来自子控件的消息,代码不长,自己慢慢看吧 -_-
#include <windows.h>#include <stdlib.h>#include <tchar.h>LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);#define LEFT_LISTBOX1#define RIGHT_LISTBOX2#define LEFT_BUTTON3#define RIGHT_BUTTON4int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, PSTR szCmdLine, int iCmdShow){WNDCLASS wndClass;HWND hWnd;MSG msg;TCHAR szClassName[] = TEXT("MyWindow");TCHAR szTitle[] = TEXT("Win32 ListBox"); wndClass.style = CS_HREDRAW | CS_VREDRAW ; wndClass.lpfnWndProc = WindowProc ; wndClass.cbClsExtra = 0 ; wndClass.cbWndExtra = 0 ; wndClass.hInstance = hInstance ; wndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndClass.hCursor = LoadCursor (NULL, IDC_ARROW) ; wndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndClass.lpszMenuName = NULL ; wndClass.lpszClassName = szClassName ; if(!RegisterClass(&wndClass)) {MessageBox(NULL, "RegisterClass fail", "message", 0);return 0; } hWnd = CreateWindow(szClassName, szTitle, WS_OVERLAPPEDWINDOW , CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, NULL, NULL, hInstance, NULL); ShowWindow(hWnd, iCmdShow); UpdateWindow(hWnd); while(GetMessage(&msg, NULL, 0, 0)) {TranslateMessage(&msg);DispatchMessage(&msg); } return msg.wParam;}// 获取ListBox控件中指定项的文本,并返回文本长度int GetListBoxItemText(HWND hWndList, int nIndex, LPTSTR *pszItemText){int nItemTextLen;LPTSTR szItemText = *pszItemText;nItemTextLen = SendMessage(hWndList, LB_GETTEXTLEN, (WPARAM)nIndex, 0);// 获取指定item文本长度szItemText = (TCHAR *)realloc(szItemText, sizeof(TCHAR *) * nItemTextLen);*pszItemText = szItemText;return SendMessage(hWndList, LB_GETTEXT, (WPARAM)nIndex, (LPARAM)szItemText);// 获取指定item文本}LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){HDC hDC ; PAINTSTRUCT ps ; RECT rect ;TEXTMETRIC tm;static intcxChar, cyChar,nLbxWidth, nLbxHeigth,nBtnWidth, nBtnHeight;static HWNDhWndListLeft, hWndListRight,hWndBtnLeft, hWndBtnRight;static int nIndex, nItemTextLen;static TCHAR *szItemText = NULL;int k, ctrlID ;TCHAR szBuf[10];switch(uMsg){case WM_CREATE: // 获取字体大小 /* cxChar = LOWORD(GetDialogBaseUnits()); cyChar = HIWORD(GetDialogBaseUnits()); */ hDC = GetDC(hWnd); GetTextMetrics(hDC, &tm); cxChar = tm.tmAveCharWidth; cyChar = tm.tmHeight; ReleaseDC(hWnd, hDC); // listbox 长宽 nLbxWidth = 10*cxChar; nLbxHeigth = 10*cyChar; nBtnWidth = 10*cxChar; nBtnHeight = 7*cyChar/4; // 创建子控件 hWndListLeft = CreateWindow(TEXT("listbox"), TEXT("Left listbox"), // Left ListBox LBS_NOTIFY | WS_BORDER | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE | WS_VSCROLL, 0, 0, 0, 0, hWnd, (HMENU)LEFT_LISTBOX, ((LPCREATESTRUCT)lParam)->hInstance, NULL); hWndListRight = CreateWindow(TEXT("listbox"), TEXT("Right listbox"),// Right ListBox LBS_NOTIFY | WS_BORDER | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE | WS_VSCROLL, 0, 0, 0, 0, hWnd, (HMENU)RIGHT_LISTBOX, ((LPCREATESTRUCT)lParam)->hInstance, NULL); hWndBtnLeft = CreateWindow(TEXT("button"), TEXT("<<"),// left button WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 0, 0, 0, 0, hWnd, (HMENU)LEFT_BUTTON, ((LPCREATESTRUCT)lParam)->hInstance, NULL); hWndBtnRight = CreateWindow(TEXT("button"), TEXT(">>"),// right button WS_CHILD | WS_VISIBLE |BS_DEFPUSHBUTTON, 0, 0, 0, 0, hWnd, (HMENU)RIGHT_BUTTON, ((LPCREATESTRUCT)lParam)->hInstance, NULL); //初始化left listbox for (k = 1; k < 20; ++k) { _itot(k*100, szBuf, 10); SendMessage(hWndListLeft, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)szBuf); } SendMessage(hWndListLeft, LB_SETCURSEL, (WPARAM)0, 0);// 设置默认选中项 GetListBoxItemText(hWndListLeft, 0, &szItemText); break; case WM_SIZE: // 调整子控件位置 GetClientRect(hWnd, &rect); MoveWindow(hWndListLeft, (rect.right - rect.left)/2 - nLbxWidth*2,// Left ListBox (rect.bottom - rect.top)/2 - nLbxHeigth/2, nLbxWidth, nLbxHeigth, TRUE); MoveWindow(hWndListRight, (rect.right - rect.left)/2 + nLbxWidth,// Right ListBox (rect.bottom - rect.top)/2 - nLbxHeigth/2, nLbxWidth, nLbxHeigth, TRUE); MoveWindow(hWndBtnRight, (rect.right - rect.left)/2 - nBtnWidth/2,// right button (rect.bottom - rect.top)/2 - nBtnHeight, nBtnWidth, nBtnHeight, TRUE); MoveWindow(hWndBtnLeft, (rect.right - rect.left)/2 - nBtnWidth/2,// left button (rect.bottom - rect.top)/2 , nBtnWidth, nBtnHeight, TRUE); break; case WM_PAINT: hDC = BeginPaint (hWnd, &ps) ; EndPaint (hWnd, &ps) ; break; case WM_COMMAND: ctrlID = LOWORD(wParam);// 提取控件ID switch (ctrlID) { case LEFT_LISTBOX: if (HIWORD(wParam) == LBN_SELCHANGE)// 选项改变 { nIndex = SendMessage(hWndListLeft, LB_GETCURSEL, 0, 0);// 获取当前选中项 GetListBoxItemText(hWndListLeft, nIndex, &szItemText); } break; case RIGHT_LISTBOX: break; case RIGHT_BUTTON: // right button nIndex = SendMessage(hWndListLeft, LB_GETCURSEL, 0, 0);// 获取左边ListBox选中项索引 if (nIndex == LB_ERR) { MessageBox(hWnd, TEXT("No Selected"), TEXT("Message"), MB_OK); break; } GetListBoxItemText(hWndListLeft, nIndex, &szItemText);// 获取左边ListBox选中项文本 SendMessage(hWndListRight, LB_ADDSTRING, 0, (LPARAM)szItemText); // 将left listbox中选中项加入到右边listbox中 SendMessage(hWndListRight, LB_SETCURSEL, (WPARAM)(SendMessage(hWndListRight, LB_GETCOUNT, 0, 0)-1), 0);// 将右边最后一项(刚选入)选中 SendMessage(hWndListLeft, LB_DELETESTRING, nIndex, 0);// 删除 left listbox 中选中 SendMessage(hWndListLeft, LB_SETCURSEL, (WPARAM)nIndex, 0);// 选中被删除项的下一项 break; case LEFT_BUTTON:// left button (处理与RIGHT_BUTTON一样) nIndex = SendMessage(hWndListRight, LB_GETCURSEL, 0, 0); if (nIndex == LB_ERR) { MessageBox(hWnd, TEXT("No Selected"), TEXT("Message"), MB_OK); break; } GetListBoxItemText(hWndListRight, nIndex, &szItemText); SendMessage(hWndListLeft, LB_ADDSTRING, 0, (LPARAM)szItemText); SendMessage(hWndListLeft, LB_SETCURSEL, (WPARAM)(SendMessage(hWndListRight, LB_GETCOUNT, 0, 0)-1), 0); SendMessage(hWndListRight, LB_DELETESTRING, nIndex, 0); SendMessage(hWndListRight, LB_SETCURSEL, (WPARAM)(nIndex-1), 0); break; default:break; } break; case WM_KEYDOWN: break; case WM_DESTROY: PostQuitMessage (0) ; break;}return DefWindowProc(hWnd, uMsg, wParam, lParam);}