win32 API listbox SendMessage not working

Hi
I have a problem with SendMessage. Anything I send doesn't show up but instead empty lines appear. What is wrong with my code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	TCHAR greeting[] = _T("Hello, World!");
	wchar_t listBoxStr[15];


	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		case LISTBOX_GET:
			SendMessageW(hListBox, LB_GETTEXT,0,(LPARAM)listBoxStr);
			MessageBoxW(NULL, listBoxStr, L"", NULL);
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...

		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	case WM_CREATE://executes when the  window is created
		InitializeComponent(hWnd);
		SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)greeting);
		SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)"text------------------");
		return 0;



	}
	return 0;
}

void InitializeComponent(HWND hWnd) {
    HINSTANCE hInstance = GetModuleHandle(NULL);

    // Adding a Button.
    hBtn = CreateWindowExW(WS_EX_APPWINDOW
        , L"BUTTON", NULL
        , WS_CHILD | WS_VISIBLE
        , 627, 7, 70, 21
        , hWnd, NULL, hInstance, NULL);        

    SetWindowTextW(hBtn, L"&Lisa");

    // Adding a Label.
    hLabel = CreateWindowExW(WS_EX_CLIENTEDGE
        , L"STATIC", NULL
        , WS_CHILD | WS_VISIBLE
        , 7, 7, 50, 21
        , hWnd, NULL, hInstance, NULL);

    SetWindowTextW(hLabel, L"Käsud:");

    // Adding a ListBox.
    hListBox = CreateWindowExW(WS_EX_CLIENTEDGE
        , L"LISTBOX", NULL
        , WS_CHILD | WS_VISIBLE | ES_VSCROLL | ES_AUTOVSCROLL
        , 7, 35, 600, 200
        , hWnd, NULL, hInstance, NULL);

    // Adding a TextBox.
    hTextBox = CreateWindowExW(WS_EX_CLIENTEDGE
        , L"EDIT", NULL
        , WS_CHILD | WS_VISIBLE | ES_AUTOVSCROLL
        , 62, 7, 545, 21
        , hWnd, NULL, hInstance, NULL);

    SetWindowTextW(hTextBox, L"Input text here...");
}

Where ES_VSCROLL | ES_AUTOVSCROLL does came from ? They are NOT supposed to be used for a listbox, are used for edit control.
1
2
3
4
5
6
/ Adding a ListBox.
    hListBox = CreateWindowExW(WS_EX_CLIENTEDGE
        , L"LISTBOX", NULL
        , WS_CHILD | WS_VISIBLE | ES_VSCROLL | ES_AUTOVSCROLL
        , 7, 35, 600, 200
        , hWnd, NULL, hInstance, NULL);


http://msdn.microsoft.com/en-us/library/windows/desktop/bb775149(v=vs.85).aspx

THere are other issues with your code, as mixing char's with wchar_t's and cast them to LPARAM, as you do here:
1
2
SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)greeting);
		SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)"text------------------")
Thanks, you are absolutelly right. I mostly code by examples and I'm not that good that I can see others mistakes.

About your second comment: can you say how you would do that?

I have a question about the char ("text--------") and TCHAR (greeting): the second works fine but the first gives me chinese characters. Why might that be?
There are countless answers on this forum about char, wchar_t and TCHAR regarding Unicode settings.
All these 3 are correct :
1
2
3
4
SendMessageA(hListBox, LB_ADDSTRING, 0, (LPARAM)"text------------------");
SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)greeting);

SendMessage(hListBox, LB_ADDSTRING, 0, (LPARAM)_T("text------------------"));
OK, thanks.

I also get problems with string to LPARAM conversion. I have a vector of strings and I'd like to print them out using SendMessage but I get errors 'no suitable conversion fubction from string to LPARAM available'.
I know I could convert string to char to LPARAM but it's a real mess. (and I'm also having problem with char and SendMessage, I get chinese characters. TCHAR to LPARAM is ok). Is there a good way to do this??
SendMessage only works with std::basic_string<TCHAR>::c_str(). If you use std::string, use SendMessageA directly.
Still having problems
SendMessageA and SendMessageW also give the same error:
	2	IntelliSense: no suitable conversion function from "std::string" to "LPARAM" exists	


Does anyone have a bbetter way for displaying strings or converting them please?
I'm getting pretty frustrated and I'm pretty happy I have never needed Win32 API before, its a real mess.
Finally got it in a quite roundabout way:
first I converted it to wstring
1
2
3
		wstring stringConverted;//commandList[a] is a vector element, it is a string
		stringConverted.assign(commandsList[a].begin(), commandsList[a].end() );//convert line  from vector=string to wstring (wide string, Windows prefered format)
		return stringConverted;


and then to LPCWSTR:
1
2
3
			wstring myName2=mainList.returnOneLine(i);//can't use directly, problems with symbols
			LPCWSTR pstr = myName2.c_str(); //convert to LPCWSTR so that it could be converted to LPARAM on the next line
			SendMessageW(hListBox, LB_ADDSTRING, 0, (LPARAM)(pstr));//send 1 line of vector to list 
Topic archived. No new replies allowed.