freezing combo box

Hello,

I'm trying to get a drop-down combo box to work. The problem is, when I open the combo box, the entire window freezes until I press the Escape key. When the window freezes, the combo box doesn't receive any messages at all.

This is the code I use:

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
#include <Windows.h>
#include <iostream>
#include <CommCtrl.h>

HWND hComboBox;

void populate_cb() {
	SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"Item 1");
	SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"Item 2");
	SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"Item 3");
	SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"Item 4");
	SendMessage(hComboBox, CB_ADDSTRING, 0, (LPARAM)"Item 5");
	SendMessage(hComboBox, CB_SETCURSEL, (WPARAM)2, 0);
}

LRESULT CALLBACK CBProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
	UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
	switch (msg) {
	default:
		std::cout << "MESSAGE: " << msg << '\n';
	}
	return DefSubclassProc(hwnd, msg, wParam, lParam);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch (msg) {
	case WM_CREATE:
		hComboBox = CreateWindow("COMBOBOX", nullptr,
			WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_OVERLAPPED | CBS_DROPDOWNLIST,
			25, 25, 150, 280, hwnd, (HMENU)1337, GetModuleHandle(nullptr), nullptr);
		populate_cb();
		SetWindowSubclass(hComboBox, CBProc, 0, 0);
		break;
	case WM_CLOSE:
		DestroyWindow(hwnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		RemoveWindowSubclass(hComboBox, CBProc, 0);
		break;
	default:
		return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wcex;
	wcex.cbClsExtra = 0;
	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.cbWndExtra = 0;
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW);
	wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
	wcex.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
	wcex.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
	wcex.hInstance = hInstance;
	wcex.lpfnWndProc = WndProc;
	wcex.lpszClassName = "WindowClass";
	wcex.lpszMenuName = nullptr;
	wcex.style = 0;

	if (!RegisterClassEx(&wcex)) {
		MessageBox(nullptr, "Window class registration failed!", "Error", MB_OK | MB_ICONERROR);
		return 1;
	}

	HWND hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, "WindowClass", "Window Title",
		WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, nullptr,
		nullptr, hInstance, nullptr);

	if (!hwnd) {
		MessageBox(nullptr, "Window creation failed!", "Error", MB_OK | MB_ICONERROR);
		return 1;
	}

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

	MSG msg;
	while (GetMessage(&msg, hwnd, 0, 0) > 0) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}

int main() {
	return WinMain(GetModuleHandle(nullptr), nullptr, nullptr, SW_SHOW);
}
Last edited on
1
2
3
int main() {
	return WinMain(GetModuleHandle(nullptr), nullptr, nullptr, SW_SHOW);
}

This looks very odd. I think you cant have a main and a WinMain function. If you want a console you better do it like this:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=4971&lngWId=3
If you use Visual Studio you also could use the OutputDebugString function
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363362(v=vs.85).aspx
Try this 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
92
93
#include <windows.h>
#include <tchar.h>
#include <Commctrl.h>
#include <crtdbg.h>

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

HWND CreateComboBox(DWORD dwStyle, int x, int y, int width, int height, 
                    HWND hParent, HMENU hMenu)
{
  return CreateWindow("COMBOBOX", 0, dwStyle, x, y, width, height, hParent, hMenu, GetModuleHandle(0), 0);
}

void FillComboBox(HWND hCombo)
{
  LPCSTR names[] = { "Anna", "Lisa", "Louisa", "Anja", "Elisabeth", "Cassandra" };
  int count = sizeof(names) / sizeof(names[0]);

  for (int i = 0; i < count; i++)
  {
    SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)names[i]);
  }

  SendMessage(hCombo, CB_SETCURSEL, 1, 0);
}

LRESULT CALLBACK CBProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam,
	                      UINT_PTR uIdSubclass, DWORD_PTR dwRefData) 
{
	return DefSubclassProc(hwnd, msg, wParam, lParam);
}

LRESULT CALLBACK WndProc (HWND hwnd, unsigned int msg, WPARAM wParam, LPARAM lParam)
{
  HWND hCombo = 0;

  switch (msg)
  {
    case WM_CREATE:
    {
      HINSTANCE hIns = ((LPCREATESTRUCT)lParam)->hInstance;
      hCombo = CreateComboBox (WS_CHILD | CBS_DROPDOWN | WS_VISIBLE, 10, 10, 200, 200, 
                               hwnd, (HMENU)1000);
      _ASSERTE(IsWindow(hCombo));
      FillComboBox(hCombo);
      SetWindowSubclass(hCombo, CBProc, 0, 0);
      return 0;
    }
    case WM_COMMAND:
    {
      return 0;
    }
    case WM_DESTROY:
    {
      PostQuitMessage (0);
      RemoveWindowSubclass(hCombo, CBProc, 0);
      return TRUE;
    }
  }

  return (DefWindowProc (hwnd, msg, wParam, lParam));
}


int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevIns, LPSTR lpszArgument, int iShow)
{
  WNDCLASSEX wc = { 0 };
  MSG messages;
  HWND hWnd;

  wc.lpszClassName = _T("Demo");
  wc.lpfnWndProc = WndProc;
  wc.cbSize = sizeof (WNDCLASSEX);
  wc.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
  wc.hInstance = hInstance;
  
  _ASSERTE(RegisterClassEx (&wc) != 0);
  
  hWnd = CreateWindowEx (0, _T("Demo"), _T("ComboBox Demo"), WS_OVERLAPPEDWINDOW, 
      CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
      HWND_DESKTOP, 0, hInstance, 0);
      
  _ASSERTE(::IsWindow(hWnd));
  
  ShowWindow (hWnd, iShow);
  while (GetMessage (&messages, NULL, 0, 0))
  {
    TranslateMessage (&messages);
    DispatchMessage (&messages);
  }

  return static_cast<int>(messages.wParam);
}
Topic archived. No new replies allowed.