#include <windows.h> LPCTSTR strText[] = { TEXT("Kitty on your lap") , TEXT("Tokyo mew mew") , TEXT("Magical nyan nyan TARUTO") , TEXT("Di Gi Charat") , TEXT("Nekoneko Zoo") }; LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp) { static HWND sortList , nonSortList; int i; switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE: sortList = CreateWindow( TEXT("LISTBOX") , NULL , WS_CHILD | WS_VISIBLE | LBS_STANDARD , 0 , 0 , 200 , 100 , hwnd , (HMENU)1 , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); for (i = 0 ; i < 5 ; i++) SendMessage(sortList , LB_ADDSTRING , 0 , (LPARAM)strText[i]); nonSortList = CreateWindow( TEXT("LISTBOX") , NULL , WS_CHILD | WS_VISIBLE | WS_BORDER , 200 , 0 , 200 , 100 , hwnd , (HMENU)1 , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); for (i = 0 ; i < 5 ; i++) SendMessage(nonSortList , LB_INSERTSTRING , i , (LPARAM)strText[i] ); return 0; } return DefWindowProc(hwnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow ) { HWND hwnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return -1; hwnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hwnd == NULL) return -1; while(GetMessage(&msg , NULL , 0 , 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
#include <windows.h> LPCTSTR strText[] = { TEXT("Kitty on your lap") , TEXT("Tokyo mew mew") , TEXT("Magical nyan nyan TARUTO") , TEXT("Di Gi Charat") , TEXT("Nekoneko Zoo") }; enum { BID_DEL = 2 , BID_RESET }; LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp) { static HWND list; int i; switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE: list = CreateWindow( TEXT("LISTBOX") , NULL , WS_CHILD | WS_VISIBLE | LBS_STANDARD , 0 , 0 , 200 , 100 , hwnd , (HMENU)1 , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); for (i = 0 ; i < 5 ; i++) SendMessage(list , LB_ADDSTRING , 0 , (LPARAM)strText[i]); CreateWindow( TEXT("BUTTON") , TEXT("削除") , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 200 , 0 , 100 , 30 , hwnd , (HMENU)BID_DEL , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); CreateWindow( TEXT("BUTTON") , TEXT("リセット") , WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON , 200 , 30 , 100 , 30 , hwnd , (HMENU)BID_RESET , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); return 0; case WM_COMMAND: switch(LOWORD(wp)) { case BID_DEL: SendMessage(list , LB_DELETESTRING , SendMessage(list , LB_GETCURSEL , 0 , 0) , 0); break; case BID_RESET: SendMessage(list , LB_RESETCONTENT , 0 , 0); break; } return 0; } return DefWindowProc(hwnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow ) { HWND hwnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return -1; hwnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hwnd == NULL) return -1; while(GetMessage(&msg , NULL , 0 , 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }「削除」ボタンを押すと、選択されているアイテムがリストから削除されます
#include <windows.h> LPCTSTR strItem[] = { TEXT("Kitty on your lap") , TEXT("Tokyo mew mew") , TEXT("Magical nyan nyan TARUTO") , TEXT("Di Gi Charat") , TEXT("Nekoneko Zoo") }; LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp) { static HWND list , label; TCHAR strText[1024]; int i; switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE: list = CreateWindow( TEXT("LISTBOX") , NULL , WS_CHILD | WS_VISIBLE | LBS_STANDARD , 0 , 0 , 200 , 100 , hwnd , (HMENU)1 , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); for (i = 0 ; i < 5 ; i++) SendMessage(list , LB_ADDSTRING , 0 , (LPARAM)strItem[i]); label = CreateWindow( TEXT("STATIC") , NULL , WS_CHILD | WS_VISIBLE , 200 , 0 , 150 , 80 , hwnd , (HMENU)2 , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); SendMessage(list , LB_SETCURSEL , 0 , 0); return 0; case WM_COMMAND: wsprintf(strText , TEXT("項目数 = %d\n項目文字数 = %d") , SendMessage(list , LB_GETCOUNT , 0 , 0) , SendMessage(list , LB_GETTEXTLEN , SendMessage(list , LB_GETCURSEL , 0 , 0) , 0 ) ); SetWindowText(label , strText); return 0; } return DefWindowProc(hwnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow ) { HWND hwnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return -1; hwnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hwnd == NULL) return -1; while(GetMessage(&msg , NULL , 0 , 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
#include <windows.h> LPCTSTR strItem[] = { TEXT("AAABBB") , TEXT("AABBBB") , TEXT("AAAACC") , TEXT("aaaCCC") , TEXT("ABBCCC") }; LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp) { static HWND list , edit; TCHAR strText[1024]; int i; switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE: list = CreateWindow( TEXT("LISTBOX") , NULL , WS_CHILD | WS_VISIBLE | LBS_STANDARD , 0 , 30 , 200 , 100 , hwnd , (HMENU)1 , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); for (i = 0 ; i < 5 ; i++) SendMessage(list , LB_ADDSTRING , 0 , (LPARAM)strItem[i]); edit = CreateWindow( TEXT("EDIT") , NULL , WS_CHILD | WS_VISIBLE | WS_BORDER , 0 , 0 , 200 , 30 , hwnd , (HMENU)2 , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); SendMessage(list , LB_SETCURSEL , 0 , 0); return 0; case WM_COMMAND: if (LOWORD(wp) == 2) { GetWindowText(edit , strText , 1024); SendMessage(list , LB_SELECTSTRING , -1 , (LPARAM)strText); } return 0; } return DefWindowProc(hwnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow ) { HWND hwnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return -1; hwnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hwnd == NULL) return -1; while(GetMessage(&msg , NULL , 0 , 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }
#include <windows.h> LPCTSTR strItem[] = { TEXT("Kitty on your lap") , TEXT("Tokyo mew mew") , TEXT("Magical nyan nyan TARUTO") , TEXT("Di Gi Charat") , TEXT("Nekoneko Zoo") }; LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp) { static HWND list , edit; PSTR strText; int i; switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE: list = CreateWindow( TEXT("LISTBOX") , NULL , WS_CHILD | WS_VISIBLE | LBS_STANDARD , 0 , 30 , 200 , 100 , hwnd , (HMENU)1 , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); for (i = 0 ; i < 5 ; i++) SendMessage(list , LB_ADDSTRING , 0 , (LPARAM)strItem[i]); edit = CreateWindow( TEXT("EDIT") , NULL , WS_CHILD | WS_VISIBLE | WS_BORDER , 0 , 0 , 200 , 30 , hwnd , (HMENU)2 , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); SendMessage(list , LB_SETCURSEL , 0 , 0); return 0; case WM_COMMAND: if (LOWORD(wp) == 1) { strText = (PSTR)malloc( SendMessage(list ,LB_GETTEXTLEN , SendMessage(list , LB_GETCURSEL , 0 , 0) , 0 ) + 1 ); SendMessage(list , LB_GETTEXT , SendMessage(list , LB_GETCURSEL , 0 , 0) , (LPARAM)strText ); SetWindowText(edit , strText); free(strText); } return 0; } return DefWindowProc(hwnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow ) { HWND hwnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return -1; hwnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hwnd == NULL) return -1; while(GetMessage(&msg , NULL , 0 , 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }リストボックスの何らかの項目を選択すると
通知コード | 解説 |
---|---|
LBN_DBLCLK | 項目をダブルクリック |
LBN_ERRSPACE | リストボックスがメモリを使いきり メモリを取得できない |
LBN_KILLFOCUS | キーボードフォーカスを失った |
LBN_SELCANCEL | 選択項目をキャンセル |
LBN_SELCHANGE | 選択項目に、何らかの変更があった (選択項目をクリックしたときなど) |
LBN_SETFOCUS | キーボードフォーカスを得た |
#include <windows.h> LPCTSTR strItem[] = { TEXT("Kitty on your lap") , TEXT("Tokyo mew mew") , TEXT("Magical nyan nyan TARUTO") , TEXT("Di Gi Charat") , TEXT("Nekoneko Zoo") }; LRESULT CALLBACK WndProc(HWND hwnd , UINT msg , WPARAM wp , LPARAM lp) { static HWND list; int i; switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; case WM_CREATE: list = CreateWindow( TEXT("LISTBOX") , NULL , WS_CHILD | WS_VISIBLE | LBS_STANDARD , 0 , 0 , 200 , 100 , hwnd , (HMENU)1 , ((LPCREATESTRUCT)(lp))->hInstance , NULL ); for (i = 0 ; i < 5 ; i++) SendMessage(list , LB_ADDSTRING , 0 , (LPARAM)strItem[i]); return 0; case WM_COMMAND: if (HIWORD(wp) == LBN_DBLCLK) MessageBox(hwnd , TEXT("ダブルクリック") , TEXT("") , MB_OK); return 0; } return DefWindowProc(hwnd , msg , wp , lp); } int WINAPI WinMain(HINSTANCE hInstance , HINSTANCE hPrevInstance , PSTR lpCmdLine , int nCmdShow ) { HWND hwnd; MSG msg; WNDCLASS winc; winc.style = CS_HREDRAW | CS_VREDRAW; winc.lpfnWndProc = WndProc; winc.cbClsExtra = winc.cbWndExtra = 0; winc.hInstance = hInstance; winc.hIcon = LoadIcon(NULL , IDI_APPLICATION); winc.hCursor = LoadCursor(NULL , IDC_ARROW); winc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); winc.lpszMenuName = NULL; winc.lpszClassName = TEXT("KITTY"); if (!RegisterClass(&winc)) return -1; hwnd = CreateWindow( TEXT("KITTY") , TEXT("Kitty on your lap") , WS_OVERLAPPEDWINDOW | WS_VISIBLE , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , CW_USEDEFAULT , NULL , NULL , hInstance , NULL ); if (hwnd == NULL) return -1; while(GetMessage(&msg , NULL , 0 , 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; }項目をダブルクリックすると、メッセージボックスが表示されます