Up-Down control doesn't show its position in its buddy window - c++

I created an up-down control by the following code.
HWND hEdit, hUpDown;
hEdit = CreateWindowExW(WS_EX_CLIENTEDGE,
L"EDIT",
Content.c_str(),
ES_LEFT | WS_VISIBLE | WS_CHILD,
600,
260,
100,
25,
hWndParent,
NULL,
hInstance,
NULL);
INITCOMMONCONTROLSEX iccx;
iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccx.dwICC = ICC_UPDOWN_CLASS;
InitCommonControlsEx(&iccx);
hUpDown = CreateWindowExW( 0,
UPDOWN_CLASSW,
L"",
UDS_ARROWKEYS | UDS_ALIGNRIGHT | WS_VISIBLE | WS_CHILD,
0,
0,
0,
0,
hWndParent,
NULL,
hInstance,
NULL);
SendMessageW(hUpDown, UDM_SETBUDDY, (WPARAM) hEdit, (LPARAM) NULL);
SendMessageW(hUpDown, UDM_SETRANGE32, (WPARAM) 0, (LPARAM) 100);
Sleep(5000);
SendMessageW(hUpDown, UDM_SETPOS32, (WPARAM) NULL, (LPARAM) 20);
Sleep(5000);
SendMessageW(hUpDown, UDM_SETPOS32, (WPARAM) NULL, (LPARAM) 60);
I checked the return values of the SendMessageW() functions. They terminate successfully by returning the previous position value as documented.
The created up-down control looks normal:
The problem is, sending the UDM_SETPOS32 message, clicking the up and down arrows and pressing the up and down keys on the keyboard have no effect. I can't change the contents of the edit control (the buddy window of the up-down control) without directly typing something into it. It just stays empty.
I am able to type anything in it manually by using keyboard:
How do I change the position/value of this up-down control by pressing keyboard arrow keys, by clicking the arrows in the GUI and by sending UDM_SETPOS32 in the code? What am I missing in my code?

Use the style UDS_SETBUDDYINT to the up-down control while creating it.
From MSDN documentation:
UDS_SETBUDDYINT
Causes the up-down control to set the text of the buddy window (using the WM_SETTEXT message) when the position changes. The text consists of the position formatted as a decimal or hexadecimal string.
Change the creation code of the up-down control like this by adding the UDS_SETBUDDYINT style:
hUpDown = CreateWindowExW( 0,
UPDOWN_CLASSW,
L"",
UDS_SETBUDDYINT | UDS_ARROWKEYS | UDS_ALIGNRIGHT | WS_VISIBLE | WS_CHILD,
0,
0,
0,
0,
hWndParent,
NULL,
hInstance,
NULL);

Related

How do I create two different comboboxes with different strings of items?

I have created a combo box having a list of items. How do I create another one with different strings of items? Can I in anyway change the hWnd because it seems the first already has the hWndCombobxes. Thus, when I apply it to the second, I get an error message, indicating that there is a duplicate value.
Below is the code I have. What function should I call else?
case WM_CREATE: {
HWND hWndComboBox = CreateWindow (TEXT("COMBOBOX"), TEXT (""),
CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
100, 150, 200, 150,
hwnd ,(HMENU) ID_COMBOBOX 1, NULL, NULL);
// ADD 2 ITEMS
SendMessage (
hWndComboBox,
(UINT) CB_ADDSTRING,
(WPARAM) 0, (LPARAM) TEXT ("Item 1"));
SendMessage (
hWndComboBox ,
(UINT) CB_ADDSTRING,
(WPARAM) 0, (LPARAM) TEXT ("Item 2"));
// SEND THE CB_SETCURSEL MESSAGE TO DISPLAY AN INITIAL ITEM IN SELECTION FIELD
SendMessage (hWndComboBox , LB_SETCURSEL , (WPARAM) 0, (LPARAM) 1);
// put this declaration somewhere up (or better move it to an include file)
#define ID_COMBOBOX_1 1001
#define ID_COMBOBOX_2 1002
// end defines
case WM_CREATE: {
// it is preferably to use SendDlgItemMessage instead of SendMessage
// this make things easier
// you will not need combobox's HWND, just ComboBox ID
TCHAR *Combo_1_Data[]={
TEXT("Item 1"),
TEXT("Item 2")
};
TCHAR *Combo_2_Data[]={
TEXT("Element 1"),
TEXT("Element 2")
};
int i;
// create two different ComboBoxs
CreateWindow (TEXT("COMBOBOX"), TEXT (""),
CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
100, 150, 200, 150,
hwnd ,(HMENU) ID_COMBOBOX_1, NULL, NULL);
CreateWindow (TEXT("COMBOBOX"), TEXT (""),
CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
208, 150, 200, 150,
hwnd ,(HMENU) ID_COMBOBOX_2, NULL, NULL);
// Fill first Combo with its Data
for( i = 0 ; i < (sizeof(Combo_1_Data) / sizeof(Combo_1_Data[0]) ) ; i++ ){
SendDlgItemMessage (hwnd,ID_COMBOBOX_1 ,CB_ADDSTRING, 0, (LPARAM) Combo_1_Data[i]);
}
SendDlgItemMessage (hwnd, ID_COMBOBOX_1 , LB_SETCURSEL , (WPARAM) 0, (LPARAM) 1);
// Fill second Combo with its Data
for( i = 0 ; i < (sizeof(Combo_2_Data) / sizeof(Combo_2_Data[0] )) ; i++ ){
SendDlgItemMessage (hwnd,ID_COMBOBOX_2 ,CB_ADDSTRING, 0, (LPARAM) Combo_2_Data[i]);
}
SendDlgItemMessage (hwnd, ID_COMBOBOX_2 , LB_SETCURSEL , (WPARAM) 0, (LPARAM) 1);
Copy and paste the other combobox. Do the same for the 'Send message' function. Then change the hWnd of the second Combobox to hWndListBox. Do the same to 'Send message'.
HWND hWndListBox = CreateWindow (TEXT("COMBOBOX"), TEXT (""),
CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
100, 70, 200, 90,
hwnd ,(HMENU) NULL, NULL, NULL);
HWND hWndComboBox = CreateWindow (TEXT("COMBOBOX"), TEXT (""),
CBS_DROPDOWN| CBS_HASSTRINGS | WS_VSCROLL| WS_VISIBLE | WS_CHILD ,
100, 150, 200, 100,
hwnd ,(HMENU) NULL, NULL, NULL);
SendMessage (
hWndComboBox ,
(UINT) CB_ADDSTRING,
(WPARAM) 0, (LPARAM) TEXT ("Item 2"));
SendMessage (
hWndListBox ,
(UINT) CB_ADDSTRING,
(WPARAM) 0, (LPARAM) TEXT ("Item 1"));
// SEND THE CB_SETCURSEL MESSAGE TO DISPLAY AN INITIAL ITEM IN SELECTION FIELD
SendMessage (hWndComboBox , CB_SETCURSEL , (WPARAM) 0, (LPARAM) 0);
SendMessage (hWndListBox , CB_SETCURSEL , (WPARAM) 0, (LPARAM) 0);

Win 32 API, drawing two child windows with vertical splitter bar

Below is code, which I am using for creating child windows:
case WM_CREATE:
hInst = ((LPCREATESTRUCT) lParam) -> hInstance;
hWnd1 = CreateWindowEx( WS_EX_CLIENTEDGE | WS_EX_LEFT,
"edit", NULL,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | ES_MULTILINE | WS_VSCROLL,
0, 0, 0, 0,
hWnd, (HMENU) 1,
hInst, NULL );
hWnd2 = CreateWindowEx( WS_EX_CLIENTEDGE | WS_EX_LEFT,
"edit", NULL,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | ES_MULTILINE | WS_VSCROLL,
0, 0, 0, 0,
hWnd, (HMENU) 2,
hInst, NULL );
But this code produces horizontal splitter, and I want vertical splitter.
First I thought, if I change height and width parameters, I could create vertical splitter. But it was of no use.
For full code and sample example (in order to save space on SO):
http://old.sumitbirla.com/software/src/splitter.c
So, what is exactly keyword/parameter, which would produce vertical splitter bar.
The code you posted just creates two child windows. It has nothing to do with vertical or horizontal splitter. Handle the WM_SIZE message in the parent window to position and size the child windows for a vertical or horizontal split.

Window creation fails with non-NULL hMenu parameter

In addition to the main window, I'm trying to create another top level window. The problem is that when I'm setting the second window's hMenu parameter to a non-NULL value, it doesn't show up.
e.g:
This window shows up (hMenu == 0)
case IDC_BUTTON_SEND_COMMAND:
{
CreateWindowExW(NULL,
L"CommandWindow", L"Send Command",
WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
100, 100, 600, 400,
NULL,
(HMENU)0,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
break;
}
This window doesn't show up (hMenu == 4)
case IDC_BUTTON_SEND_COMMAND:
{
CreateWindowExW(NULL,
L"CommandWindow", L"Send Command",
WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
100, 100, 600, 400,
NULL,
(HMENU)4,
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
break;
}
I'm using Windows 7.
Passing (HMENU)4 as the hMenu parameter to CreateWindowEx to create a top level window tells the system to attach a menu to it. This menu has the menu handle 4. A menu handle (HMENU) is returned from functions like CreateMenu. If the handle is not a valid HMENU window creation fails.
Your observation, that the window doesn't show up is misleading yourself into believing that the window actually exists. The window doesn't exist, and CreateWindowEx returns NULL. Checking return values is advisable, and calling GetLastError when an API call fails is usually quite helpful.

CWnd::CreateEx() fails after replacing WS_CHILD with WS_POPUP

When user click a button, I would like to bring up another window. So in the OnBnClicked..() function, I added following code:
CWnd* window = new CWnd;
BOOL result = window->CreateEx(WS_EX_CLIENTEDGE, _T("STATIC"), _T("Hi"), WS_CHILD | WS_VISIBLE, 0, 0, 20, 20, m_hWnd, (HMENU)1234);
window->ShowWindow(SW_SHOW);
This works, except it created a child window. What I need is a separate window. So I modified the code by changing "WS_CHILD" to "WS_POPUP", and "m_hWnd" to "NULL".
CWnd* window = new CWnd;
BOOL result = window->CreateEx(WS_EX_CLIENTEDGE, _T("STATIC"), _T("Hi"), WS_POPUP | WS_VISIBLE, 0, 0, 20, 20, NULL, (HMENU)1234);
window->ShowWindow(SW_SHOW);
But now, the CreateEx() call returns FALSE. I would appreciate some pointer.
[Edit:]
I updated the code as following. But still seeing the same issue. Also, even when CreateEx() returns FALSE, GetLastError() actually returns 0.
CWnd* window = new CalibrationWindow;
LPCTSTR className = NULL;
if (!className) {
className = AfxRegisterWndClass(
CS_VREDRAW | CS_HREDRAW,
::LoadCursor(NULL, IDC_ARROW),
(HBRUSH) ::GetStockObject(WHITE_BRUSH),
::LoadIcon(NULL, IDI_APPLICATION));
}
BOOL isValid = ::IsWindow(GetSafeHwnd());
BOOL result = window->CreateEx(0, _T(className), _T("Hi"), WS_POPUP | WS_VISIBLE, 0, 0, 20, 20, m_hWnd, (HMENU)1234);
DWORD errorCode;
if (!result) {
errorCode = GetLastError();
}
window->ShowWindow(SW_SHOW);
The nID or nIDorHMenu parameter for CWnd::CreateEx is an 'overloaded' parameter that either specifies a control ID or a menu ID.
For a child window (WS_CHILD window styles) it is the control identifier. You can specify any value as long as it is unique among all child windows with the same parent window.
For a popup window on the other hand (a window that does have WS_CHILD window style) it is the menu identifier. In this case it has to be a valid HMENU. Passing a made up value will cause window creation to fail.
Here's how to create a window out of your CWnd-derived class using a different method:
CWndDerived myWnd;
LPCTSTR className = AfxRegisterWndClass(CS_VREDRAW | CS_HREDRAW,
nullptr,
(HBRUSH) ::GetStockObject(WHITE_BRUSH),
nullptr);
myWnd.CreateEx(
0, // WS_EX_TOPMOST
className,
_T("Title"),
WS_POPUP | WS_BORDER | WS_CAPTION | WS_MAXIMIZEBOX | WS_SYSMENU,
0, 0, 300, 200,
m_hWnd,
nullptr,
nullptr);
This allows you to have your CWndDerived myWnd as a member variable (should you need it) of your other window and retain all the goodies that come with MSVC's MFC class editor.
I hope this helps

Wrong symbols in the buffer using GetWindowText

There is combo box with two items and button on the main window. Combobox:
HWND hCombo;
hCombo = CreateWindow(L"COMBOBOX", L"combobox",
WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST,
10, 55, 232, 500, hWnd, 0, hInstance, 0);
const wchar_t *langEnglish = L"English";
const wchar_t *langRussian = L"Russian";
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)langEnglish);
SendMessage(hCombo, CB_ADDSTRING, 0, (LPARAM)langRussian);
SendMessage(hCombo, CB_SETCURSEL, 0, 0);
I am trying to get selected item text in the WndProc by clicking on the button:
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDC_BUTTON_OK:
wchar_t buf[10];
hCombo = GetDlgItem(hWnd, IDC_COMBO);
GetDlgItemText(hCombo, IDC_COMBO, (LPWSTR)buf, 10);
MessageBox(hWnd, (LPCWSTR)buf, NULL, MB_OK);
break;
}
} break;
I am using breakpoint in the MSVS2010 to see buf variable. It contains chinese symbols!!! Message box shows empty message (With the title "Error"). I want to see english text. What is wrong?
This code
nIndex = SendMessage(hCombo, CB_GETCURSEL, 0, 0);
SendMessage(hCombo, CB_GETLBTEXT, nIndex, (LPARAM)buf);
fills buf with the same chinese symbols
SOLUTION:
hCombo = CreateWindow(L"COMBOBOX", L"combobox",
WS_CHILD | WS_VISIBLE | CBS_SORT | CBS_DROPDOWNLIST,
10, 55, 232, 500, hWnd, (HMENU)IDC_COMBO, hInstance, 0);
In order to get currently selected item from CBS_DROPDOWNLIST styled combo box you need CB_GETCURSEL to get selection index and then CB_GETLBTEXT to get the string.