WM_COMMAND user message macro | CodeGuru

WM_COMMAND user message macro

Here is a nice macro that works very nice if you are working on an MFC project involving user messages. I always got irritated every time I had to add user messages, and sometimes forgot what parameters should be used. This macro pops up a nice dialog box where you just enter your user message […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 1, 2002
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Here is a nice macro that works very nice if you are
working on an MFC project involving user messages.
I always got irritated every time I had to add user
messages, and sometimes forgot what parameters should
be used. This macro pops up a nice dialog box where you
just enter your user message (e.g. WM_MYMESSAGE) and
it inserts the message body + the line in the AFX_MSG_MAP
as well as the line in the header file under AFX_MSG.

It creates the name based on the message, so if it finds
“WM_” in the beginning, this is stripped away and replaced
with the word “On” just like all the other MFC messages
are defined.

It uses another macro you have on your web page called
ToggleHandCPP. It should probably be refined better so
that it doesn’t spew garbage when it is unable to find
the header file.

BTW, you have to be in the CPP file to run the macro,
if not a message box will pop up. (The macro can be
extended to do this switch automatically of course.)

I’ve been using this for a while and it seems to work just
fine. Please report any bugs if you find them.

Sub AddMessage()DESCRIPTION: Adds user messages to MFC project (WM_COMMAND)
	ext = ActiveDocument.Name
	pos = Instr(ext,.)
	if pos > 0 then
		Do While pos <> 1
			ext = Mid(ext, pos, Len(ext)pos + 1)
			pos = Instr(ext,.)
		Loop
		ext = LCase(ext)
	end if
	If ext =.cppThen
		msg = InputBox (Write the message ID:)
		If msg <> “” Thenfunc = LCase(msg)
			If Left(msg, 3) =WM_Then
				func =On+ Mid(msg,4,1) + Mid(LCase(msg),5)
			End If
			ActiveDocument.Selection.EndOfDocument
			ActiveDocument.Selection.FindTextAFX_MSG_MAP, dsMatchBackward
			ActiveDocument.Selection.FindTextAFX_MSG_MAP, dsMatchBackward
			ActiveDocument.Selection.CharRight dsMove, 2
			ActiveDocument.Selection.WordRight dsExtend
			ActiveDocument.Selection.Copy
			ActiveDocument.Selection.EndOfLine
			ActiveDocument.Selection.NewLine
			ActiveDocument.Selection =ON_MESSAGE(+msg+,+func+)ActiveDocument.Selection.EndOfDocument
			ActiveDocument.Selection.NewLine
			ActiveDocument.Selection =LONGActiveDocument.Selection.Paste
			ActiveDocument.Selection =::+func+( UINT uParam, LONG lParam )ActiveDocument.Selection.NewLine
			ActiveDocument.Selection ={ActiveDocument.Selection.NewLine
			ActiveDocument.Selection =return 0;ActiveDocument.Selection.NewLine
			ActiveDocument.Selection.CharLeft
			ActiveDocument.Selection =}ActiveDocument.Selection.NewLine
			ActiveDocument.Selection.LineUp
			ActiveDocument.Selection.LineUp
			ActiveDocument.Selection.LineUp
			ActiveDocument.Selection.EndOfLine
			ActiveDocument.Selection.NewLine
			ToggleHandCPP
			ActiveDocument.Selection.EndOfDocument
			ActiveDocument.Selection.FindTextAFX_MSG, dsMatchBackward
			ActiveDocument.Selection.LineUp
			ActiveDocument.Selection.EndOfLine
			ActiveDocument.Selection.NewLine
			ActiveDocument.Selection =afx_msg LONG+func+( UINT uParam, LONG lParam );ToggleHandCPP
		End If
	Else
		MsgBoxFile is not a .cpp fileEnd If
End Sub
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.