How to group radio box buttons using Win32 API - c++

I am working on a program with grouped check box and get confused on how the messages convey through different handles.
IDE:VC++, Win32 API
First, I have a main window which has a handle, say hWnd.
And in the WndProc function under case WM_CREATE, we create the "group button" and the individual 2 check boxes
Note: the first button uses "BS_GROUPBOX" style, and it was created with the handle hGrpButton while its parent handler is hWnd. The second and third button is "BS_AUTORADIOBUTTON" style and it's parent handle is hGrpButton.
If the 2 buttons are not grouped(so their parent hanlder would be hWnd), it is easy to check the status of them. Just simply go to case WM_COMMAND AND use their ID to check with the IsDlgButtonChecked function. After the two check boxes are grouped (their parent handle are not longer hWnd but hGrpButtons), I don't think the case WM_COMMAND will find their IDs since it is looking for the IDs under hWnd.
In short, after 2 check boxes are grouped, I don't know what is the event to monitor them.
case WM_CREATE:
{
/*Group for Radio button for preview/single or batch operation */
hGrpButtons=CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"Select Process Mode:",
WS_VISIBLE | WS_CHILD|BS_GROUPBOX, // Styles
10,280,
350,100,
hWnd,
NULL,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"Batch Process Mode",
WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON, // Styles
10,20,
300,20,
hGrpButtons,
(HMENU)IDC_CHK1,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"Single Process Mode (Preview Mode)",
WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON, // Styles
10,45,
300,20,
hGrpButtons,
(HMENU)IDC_CHK2,
hInst, NULL);

After read Coder_Dan's comment and MSDN article "http://msdn.microsoft.com/en-us/library/bb775947(v=vs.85).aspx#related_topics" about the button types, I finally sort it out.
BS_GROUPBOX is just an "eye candy" and it did no contribute to the group of the radio box!! The critical parameter to control the grouping is the "WS_GROUP"
How to group the radio box: Put the WS_GROUP in the first radio button's style. It will group the current radio button until it see the second WS_GROUP as mentioned by Coder_Dan.
Modification on my previous code
a. use hWnd as parent for all 4 radio buttons
b. Put WS_GROUP in the first and third button style so that we group the 1,2 and 3,4 radio buttons
c. Now you can go to the WM_COMMAND, and listen to the messages from the 4 buttons according to their IDs under the main window's handle hWnd
case WM_CREATE:
{
/*Group for Radio button for preview/single or batch operation */
hGrpButtons=CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"Select Process Mode:",
WS_VISIBLE | WS_CHILD|BS_GROUPBOX,// <----BS_GROUPBOX does nothing on the grouping
10,280,
350,100,
hWnd,
(HMENU)IDC_GRPBUTTONS,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"first radio button",
WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON|WS_GROUP, // <---- WS_GROUP group the following radio buttons 1st,2nd button
10,520,
300,20,
hWnd, //<----- Use main window handle
(HMENU)IDC_CHK1,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"second radio button",
WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON, // Styles
10,545,
300,20,
hWnd,
(HMENU)IDC_CHK2,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"third radio button",
WS_VISIBLE | WS_CHILD|BS_AUTOCHECKBOX|WS_GROUP, //<---Start second group for 3rd,4th button
10,570,
300,20,
hWnd,
(HMENU)IDC_CHK3,
hInst, NULL);
CreateWindowEx(WS_EX_WINDOWEDGE,
L"BUTTON",
L"forth radio button",
WS_VISIBLE | WS_CHILD|BS_AUTORADIOBUTTON, // Styles
500,545,
300,20,
hWnd,
(HMENU)IDC_CHK4,
hInst, NULL);

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

How to put an icon on a text button?

I want to have both an image icon and text on the same button, like here in Word for example.
I set an icon on a button, but the text disappears.
HANDLE hBmp = (HBITMAP)LoadImage(g_hDllInstance,
MAKEINTRESOURCE(IDB_BITMAP4),
IMAGE_BITMAP,
NULL,
NULL,
LR_DEFAULTCOLOR);
HWND hwndButton = CreateWindowEx(
NULL,
_T("BUTTON"),
_T("SOME TEXT"),
BS_BITMAP | WS_VISIBLE | WS_CHILD,
point.x - 47,
point.y - 3,
36,
40,
hWnd,
(HMENU)200,
NULL,
NULL);
SendMessage(
(HWND)hwndButton,
(UINT)BM_SETIMAGE,
(WPARAM)IMAGE_BITMAP,
(LPARAM)hBmp);
I have also tried to set the icon on a smaller subwindow on my button, but for some reason my subwindow is not visible.
Instructions on how to get a button to display both an image and text are outlined in the Button Styles reference1:
The appearance of text or an icon or both on a button control depends on the BS_ICON and BS_BITMAP styles, and whether the BM_SETIMAGE message is sent. The possible results are as follows.
BS_ICON or BS_BITMAP set? | BM_SETIMAGE called? | Result
--------------------------+---------------------+--------------------
Yes | Yes | Show icon only.
No | Yes | Show icon and text.
Yes | No | Show text only.
No | No | Show text only
In other words: Don't set the BS_ICON or BS_BITMAP style (but do set the BS_TEXT style), and send a BM_SETIMAGE message once the button has been created.
To see this in action, create a standard Windows Desktop application in Visual Studio, and apply the following changes:
Enable visual styles. This is easiest done by placing a #pragma linker directive into the only compilation unit:
#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
Create the button in the main window's WM_CREATE handler:
case WM_CREATE:
{
HWND btn{ ::CreateWindowExW(0x0, L"BUTTON", L"Button text",
WS_VISIBLE | WS_CHILD | BS_TEXT,
10, 10, 200, 50, hWnd, (HMENU)110,
nullptr, nullptr) };
HICON icon{ (HICON)::LoadImageW(::GetModuleHandle(nullptr),
MAKEINTRESOURCEW(107),
IMAGE_ICON, 32, 32, 0x0) };
::SendMessageW(btn, BM_SETIMAGE, IMAGE_ICON, (LPARAM)icon);
}
break;
Make sure to adjust the numeric constants as needed. 110 is the button's control identifier, 107 is the resource ID of the wizard-generated application icon resource, and 32 are the width and height of the requested icon.
This code produces the following output:
1 Note, that you have to enable visual styles for this to work.

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.

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

C++ Win32 API Controls messages

im starting with Win32 api, im adding a button control to my main window with the flowing code:
HWND boton = CreateWindow(
"BUTTON", //
"Caption", //
WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, // Styles.
250, // x position.
10, // y position.
100, // Button width.
40, // Button height.
hwnd, // Parent window.
NULL, // No menu.
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
how can i assign it an id, so i can get the message on the loop, in the message loop im trying to catch the message as WM_COMMAND but i don't get any result i've tried with WM_NOTIFY too.
To assign it an ID, you have to use the hMenu parameter. If you have specified that the window will be a child (i.e. with WS_CHILD), the hMenu parameter will be interpreted as an integer ID for the window. Also, provide the BS_NOTIFY style.
HWND boton = CreateWindow (
"BUTTON",
WS_TAPSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON | BS_NOTIFY,
250, 10, 100, 40,
hwnd,
(HMENU)101, // This becomes the Control ID
(HINSTNACE)GetWindowLong(hwnd,GWL_HINSTANCE),
NULL);
EDIT: Special shout goes out to Heath Hunnicutt for the info on BS_NOTIFY.
In fact, you do not need to specify an ID for the button. The problem is your code is missing a style bit to CreateWindow().
You must specify the style BS_NOTIFY for the parent window to receive notifications from Button controls.
You will then receive window message WM_COMMAND with (HIWORD(w_param) == BN_CLICKED) every time your button is clicked. See the BN_CLICKED documentation for more.
Using a control ID is unnecessary because the BN_CLICKED message will provide you with the control's window handle. Because you are already required to keep track of the window handle in order to properly call DestroyWindow when you receive WM_DESTROY comparing the button's window handle is just as easy as using a control ID.
To set the window id, pass it in as if it were an HMENU:
(HMENU) nChildID