如何设置多个单选按钮组以获得正确的 Tab 键顺序和键盘交互 (WIN32)? [英] How to setup multiple radio button groups for proper tab-order and keyboard interaction (WIN32)?

查看:104
本文介绍了如何设置多个单选按钮组以获得正确的 Tab 键顺序和键盘交互 (WIN32)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这不是 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 不是,它只是一个按钮神器).下面描述了如何在不需要分组框的情况下执行此操作.如果您想要一个在功能上有助于此处描述的布局的分组框,请向下转到此答案的 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.

Auto-Radio 按钮banks"通过使用两个关键的窗口样式属性WS_GROUPWS_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. Bank 1:第一个单选按钮应该两者 WS_GROUP |控件样式中的 WS_TABSTOP.其余的单选按钮应该两者都没有,并且必须按同级顺序(这意味着在 DIALOG 脚本中它们立即彼此跟随;在动态创建中它们是顺序创建的).

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

Bank 2:第一个单选按钮应该两者 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"到一个单选按钮组,并使用箭头键来切换选择.然后您再次tab"离开该银行并前往下一个tab-stop.请记住,当您点击 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_GROUP)上插入带有 WS_TABSTOP 的单选按钮控件,也将起作用.我通常发现在不依赖分组框的情况下排列单选按钮更容易.

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:

  1. 图标"分组框,包括 WS_GROUP 样式.
  2. 信息"自动单选按钮,包括 WS_TABSTOP
  3. 所有其他图标"组单选按钮.请勿包含 WS_TABSTOP 或 WS_GROUP.
  4. "Button" 分组框,包括 WS_GROUP 样式.这将关闭当前的控制组并开始下一个.
  5. AbortretryIgnore"自动单选按钮,包括 WS_TABSTOP
  6. 所有其他按钮"组单选按钮.请勿包含 WS_TABSTOP 或 WS_GROUP.
  7. Button"单选按钮之后的下一个控件必须包含 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.

这篇关于如何设置多个单选按钮组以获得正确的 Tab 键顺序和键盘交互 (WIN32)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆