Change button style on the fly in Win32 - c++

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);

Related

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).

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.

WinApi : Add style to combo box

I'm trying to add CBS_OWNERDRAWFIXED style to an existing combobox , my code doesn't work and I have no idea why .
I suspect maybe the expression oldStyle | addedStyle is not valid , but I can't figure out why.
HWND hwnd = CreateWindow(
L"ComboBox",
L"",
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST ,
200,
200,
200,
200,
parentHandle,
(HMENU)1,
GetModuleHandle(NULL),
NULL);
auto comboBoxStyle = GetWindowLongPtr(hwnd, GWL_STYLE);
comboBoxStyle = comboBoxStyle | CBS_OWNERDRAWFIXED;
SetWindowLongPtr(hwnd, GWL_STYLE, comboBoxStyle);
the output is just regular combobox , without any change.
This particular style can only be specified at creation time. The documentation alludes to this when it says:
CBS_OWNERDRAWFIXED
Specifies that the owner of the list box is responsible for drawing its contents and that the items in the list box are all the same height. The owner window receives a WM_MEASUREITEM message when the combo box is created and a WM_DRAWITEM message when a visual aspect of the combo box has changed.

C++ Button with transparent background

I'm creating a basic application in C++ (Win32 API). I tried to make a button with the CreateWindow() function as seen below:
button1 = CreateWindow("button", TEXT("Click Me!"), WS_TABSTOP | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 100, 200, 70, 25, hwnd, (HMENU)NULL, NULL, NULL);
The problem is that there is a white border around the button as you can see here:
Link
I tried to make it transparent with this code but is doesn't work.
case WM_CTLCOLORBTN:{
HBRUSH hBrushbtn;
hBrushbtn = (HBRUSH)GetStockObject(NULL_BRUSH);
SetBkMode((HDC) wParam, TRANSPARENT);
return ((LRESULT)hBrushbtn);
break;
}
How can I do this?
Thanks
SetBkMode() affects whether or not text rendering is transparent or not, which is not the issue here.
You're going to have to WM_PRINTCLIENT up to the parent control into the button's DC, as I demonstrate here. (Note that my code still calls SetBkMode() for transparent text in checkboxes, groupboxes, labels, etc.; for pushbuttons it won't matter if you're using visual styles).

How to make an icon button in C++

I know how to draw a button in C++ but how would i make an icon on it can someone post source or give reference please? by SendMessage() or if not that way just please paste
Please need easier anwsers without so many files im new a bit
Since you're new, you may also wish to consult the MSDN Library. You can find information on Button Styles (see, specifically, the BS ICON and BS BITMAP styles) and the BM_SETIMAGE message .
If you use MFC then I would recommend you to use the following CButton method SetIcon:
CButton myButton;
// Create an icon button.
myButton.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_ICON,
CRect(10,10,60,50), pParentWnd, 1);
// Set the icon of the button to be the system question mark icon.
myButton.SetIcon( ::LoadIcon(NULL, IDI_QUESTION) );
This works very well.
send BM_SETIMAGE message, and pass loaded image handle to lParam.
button1 = CreateWindowW(L"BUTTON", L"&Button", WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_BITMAP, 20, 50, 80, 25, hwnd, (HMENU) 600, NULL, NULL);
hImg = LoadImageW(NULL, L"test123.bmp", IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_LOADFROMFILE);
SendMessageW(button1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hImg);
P.S: you need to use BS_BITMAP flag when CreateWindow()