WM_COMMAND user message macro

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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 = ".cpp" Then
		msg = InputBox ("Write the message ID:")
		If msg <> "" Then
			'func = 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.FindText "AFX_MSG_MAP", dsMatchBackward
			ActiveDocument.Selection.FindText "AFX_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 = "LONG "
			ActiveDocument.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.FindText "AFX_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
		MsgBox "File is not a .cpp file"
	End If
End Sub


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date