MFC:ON_COMMAND 函数中的混乱?

发布于 2024-11-04 14:01:57 字数 554 浏览 1 评论 0原文

好的,这就是这个函数

 
.
.
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 技术交流群。

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

发布评论

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

评论(2

睫毛上残留的泪 2024-11-11 14:01:57

我不知道该代码的作用。主要是因为 m_nCurrentColor = nID _ ID_COLOR_RED 无法编译。 nIDID_COLOR_RED 之间有一个下划线 (_)。这对编译器来说没有任何意义。您是否想输入减号 (-)?

但更一般地说,ON_COMMAND是用于处理WM_COMMAND消息。该宏有两个参数:

  • id,这是命令 ID
  • memberFxn,它是命令映射到的消息处理函数的名称

它看起来像你'一切都已设置完毕。所有三个命令 ID(红色、绿色和蓝色)均由同一 OnColor 函数处理。

因此,让我们看一下 WM_COMMAND 消息的文档。它表示 wParamlParam 参数的含义取决于消息的来源。它们具有不同的含义,具体取决于用户是否从菜单中选择了某个项目、键入了加速键或控件正在向其父窗口发送通知消息。

我无法从您的问题中真正判断出那些 ID_COLOR_RED (及其兄弟)对应于哪一个。
但这并不重要。无论哪种方式,代码看起来都像尝试设置一个成员变量 (m_nCurrentColor),该变量根据 ID 跟踪用户当前选择的颜色发送最后一个通知的项目的名称。如果我们假设这是一个减号,那么事情就开始变得焦点起来:

代码正在做的是获取发送消息的项目的 ID(nID),并从中减去集合中的第一个值 (ID_COLOR_RED)。这意味着如果 nID = ID_COLOR_REDm_nCurrentColor 将为 0。

如果 ID_COLOR_REDID_COLOR_GREEN 的值ID_COLOR_BLUE连续的(这是一个很大的if,这是你不应该编写这样的代码的一个很好的理由),那么如果 nID = ID_COLOR_GREENm_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 (_) between nID and ID_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 process WM_COMMAND messages. The macro takes two parameters:

  • id, which is the command ID
  • memberFxn, which is the name of the message-hander function to which the command is mapped

It 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 the wParam and lParam 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 if nID = ID_COLOR_RED then m_nCurrentColor will be 0.

If the values of ID_COLOR_RED, ID_COLOR_GREEN, and ID_COLOR_BLUE are sequential (and this is a big if, a good reason why you shouldn't write code like this), then if nID = ID_COLOR_GREEN, m_nCurrentColor will be 1. Likewise, if nID = ID_COLOR_BLUE, then m_nCurrentColor will be 2.

最丧也最甜 2024-11-11 14:01:57

这个答案是我对这个问题的评论的延续。

对于您的示例,我将在链接函数内使用 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 a switch(nID) inside the linked function. You wouldn't be worried if the ID of some option eventually changed.

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