MFC 单选按钮 - DDX_Radio 和 DDX_Control 行为

发布于 2024-07-27 21:12:45 字数 1232 浏览 1 评论 0原文

我有一个 MFC 对话框,其中有两个单选按钮。 我把它们放在一个很好的组中,它们的 IDC 是一个接一个的(RB_LEFT, RB_RIGHT)。

我想使用 DDX_Radio,这样我就可以使用整数值访问按钮,因此在函数 DoDataExchange 中我调用:

DDX_Radio(pDX, RB_LEFT, mRBLeftRight);

其中 mRBLeftRight 是成员变量integer 类型。 我还需要编辑按钮属性,因此我想使用 DDX_Control 将它们映射到成员变量 mRBLeftmRBRight (CButton):

DDX_Control(pDX, RB_LEFT, mRBLeft);
DDX_Control(pDX, RB_RIGHT, mRBRight);

现在,如果我调用 DDX_Control,每当调用 DoDataExchange 时,应用程序都会崩溃,因为 DDX_Control 强制 RB_LEFT 处理 DDX_Radio 无法处理的消息。 这部分我明白了。

我决定不使用 DDX_Control(删除了 DoDataExchange 中的调用),只在我的类中保留指向单选按钮 (CButton*) 的指针。 因此,在我的 OnInitDialog 函数中,我执行了以下调用:

mRBLeft= ((CButton*)GetDlgItem(RB_LEFT));
mRBRight = ((CButton*)GetDlgItem(RB_RIGHT));

现在只要我不使用 mRBLeft 就可以了,但如果我这样做了,砰,崩溃在DoDataExchange上。 真正让我困惑的是,如果我使用以下命令更改左侧单选按钮 ((CButton*)GetDlgItem(RB_LEFT)->SetCheck(true) 它会起作用的。 苏有什么区别?

(我知道这很麻烦,但我只是想了解其中的机制)

I have an MFC dialog in which there are two radio buttons. I have put them in a nice group, their IDCs are one after each other (RB_LEFT, RB_RIGHT).

I want to use DDX_Radio so I can access the buttons using an integer value so in the function DoDataExchange I call :

DDX_Radio(pDX, RB_LEFT, mRBLeftRight);

where mRBLeftRight is a member variable of integer type. I also need to edit the buttons properties so I wanted to use a DDX_Control to map them on member variables mRBLeft and mRBRight (CButton):

DDX_Control(pDX, RB_LEFT, mRBLeft);
DDX_Control(pDX, RB_RIGHT, mRBRight);

Now if I do the call to DDX_Control, whenever DoDataExchange is called, the application crashes because DDX_Control forces RB_LEFT to handle a message that DDX_Radio cannot handle. This part I understand.

I decided to not use DDX_Control (removed the calls in DoDataExchange) and just keep a pointer to my radio buttons (CButton*) in my classes. So in my OnInitDialog function, I do the following calls :

mRBLeft= ((CButton*)GetDlgItem(RB_LEFT));
mRBRight = ((CButton*)GetDlgItem(RB_RIGHT));

Now as long as I don't use mRBLeft it's going to be fine, but if I do, bam, crash on DoDataExchange. The thing that really puzzles me is if I change my left radio button using
((CButton*)GetDlgItem(RB_LEFT)->SetCheck(true)
it's going to work. Sooo what's the difference ?

(I know it's a lot of hassle for little, but I just wanna understand the mechanics)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无尽的现实 2024-08-03 21:12:45

说实话,这比 JC 的帖子让你相信还要容易。

DDX_Control( pDX, IDC_RADIO3, m_r3 );
DDX_Control( pDX, IDC_RADIO4, m_r4 );

m_Val   = 0;
DDX_Radio( pDX, IDC_RADIO3, m_Val );

您需要使用 WS_GROUP 标记组中的第一个单选按钮(在本例中为 IDC_RADIO3)。 您现在可以开始了,它会自动选择IDC_RADIO3)。

现在,为了使 m_Val 保持最新状态,可能值得在组中的所有单选按钮上放置一个单击处理程序。 在该函数内,只需调用 UpdateData( TRUE ); m_Val 现在将指向组中单选按钮的索引。

TBH Its even easier than JC's post leads you to believe.

DDX_Control( pDX, IDC_RADIO3, m_r3 );
DDX_Control( pDX, IDC_RADIO4, m_r4 );

m_Val   = 0;
DDX_Radio( pDX, IDC_RADIO3, m_Val );

You need to mark the FIRST radio button in the group with WS_GROUP (In this case IDC_RADIO3). You are now good to go and it will automatically select IDC_RADIO3).

Now to keep m_Val up to date it is probably worth putting a click handler on all the radio buttons in the group. Inside that function, simply, call UpdateData( TRUE ); m_Val will now point to the index of the radio button in the group.

淡淡的优雅 2024-08-03 21:12:45

我在这里找到了答案: http://www.flounder.com/getdlgitem.htm

那里同时使用DDX_ControlDDX_Radio是一个问题:)这是一个解决这个问题的小技巧。

单选按钮变量

另一个内在问题,
代表着某种奇怪的东西
微软的哲学观点,
是你不能被允许
创建无线电控制变量
纽扣。 这毫无意义。 他们
有一些奇怪的想法,唯一的办法
你将永远操纵单选按钮
是通过索引。 这是无望的
不足。 因此,你必须去
通过一些严重的扭曲
获取无线电的控制变量
按钮。

你要做的第一件事就是去
返回并将所有单选按钮标记为
具有 WS_GROUP 样式。 仅有的
具有 WS_GROUP 样式的单选按钮
可以有一个控制变量。 然而,
如果你将它们全部标记为
WS_GROUP,创建控件
变量,然后删除
WS_GROUP 属性,一切正常
很好,谢谢。 为什么我们必须
完成这些额外的步骤不会
任何意义,但就像派生的
课程问题,我一直在抱怨
多年来一直关注这个问题,但没有任何效果。
我的问题是我总是忘记
返回并撤消所有 WS_GROUP
属性,所以我第一次运行
之后的程序我发现所有
我的单选按钮是一键式的
组。 哎呀。 $#%! 修复,并且
重新编译/重新链接。

I found the answer here : http://www.flounder.com/getdlgitem.htm

There IS a problem with using DDX_Control and DDX_Radio at the same time :) This is a small hack to get around the problem.

Radio button variables

Another intrinsic problem, which
represents some sort of strange
philosophical viewpoint of Microsoft,
is that you must not be allowed to
create control variables for radio
buttons. This makes no sense. They
have some weird idea that the only way
you will ever manipulate radio buttons
is via an index. This is hopelessly
inadequate. Therefore, you have to go
through some serious contortions to
get control variables for your radio
buttons.

The first thing you have to do is go
back and mark all radio buttons as
having the WS_GROUP style. Only
radio buttons with a WS_GROUP style
can have a control variable. However,
if you mark all of them with
WS_GROUP, create the control
variables, and then remove the
WS_GROUP attribute, everything works
just fine, thank you. Why we have to
go through these extra steps makes no
sense whatsoever, but like the derived
classes problem, I've been complaining
about this for years with no effect.
My problem is that I keep forgetting
to go back and undo all the WS_GROUP
attributes, so the first time I run
the program after this I find that all
my radio buttons are one-button
groups. Whoops. $#%! Fix, and
recompile/relink.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文