Thread: Basic GUI question (trouble getting notifications from listboxes)

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    19

    Basic GUI question (trouble getting notifications from listboxes)

    I create my child windows (listbox, buttons etc.) with:
    Code:
    switch(message) {
    	case WM_CREATE:
    		ListBox = CreateWindow();
    	break;
    }
    So how do I process their notifications? The examples I've seen do this:
    Code:
    case WM_COMMAND: 
    	switch (LOWORD(wParam)) {
    		case SOME_REAL_LOUD_CONSTANT_DEFINED_IN_THE_RC_FILE: 
                        switch (HIWORD(wParam)) {
    			// All the interesting stuff
    			}
    		break;
    	}
    break;
    But I don't have that constant defined in the rc file. I'm creating my windows with createwindow instead of using dialogs or something. So what do I need to do (differently)?

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Code:
    #include <windows.h>
    
    #define IDI_ICON         101
    #define IDC_LISTBOX_TEXT 200
    
    const char ClassName[] = "MyWindowClass";
    HWND hListBox;
    
    LRESULT CALLBACK WndProc( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM  lParam )
    {
     char Buffer[256];
     int index;
        switch (Msg)
        {
            case WM_CREATE:
                {
                    hListBox = CreateWindow(
                        "LISTBOX", 
                        NULL, 
                        WS_VISIBLE | WS_CHILD | LBS_STANDARD | LBS_NOTIFY, 
                        10, 
                        10, 
                        100, 
                        50, 
                        hWnd, 
                        (HMENU)IDC_LISTBOX_TEXT, 
                        (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), 
                        NULL);
                    SendMessage(GetDlgItem(hWnd, IDC_LISTBOX_TEXT), LB_ADDSTRING, 0, (LPARAM)"Bob");
                    SendMessage(GetDlgItem(hWnd, IDC_LISTBOX_TEXT), LB_ADDSTRING, 0, (LPARAM)"Joe");
                    SendMessage(GetDlgItem(hWnd, IDC_LISTBOX_TEXT), LB_ADDSTRING, 0, (LPARAM)"Theodore");
                }
                break; 
            case WM_COMMAND: 
                {
                    switch(LOWORD(wParam))
                    {
                        case IDC_LISTBOX_TEXT:
                            {
                                switch(HIWORD(wParam))
                                {
                                    case LBN_SELCHANGE:
                                        {
                                            index = SendMessage((HWND)lParam, LB_GETCARETINDEX, 0, 0);
                                            SendMessage((HWND)lParam, LB_GETTEXT, (LPARAM)index, (WPARAM)Buffer);
                                            SetWindowText(hWnd, Buffer); 
                                        }
                                }
                            }
                            break;
                    }
                    return 0;
                } 
                break; 
    
            case WM_CLOSE:
                DestroyWindow(hWnd);
                break;
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
            default:
                return (DefWindowProc(hWnd, Msg, wParam, lParam));
        }
        return 0;
    }
    
    INT WINAPI WinMain( HINSTANCE  hInstance, HINSTANCE  hPrevInstance, LPSTR lpCmdLine, INT nCmdShow )
    {
      	HWND    hWnd;
    	MSG    Msg;
        WNDCLASSEX    wc;
    
        wc.cbSize           = sizeof(WNDCLASSEX);
        wc.style            = 0;
        wc.lpfnWndProc      = (WNDPROC)WndProc;
        wc.cbClsExtra       = 0;
        wc.cbWndExtra       = 0;
        wc.hInstance        = hInstance;
        wc.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
        wc.hIconSm          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
        wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground    = (HBRUSH)(COLOR_WINDOW + 1);
        wc.lpszMenuName     = NULL;
        wc.lpszClassName    = ClassName;
    
        if (!RegisterClassEx(&wc))
        {
            MessageBox(NULL, "Failed To Register The Window Class.", "Error", MB_OK | MB_ICONERROR);
            return 0;
        }
      
        hWnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            ClassName,
            "List Box Test",
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT,
            CW_USEDEFAULT,
            240,
            120,
            NULL,
            NULL,
            hInstance,
            NULL);
    
        if (!hWnd)
        {
            MessageBox(NULL, "Window Creation Failed.", "Error", MB_OK | MB_ICONERROR);
            return 0;
        }
        ShowWindow(hWnd, SW_SHOW);
        UpdateWindow(hWnd);
       
        while (GetMessage(&Msg, NULL, 0, 0))
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    }

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    19
    My appologies for not reading up on createwindow well enough.

    Thanks for the anwser.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Basic question
    By voodoo3182 in forum Windows Programming
    Replies: 11
    Last Post: 09-12-2005, 07:40 PM
  2. A Very Basic Question... I think.
    By Hp_Sauce in forum C++ Programming
    Replies: 8
    Last Post: 08-10-2005, 10:04 PM
  3. General GUI question in C
    By Music_Man in forum Game Programming
    Replies: 3
    Last Post: 11-16-2001, 11:45 AM
  4. A very basic question
    By AshFooYoung in forum C Programming
    Replies: 8
    Last Post: 10-07-2001, 03:37 PM