0

Hello, im new to working with windows controls and im trying to create a simple listbox inside a main window. It compiles fine, but when i run it, the listbox is all screwed up and im fairly sure that my error message is trying to pop up (but for some reason i dont see it...) which would mean hList is null. Ive searched around for answers but havnt really found any, since it does succesfully compile.

NOTE: the list box stuff is all located inbetween the problem code start and problem code end comment markers.

#include <windows.h>
#include <Commctrl.h>
#include "resource.h"

const char g_szClassName[] = "myWindowClass";
#define IDC_LISTBOX1		10151

// Dialog Procedure
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
    switch(Message)
    {
        case WM_INITDIALOG:

        return TRUE;
        case WM_COMMAND:
            switch(LOWORD(wParam))
            {
                case IDOK:
                    EndDialog(hwnd, IDOK);
                break;
            }
        break;
        default:
            return FALSE;
    }
    return TRUE;
}



// Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
		// Problem Code start ---------------------
		case WM_CREATE:
		{
			HFONT hfDefault;
			HWND hList = NULL;

			
			hList = CreateWindowEx(
				WS_EX_CLIENTEDGE,
				g_szClassName,
				"ListBox",
				WS_CHILD | WS_VISIBLE | WS_VSCROLL,
				10, 10, 100, 200, hwnd,
				(HMENU)IDC_LISTBOX1,
				GetModuleHandle(NULL),
				NULL);
				

			if(hList == NULL)
			{
				MessageBox(hwnd, "Could not create list box", "ERROR", MB_OK | MB_ICONERROR);
			}

			hfDefault = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
			SendMessage(hList, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE, 0));
		}
		break;
		// Problem Code end -----------------------
		case WM_COMMAND:
			switch(LOWORD(wParam))
			{
				case IDABOUT:
				{
					int retval = DialogBox(GetModuleHandle(NULL), 
					MAKEINTRESOURCE(IDD_DIALOG1), hwnd, AboutDlgProc);
					if(retval == -1)
					{
						MessageBox(hwnd, "Dialog failed to open", "ERROR",
								   MB_OK | MB_ICONINFORMATION);
					}
				}
				break;
				case IDQUIT:
					PostQuitMessage(0);
				break;
			}
		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)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    // Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = CreateSolidBrush(RGB(150, 150, 150));
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, 0);


    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed", "ERROR",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Main Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "PVM",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 540, 420,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed", "ERROR",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}

Heres a photo of the listbox, im pretty sure thats not what its supposed to look like
http://i86.photobucket.com/albums/k109/dark_ivader/listerror.jpg

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by mitrmkar
0

You've gotten a bit confused about how to use the CreateWindowEx() function, the following should work ...

hList = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    "ListBox", // the window class here 
    "Caption text ...", // needs WS_CAPTION style in order to be shown
                        // .. see below
    WS_CAPTION | WS_CHILD | WS_VISIBLE | WS_VSCROLL,
    10, 10, 100, 200, 
    hwnd,
    (HMENU) IDC_LISTBOX1,
    GetModuleHandle(NULL),
    NULL);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.