问题描述
首先,这不是 MFC.
这是我一直在处理的 GUI 的裁剪版本:
First of all this is not MFC.
Here is a cropped version of the GUI I have been working on:
如您所见,我已经(尝试)使用代码创建了两个不同的组,图标和按钮:
As you can see I have (attempted) to create two different groups, Icon and Button, using the code:
index->hAddT.hwndIndex[2] = CreateWindowEx(NULL,L"BUTTON",L"Icon",WS_CHILD | WS_VISIBLE | BS_GROUPBOX,
200,135,120,170,WINDOWHANDLE,(HMENU)IDC_RADIOGROUP,(HINSTANCE)GetWindowLong(WINDOWHANDLE,GWL_HINSTANCE),NULL);
我遇到的问题以及您可能看到的问题是,该窗口只有一个单选按钮.这意味着用户不可能从 Icon 组中选择一个单选按钮,从 Button 组中选择一个.我已经初始化了每个单选按钮:
The issue I have, and what you can probably see, is that there is only one radio button for the window. This means that the user couldn't possibly select one radio button from the Icon group, and one from the Button group. I have initialised each Radio button as so:
index->hAddT.hwndIndex[3] = CreateWindowEx(NULL,L"BUTTON",L"Information",WS_CHILD | BS_AUTORADIOBUTTON | WS_VISIBLE,
205,155,100,20,WINDOWHANDLE,(HMENU)IDC_RADIO1,(HINSTANCE)GetWindowLong(WINDOWHANDLE,GWL_HINSTANCE),NULL);
如果有意义的话,我想以某种方式将图标"单选按钮组与按钮"单选按钮组分开,因此每个组都可以使用一个单选按钮.这怎么可能,是否需要我创建一个新窗口和一个新的回调程序只是为了有一个额外的单选按钮.必须有另一种方法来对子项进行分组.
I would like to, somehow, have the "Icon" group of radio buttons seperate from the "Button" group of radio buttons, if that makes sense, and therefore will have one radio button available to each group. How will this be possible, will it require me to make a new window and a new callback procedure just to have an extra radio button. There must be another way to group child items like so.
2以相同形式 WINAPI(无 MFC)的不同单选按钮组该链接对我的目的没有任何用处.
2 separate groups of radio buttons in the same form WINAPI (No MFC) The link was not of any use for my purpose.
我有我旁边的 Charles Petzold 编写的 Programming For Windows Fifth Edition 作为参考,他在 Group Boxes 部分中指出Group box 通常用于包围其他按钮控件",但没有这方面的真实示例.
I have Programming For Windows Fifth Edition, by Charles Petzold beside me as reference, and he states in the Group Boxes section "Group boxes are often used to enclose other button controls", yet there is not real example of this.
推荐答案
与流行的观点相反,您不需要 groupbox 控件或任何其他此类外部容器"(groupbox 无论如何都不是,它只是一个按钮工件).下面描述了如何在 no 要求 group-box 的情况下做到这一点.如果您想要一个在功能上有助于此处描述的布局的组框,请转发到此答案的 EDIT 部分,我将在其中解释如何实现 OP 的特定愿望.
Contrary to popular opinion, you do NOT need an groupbox control, or any other such outer "container" (which a groupbox isn't anyway, its just a button artifact). The following describes how you can do this with no requirement of a group-box. If you want a group box that functionally assists in the layout described here, forward down to the EDIT portion of this answer, where I explain how the OP's specific desires can be achieved.
自动单选按钮银行"通过使用两个关键窗口样式属性WS_GROUP 和WS_TABSTOP 工作.为您的两个银行"执行以下操作,我将它们亲切地称为 Bank1 和 Bank2:
Auto-Radio button "banks" work by using two key window style attributes, WS_GROUP, and WS_TABSTOP. Do the following for your two "banks" which I will affectionately called Bank1 and Bank2:
银行 1:第一个单选按钮应该有 both WS_GROUP |控件样式中的 WS_TABSTOP.其余的单选按钮应该都没有,并且必须按兄弟顺序排列(这意味着在 DIALOG 脚本中它们立即相互跟随;在动态创建中它们是按顺序创建的).
Bank 1: the first radio button should have both WS_GROUP | WS_TABSTOP in the control style. the remaining radio buttons should have neither of those, and must be in sibling order (meaning in DIALOG script they immediately follow each other; in dynamic creation they are created sequentially).
第一个子控件之后 Bank1 中的最后一个单选按钮应至少具有 WS_GROUP 样式,并且 WS_GROUP |WS_TABSTOP 如果它是制表符停止的控件.
The first child control after your last radio button in Bank1 should have at least WS_GROUP style, and WS_GROUP | WS_TABSTOP if it is a tab-stopped control.
银行 2:第一个单选按钮应该有 both WS_GROUP |控件样式中的 WS_TABSTOP.其余的单选按钮应该都没有,并且必须按兄弟顺序排列(这意味着在 DIALOG 脚本中它们立即相互跟随;在动态创建中它们是按顺序创建的).
Bank 2: the first radio button should have both WS_GROUP | WS_TABSTOP in the control style. the remaining radio buttons should have neither of those, and must be in sibling order (meaning in DIALOG script they immediately follow each other; in dynamic creation they are created sequentially).
第一个子控件之后 Bank2 中的最后一个单选按钮应至少具有 WS_GROUP 样式,并且 WS_GROUP |WS_TABSTOP 如果它是制表符停止的控件.
The first child control after your last radio button in Bank2 should have at least WS_GROUP style, and WS_GROUP | WS_TABSTOP if it is a tab-stopped control.
类似上面的布局允许您制表"到单选按钮库,并使用箭头键切换选择.然后,您再次制表"以离开该银行并前往下一个制表位.请记住,当您点击 Tab(或之前使用 Shift-Tab)时,对话框管理器将始终移动到下一个 WS_TABTOP 子控件.如果要跳转到的控件是自动类型,则选定的控件将是最近 WS_GROUP 中的选定"控件.
Layout like the above allows you to "tab" to a radio button bank, and use the arrow keys to switch selections. You then "tab" again to leave that bank and head to the next tab-stop. Remember, the dialog manager will always move to the next WS_TABTOP child control when you hit Tab (or prior with Shift-Tab). If the control being hopped to is an auto-type the control selected will be the 'selected' control within the most recent WS_GROUP.
如果有帮助,请拿起一个速写本,在纸上画出来,然后在制表位上贴上T",在组属性上贴上G",如上所述.一旦可视化,它可能会更清晰.查看对话资源脚本以了解它们如何协同工作以获得更多信息.
If it helps, grab a sketch pad, draw it on paper, and stick a "T" on the tab-stops and a "G" on the group attributes as described above. It will probably be much clearer once visualized. Look at a dialog resource script to see how these work together for more insight.
注意:如果您想使用围绕这些的分组框,您可以.对话框管理器通过将控件关联到基于使用 WS_GROUP 标记的最后一个控件的组来工作,并且此后具有 WS_TABSTOP 的第一个控件被认为是该组的制表符跳转点.首先插入一个 Groupbox(不能是制表符),然后在第一个单选按钮上插入带有 WS_TABSTOP 的单选按钮控件(这次没有 WS_GROUP),也可以.我通常发现只排列单选按钮而不依赖于分组框更容易.
Notes: If you want to use group boxes surrounding these you can. The dialog manager works by associating controls to groups based on the last control that was flagged with WS_GROUP, and the first control thereafter that has WS_TABSTOP is considered the tab-jump-in point for that group. Inserting a Groupbox first (which can't be a tabstop) followed by the radio button controls with WS_TABSTOP on the first radio button (no WS_GROUP this time), will also work. I generally find it easier to just arrange my radio buttons without dependence on groupboxes.
编辑一张图说一千个字
对于您的图片,我可能会按以下顺序创建以下子项:
For your picture I would probably create the following children in the following order:
- 图标"分组框,包括 WS_GROUP 样式.
- 信息"自动单选按钮,包括 WS_TABSTOP
- 所有其他图标"组单选按钮.不要包含 WS_TABSTOP 或 WS_GROUP.
- 按钮"组框,包括 WS_GROUP 样式.这将关闭当前控制组并开始下一个控制组.
- AbortretryIgnore"自动单选按钮,包括 WS_TABSTOP
- 所有其他按钮"组单选按钮.不要包含 WS_TABSTOP 或 WS_GROUP.
- 按钮"单选按钮之后的下一个控件必须包含 WS_GROUP.这将关闭当前控制组并开始下一个控制组.
显然,所有其他子控件样式、可见性等也需要正确使用,当然子控件都应该具有唯一的 ID.我假设您已经了解了其余的内容.
Obviously all the other child control styles, visibility, etc need to be used correctly as well, and of course the children should all have unique ids. I'm assuming you already have the rest of that covered.
这篇关于如何设置多个单选按钮组以实现正确的选项卡顺序和键盘交互(WIN32)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!