MFC:ON_COMMAND 函数中的混乱?
好的,这就是这个函数
. . ON_COMMAND (ID_COLOR_RED, OnColor) ON_COMMAND (ID_COLOR_GREEN, OnColor) ON_COMMAND (ID_COLOR_BLUE, OnColor) . . . void CMainWindow::OnColor () { UINT nID = (UINT) LOWORD (GetCurrentMessage ()->wParam); m_nCurrentColor = nID _ ID_COLOR_RED; }
所以,这里 CurrentMessage 的 wParam 的 LOWORD 应该包含消息的 ID,没关系,但是 m_nCurrentColor = nID _ ID_COLOR_RED; 是什么呢?方法? m_nCurrentColor 可以是 0、1 或 2,分别表示红色、绿色或蓝色...
因此,首先我们在第一个语句中将消息的 ID 转换为 UINT,但是我们在第二个语句中尝试使用 m_nCurrentColor = nID _ ID_COLOR_RED 做什么?
谁能解释一下吗?
Alright, so here's this function
. . ON_COMMAND (ID_COLOR_RED, OnColor) ON_COMMAND (ID_COLOR_GREEN, OnColor) ON_COMMAND (ID_COLOR_BLUE, OnColor) . . . void CMainWindow::OnColor () { UINT nID = (UINT) LOWORD (GetCurrentMessage ()->wParam); m_nCurrentColor = nID _ ID_COLOR_RED; }
So, in here the LOWORD of wParam of CurrentMessage is supposed to contain the Message's ID, that's okay, but what does m_nCurrentColor = nID _ ID_COLOR_RED; means? The m_nCurrentColor can be 0,1, or 2 for red, green, or blue respectively...
So first we convert the Message's ID to UINT in first statement, but what are we trying to do in the second one with m_nCurrentColor = nID _ ID_COLOR_RED?
Can anyone please explain?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道该代码的作用。主要是因为
m_nCurrentColor = nID _ ID_COLOR_RED
无法编译。nID
和ID_COLOR_RED
之间有一个下划线 (_
)。这对编译器来说没有任何意义。您是否想输入减号 (-
)?但更一般地说,
ON_COMMAND
宏是用于处理WM_COMMAND
消息。该宏有两个参数:id
,这是命令 IDmemberFxn
,它是命令映射到的消息处理函数的名称它看起来像你'一切都已设置完毕。所有三个命令 ID(红色、绿色和蓝色)均由同一
OnColor
函数处理。因此,让我们看一下
WM_COMMAND
消息的文档。它表示wParam
和lParam
参数的含义取决于消息的来源。它们具有不同的含义,具体取决于用户是否从菜单中选择了某个项目、键入了加速键或控件正在向其父窗口发送通知消息。我无法从您的问题中真正判断出那些
ID_COLOR_RED
(及其兄弟)对应于哪一个。但这并不重要。无论哪种方式,代码看起来都像尝试设置一个成员变量 (
m_nCurrentColor
),该变量根据 ID 跟踪用户当前选择的颜色发送最后一个通知的项目的名称。如果我们假设这是一个减号,那么事情就开始变得焦点起来:代码正在做的是获取发送消息的项目的 ID(
nID
),并从中减去集合中的第一个值 (ID_COLOR_RED
)。这意味着如果nID
=ID_COLOR_RED
则m_nCurrentColor
将为 0。如果
ID_COLOR_RED
、ID_COLOR_GREEN 的值
和ID_COLOR_BLUE
是连续的(这是一个很大的if,这是你不应该编写这样的代码的一个很好的理由),那么如果nID
=ID_COLOR_GREEN
,m_nCurrentColor
将为 1。同样,如果nID
=ID_COLOR_BLUE
,则m_nCurrentColor
将为 2。I have no idea what that code does. Primarily because
m_nCurrentColor = nID _ ID_COLOR_RED
won't compile. You have an underscore (_
) betweennID
andID_COLOR_RED
. That doesn't mean anything to the compiler. Did you mean to type a minus sign (-
), instead?But more generally, the
ON_COMMAND
macro is used to processWM_COMMAND
messages. The macro takes two parameters:id
, which is the command IDmemberFxn
, which is the name of the message-hander function to which the command is mappedIt looks like you've got that all set up. All three command IDs (red, green, and blue) are being handled by the same
OnColor
function.So let's look at the documentation for the
WM_COMMAND
message. It says that the meaning of thewParam
andlParam
parameters depends on the source of the message. They have different meanings depending on whether the user selected an item from a menu, typed an accelerator keystroke, or a control is sending a notification message to its parent window.I can't really tell from your question which of those
ID_COLOR_RED
(and its brethren) correspond to.But it doesn't really matter. Either way, it looks like the code is attempting to set a member variable (
m_nCurrentColor
) that keeps track of the color that is currently selected by the user, based on the ID of the item that sent the last notification. If we assume that that's a minus sign, things start to come into focus a little bit:What the code is doing is getting the ID of the item that's sending the message (
nID
), and subtracting the first value in the set from it (ID_COLOR_RED
). That means ifnID
=ID_COLOR_RED
thenm_nCurrentColor
will be 0.If the values of
ID_COLOR_RED
,ID_COLOR_GREEN
, andID_COLOR_BLUE
are sequential (and this is a big if, a good reason why you shouldn't write code like this), then ifnID
=ID_COLOR_GREEN
,m_nCurrentColor
will be 1. Likewise, ifnID
=ID_COLOR_BLUE
, thenm_nCurrentColor
will be 2.这个答案是我对这个问题的评论的延续。
对于您的示例,我将在链接函数内使用
ON_COMMAND_EX
宏和switch(nID)
。如果某些选项的 ID 最终发生变化,您也不必担心。This answer is a continuation of my comment to the question.
For your example I would use the
ON_COMMAND_EX
macro with aswitch(nID)
inside the linked function. You wouldn't be worried if the ID of some option eventually changed.