Alright, so here's this function
. . ON_COMMAND (ID_COLOR_RED, OnColor) ON_COMMAND (ID_COLOR_GREEN, OnColor)开发者_StackOverflow社区 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?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 IDmemberFxn
, 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.
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.
精彩评论