MFC 单选按钮 - DDX_Radio 和 DDX_Control 行为
我有一个 MFC 对话框,其中有两个单选按钮。 我把它们放在一个很好的组中,它们的 IDC 是一个接一个的(RB_LEFT, RB_RIGHT
)。
我想使用 DDX_Radio,这样我就可以使用整数值访问按钮,因此在函数 DoDataExchange
中我调用:
DDX_Radio(pDX, RB_LEFT, mRBLeftRight);
其中 mRBLeftRight
是成员变量integer
类型。 我还需要编辑按钮属性,因此我想使用 DDX_Control
将它们映射到成员变量 mRBLeft
和 mRBRight
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
说实话,这比 JC 的帖子让你相信还要容易。
您需要使用
WS_GROUP
标记组中的第一个单选按钮(在本例中为IDC_RADIO3
)。 您现在可以开始了,它会自动选择IDC_RADIO3
)。现在,为了使
m_Val
保持最新状态,可能值得在组中的所有单选按钮上放置一个单击处理程序。 在该函数内,只需调用UpdateData( TRUE );
m_Val
现在将指向组中单选按钮的索引。TBH Its even easier than JC's post leads you to believe.
You need to mark the FIRST radio button in the group with
WS_GROUP
(In this caseIDC_RADIO3
). You are now good to go and it will automatically selectIDC_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, callUpdateData( TRUE );
m_Val
will now point to the index of the radio button in the group.我在这里找到了答案: http://www.flounder.com/getdlgitem.htm
那里同时使用
DDX_Control
和DDX_Radio
是一个问题:)这是一个解决这个问题的小技巧。I found the answer here : http://www.flounder.com/getdlgitem.htm
There IS a problem with using
DDX_Control
andDDX_Radio
at the same time :) This is a small hack to get around the problem.