Page 1 of 1

Microsoft : Working With Listboxes - Part I Using simple listboxes with strings Rate Topic: -----

#1 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • Posts: 1,406
  • Joined: 22-August 09

Posted 23 March 2010 - 01:28 PM

Microsoft : Working With Listboxes - Part I

Introduction

This two part tutorial discusses listboxes. Listboxes provide a means of listing like items in a window, and enabling the user to scroll up and down the list.

Creating a listbox

A listbox is created by specifying "Listbox" as the window class. The following code snippet will create a listbox.

    window_handle = CreateWindowEx(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,    
                                   window_class_name,    
                                   L"Listbox Example",   
                                   WS_OVERLAPPEDWINDOW | 
                                   WS_CLIPCHILDREN | WS_CLIPSIBLINGS,   
                                   100, 100, screen_width-200,    
                                   screen_height-200, NULL, NULL,   
                                   instance_handle, NULL);
    RECT client_rectangle;
    GetClientRect(window_handle, &client_rectangle);
    int width = client_rectangle.right - client_rectangle.left;
    int height = client_rectangle.bottom - client_rectangle.top;
    listbox_handle = CreateWindowEx(WS_EX_CLIENTEDGE, L"Listbox", NULL,
                                    WS_CHILD | WS_VISIBLE | WS_VSCROLL |
                                    LBS_DISABLENOSCROLL | LBS_NOSEL,   
                                    10, 10, width-20,
                                    height-20, window_handle, (HMENU)11,   
                                    instance_handle, NULL);



The listbox specific flag LBS_NOSEL used in the window style parameter simply means that the user cannot select items in the listbox. Alternates to this would be not to specify LBS_NOSEL permitting one single line to be selected (this is the default action), LBS_MULTISEL permitting multiple selections and LBS_EXTENDEDSEL which permits multiple selections using the keyboard keys. The WS_VSCROLL in combination with the LBS_DISABLENOSCROLL says that if there are fewer items in the listbox than can be displayed, do not hide the scroll window.

Sorting strings in the listbox

To sort the list, simply add LBS_SORT to the window style.

So modifying the code example above gives us

    listbox_handle = CreateWindowEx(WS_EX_CLIENTEDGE, L"Listbox", NULL,
                                    WS_CHILD | WS_VISIBLE | WS_VSCROLL |
                                    LBS_DISABLENOSCROLL | LBS_NOSEL | LBS_SORT,   
                                    10, 10, width-20,
                                    height-20, window_handle, (HMENU)11,   
                                    instance_handle, NULL);



Adding strings to the listbox

To add items to a listbox, we use the SendMessage function with a message type LB_ADDSTRING. This will add a NULL teminated string to the end of the list.

    SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Martyn.Rae");



Finding a string in the listbox

As the list is now sorted into order, the last item in the list is not "Martyn.Rae". So, to find the index position of that string in the list, we use the SendMessage function with a message type LB_FINDSTRING and the string we want to find (this function is case-insensitive - to find an exact string match use the LB_FINDSTRINGEXACT function). We can then select that index item in the list using LB_SETCURSEL.

    int index = SendMessage(listbox_handle, LB_FINDSTRING, 0, (LPARAM)L"Martyn.Rae");
    SendMessage(listbox_handle, LB_SETCURSEL, index, 0);



Deleting strings from the listbox

To delete a string from the listbox, we would find the index of the string to delete and then use the SendMessage function with a message type LB_DELETESTRING to delete the string.

    int index = SendMessage(listbox_handle, LB_FINDSTRING, 0, (LPARAM)L"Martyn.Rae");
    SendMessage(listbox_handle, LB_DELETESTRING, index, 0);



A word of warning

A listbox cannot be used if there are going to be more than 32,767 items in the listbox. The mechanism used by the listbox for allocating memory for large numbers of items is very inefficient, but you can use the SendMessage function with a message type LB_INITSTORAGE to allocate space to contain the items.

Example Code

The code example below demonstrates the use of a simple listbox that contains name strings. The list is sorted and will find and select the listbox item for the string "Martyn.Rae".

#include <windows.h>  
#include <CommCtrl.h> 
#include <ctime>  
#include <cstdlib>  
using namespace std;

#pragma comment(lib, "comctl32.lib")

class frame_window {  
    private:
        LPWSTR window_class_name;  
        HINSTANCE instance_handle;  
        HCURSOR cursor_arrow;  
        HWND window_handle;
        HWND listbox_handle;
        RECT client_rectangle;
        int pressed;
    public:
        frame_window(LPWSTR window_class_identity) : window_class_name(window_class_identity), pressed(-1) {  
            int screen_width = GetSystemMetrics(SM_CXFULLSCREEN);  
            int screen_height = GetSystemMetrics(SM_CYFULLSCREEN);  
            instance_handle = GetModuleHandle(NULL);  
               
            WNDCLASS window_class = { CS_OWNDC, main_window_proc, 0, 0,    
                                      instance_handle, NULL,    
                                      NULL, NULL, NULL,    
                                      window_class_name };   
            
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
            // Create a standard frame window   
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
            
            RegisterClass(&window_class);   
            window_handle = CreateWindowEx(WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,    
                                           window_class_name,    
                                           L"Listbox Example",   
                                           WS_OVERLAPPEDWINDOW | 
                                           WS_CLIPCHILDREN | WS_CLIPSIBLINGS,   
                                           300, 300, screen_width-600,    
                                           screen_height-600, NULL, NULL,   
                                           instance_handle, NULL);
            RECT client_rectangle;
            GetClientRect(window_handle, &client_rectangle);
            int width = client_rectangle.right - client_rectangle.left;
            int height = client_rectangle.bottom - client_rectangle.top;
            listbox_handle = CreateWindowEx(WS_EX_CLIENTEDGE, L"Listbox", NULL,
                                            WS_CHILD | WS_VISIBLE | WS_VSCROLL |
                                            LBS_DISABLENOSCROLL | LBS_SORT,   
                                            10, 10, width-20,
                                            height-20, window_handle, (HMENU)11,   
                                            instance_handle, NULL);
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"mark.bottomley");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"megamu");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Bench");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Cerolobo");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"CyanBlue");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"DilutedImage");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"g00se");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"gregoryH");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"ajwsurfer");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"baavgai");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"CTphpnwb");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Dogstopper");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"eclipsed4utoo");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Gloin");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"gregwhitworth");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"GWatt");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"KevinADC");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"mostyfriedman");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"n8wxs");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"perfectly.insane");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"letthecolorsrumble");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"lockdown");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Martyn.Rae");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Martyr2");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"pbl");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"PennyBoki");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Programmist");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"macosxnerd101");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"mocker");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"MorphiusFaydal");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"thava");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Ancient Dragon");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"EdwinNameless");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"erik.price");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"wildgoose");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"FlashM");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Fuzzyness");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Arbitrator");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"malkiri");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"red01");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"MentalFloss");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"japanir");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"joeyadms");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"mikeblas");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"reyco1");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"RodgerB");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"runtime error");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"ScottAdams");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"ShaneK");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"SwiftStriker00");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"horace");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"janotte");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"akozlik");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"Voodoo Doll");
            SendMessage(listbox_handle, LB_ADDSTRING, 0, (LPARAM)L"WolfCoder");
            int index = SendMessage(listbox_handle, LB_FINDSTRING, 0, (LPARAM)L"Martyn.Rae");
            SendMessage(listbox_handle, LB_SETCURSEL, index, 0);
            SetCursor(LoadCursor(NULL, IDC_ARROW)); 
            SetWindowLongPtr(window_handle, GWL_USERDATA, (LONG)this);  
            ShowWindow(window_handle, SW_SHOW);   
            UpdateWindow(window_handle); 
        }  
        ~frame_window() {  
            UnregisterClass(window_class_name, instance_handle);   
        }  
        
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
        // Windows message processing function   
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
        static LRESULT WINAPI main_window_proc(HWND window_handle, UINT message,    
                                               WPARAM wparam, LPARAM lparam) {  
            frame_window *This = (frame_window *)GetWindowLongPtr(window_handle, GWL_USERDATA);  
            switch ( message ) {  
                case WM_PAINT:   
                {   
                    PAINTSTRUCT paint_structure;   
                    RECT client_rect;   
                    HDC paint_device_context, paint_dc;   
                    HBITMAP bitmap;   
  
                    paint_device_context = BeginPaint(window_handle, &paint_structure);   
                    paint_dc = CreateCompatibleDC(paint_device_context);   
                    GetClientRect(window_handle, &client_rect);   
                    int window_width = client_rect.right - client_rect.left;  
                    int window_height = client_rect.bottom - client_rect.top;  
                    bitmap = CreateBitmap(window_width, window_height, 1, 32, NULL);  
                    HGDIOBJ old_bitmap = SelectObject(paint_dc, bitmap);  
  
                    // Fill the client aread with the user selected face colour  
                    HBRUSH light_brush = CreateSolidBrush(GetSysColor(COLOR_BTNFACE));  
                    FillRect(paint_dc, &paint_structure.rcPaint, light_brush);  
                    DeleteObject(light_brush);  
                                                                            
                    BitBlt(paint_device_context, 0, 0,    
                           client_rect.right-client_rect.left,   
                           client_rect.bottom-client_rect.top,   
                           paint_dc, 0, 0, SRCCOPY);   
                    SelectObject(paint_dc, old_bitmap);   
                    DeleteObject(bitmap);   
                    DeleteDC(paint_dc);   
                    EndPaint(window_handle, &paint_structure);   
                    return 0;   
                }   
                case WM_ERASEBKGND:   
                {   
                    return TRUE;   
                }  
                case WM_SIZE:   
                {
                    RECT client_rectangle;
                    GetClientRect(window_handle, &client_rectangle);
                    int width = client_rectangle.right - client_rectangle.left;
                    int height = client_rectangle.bottom - client_rectangle.top;
                    InvalidateRect(window_handle, NULL, TRUE);   
                    return 0;   
                }
                case WM_COMMAND:
                {
                    return 0;
                }
                case WM_CLOSE:   
                {   
                    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                    // The user wants to close the window   
                    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
           
                    PostQuitMessage(0);   
                    return 0;   
                }   
            }   
            return DefWindowProc(window_handle, message, wparam, lparam);   
        }   
          
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
        // Windows message loop   
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        void run() {  
            MSG window_message;   
            while ( GetMessage(&window_message, NULL, 0, 0) ) {   
                TranslateMessage(&window_message);   
                DispatchMessage(&window_message);   
            }   
        }  
};  
   
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
// Windows main entry point   
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++   
   
int WINAPI wWinMain(HINSTANCE instance_handle, HINSTANCE, LPWSTR, INT) {  
    frame_window main_window(L"my base window");  
    main_window.run();  
    return 0;   
}



The next part of this tutorial may be found here.

This post has been edited by Martyn.Rae: 23 March 2010 - 11:41 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1