Why does my Button look like it's from 1990? - c++

I'm writing a Windows GUI app and I have come on the need to have a normal button.
I am using this code to generate the button:
hwnd = CreateWindowEx(
NULL,
"BUTTON",
"Button",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
CW_USEDEFAULT, CW_USEDEFAULT,
60, 25,
parentHwnd, NULL,
GetModuleHandle(NULL), NULL);
I want the button to look like this (Ignore the background colour):
But it ends up looking like this:
I think I'm missing a style or something. What can I do to make it look like the first button?

See Normal looking button with c++ / win32

Related

Text box in menu win32 C++

I have a windows app on which when I do a right click a lot of options are shown in the pop up context menu. How can one place an edit text box, in the context menu so that I can type something (and on the change of text box disable few menu items ) . I tried something like this . This doesn't work.
HWND hWndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("Edit"), TEXT("test"),
WS_CHILD | WS_VISIBLE, 100, 20, 140,
20, NULL, NULL, NULL, NULL);
AppendMenu(MyLocalRightClick, MF_STRING, (UINT)hWndEdit,NULL);

buttons are all highlighted

I have two buttons. Each button is highlighted instead of just the first one. How do I make the first button to be only the one highlighted?
BTW This is the code for the two buttons:
openFileButton = CreateWindowA("button", "Open File and start hashing", WS_VISIBLE | WS_CHILD | SS_CENTER, 220, 210, 200, 50, hWnd,
(HMENU)BUTTON_OPEN_FILE, NULL, NULL);
SendMessage(openFileButton, WM_SETFONT, my_font, true);
hashFileButton = CreateWindowA("button", "Get the File hash", WS_VISIBLE | WS_CHILD | SS_CENTER, 220, 270, 200, 50, hWnd,
(HMENU)BUTTON_HASH_FILE, NULL, NULL);
SendMessage(hashFileButton, WM_SETFONT, my_font, true);
Tried using SetFocus but got no luck.
First, stop calling the A versions of WinAPI functions. Those went obsolete back in the '90s. Use the W versions instead, with L prefixes on your string literals and wchar_t as your character type.
Second, you are passing the wrong style flags for buttons. Specifically, you are passing SS_CENTER. This is a style for static controls (hence the SS_ prefix), not for buttons. SS_CENTER is #defined in the Windows header files as the value 0x1, which is equivalent to the button style, BS_DEFPUSHBUTTON. (Note that #define macros are not type-safe, so the compiler doesn't know and can't warn you that you're specifying a static-control style when creating a button control.)
The presence of this erroneous BS_DEFPUSHBUTTON style is why your buttons are both appearing highlighted, as if they are the default button in a dialog box. For standard buttons, you want BS_PUSHBUTTON, which is #defined as the value 0x0, and thus equivalent to not passing any style flags to your CreateWindow function. But for readability and self-documenting code, I would strongly recommend explicitly passing BS_PUSHBUTTON.
So, your corrected code becomes:
openFileButton = CreateWindowW(L"button",
L"Open File and Start Hashing",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
220, 210, 200, 50,
hWnd,
(HMENU)BUTTON_OPEN_FILE,
NULL,
NULL);
SendMessage(openFileButton, WM_SETFONT, my_font, true);
hashFileButton = CreateWindowW(L"button",
L"Get the File Hash",
WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON,
220, 270, 200, 50,
hWnd,
(HMENU)BUTTON_HASH_FILE,
NULL,
NULL);
SendMessage(hashFileButton, WM_SETFONT, my_font, true);
You might actually want to make one (and only one) of your buttons into the "default" button using the BS_DEFPUSHBUTTON style. The behavior is easily implemented by calling the IsDialogMessage function within your main message loop (behavior which you would get for "free" by creating a dialog box, with the added boon of the Resource Editor to lay out the controls and set styles).

Use combobox into Listview Items in C++ win32 api

How can I set items of a listview as Combobox in C++ win32 api?
my listview
hWndListView = CreateWindowEx(
WS_EX_CLIENTEDGE,
WC_LISTVIEW,
L"",
WS_VISIBLE | WS_CHILD | LVS_REPORT | LVS_EDITLABELS,
10,
100,
430,
400,
hWndx,
(HMENU)IDC_LIST,
NULL,
NULL);
my combobox
HWND hComboBox2 = CreateWindow(
WC_COMBOBOX,
CMBX_CLASS_NAME,
CBS_DROPDOWN | CBS_HASSTRINGS | WS_CHILD | WS_OVERLAPPED | WS_VISIBLE,
10,
50,
300,
300,
hWndListView,
NULL,
NULL,
NULL);
Is it possible?
thank you!
It's not possible with standard ListView common control. You can implement your own control that does what you want by subclassing standard one, and letting it do the painting, and all other functionality. Your customization then will include responding to clicks, hittesting to figure out where click happened (with LVM_HITTEST), creating and displaying combobox, and updating item/subitem with new selection from combobox.

How to get text from a textbox in the Windows API

I've been scratching my head over this for a week now. I'm using the Windows API and I made a textbox with
editBox = CreateWindowEx(WS_EX_PALETTEWINDOW, TEXT("Edit"), NULL, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER | ES_MULTILINE | ES_LEFT | ES_AUTOVSCROLL, 175, 110, 140, 150, hwnd, (HMENU) ID_TEXT, NULL, NULL);
and a button
button = CreateWindowEx(WS_EX_PALETTEWINDOW, "BUTTON", "Ok", WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 175, 260, 140, 20, hwnd, (HMENU) ID_BUTTON, NULL, NULL);
and I want to be able to click the button and it store what was typed into editBox in a primitive string. Examples are preffered but links and documentation are also very much appreciated! :)
You can use the GetWindowText API function to get the text of an edit control.
For a window with title this function retrieves the title. For an edit control it retrieves the edit control's text. Just ignore any documentation saying that you're limited to 64K or so, if you encounter it (it was once that way).
To detect the button click, process the WM_COMMAND window message in your window procedure; it's sent to the parent window of the button. There is a more sophisticated approach based on reflecting the message back to button, which can then process it itself, and that approach is used in most higher level frameworks. But at the API level, just check WM_COMMAND in the parent window's window procedure.

Change button style on the fly in Win32

Below is part of a working normal win32 application created from scratch, not dialog based. it will show the text on the buttom, not top:
hButtonApply = CreateWindow(
"BUTTON",
"Reset",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_BOTTOM,
95, 130, 120, 40,
hWnd, (HMENU)IDC_BUTTON_RESET, hInstance, NULL);
SendMessage(hButtonApply, BM_SETSTYLE, BS_TOP, TRUE);
this however makes it a radio button:
SendMessage(hButtonApply, BM_SETSTYLE, BS_RADIOBUTTON, TRUE);
same for a resource-defined dialog button control.
Subclassing is acceptable. Owner-Drawn Buttons are not acceptable. Thanks, Haotian Yang
Some window styles can only be set during CreateWindow, I guess this might be one of them.
Did you try SetWindowLong ?
Edit:
This seems to work:
LONG style = GetWindowLong(hBtn,GWL_STYLE);
style = (style & ~BS_BOTTOM) | BS_TOP;
SetWindowLong(hBtn,GWL_STYLE,style);