3

I am writing a CFrameWnd wrapper and I have this line in the header file :

#define WM_CFW_MESSAGE              (WM_APP + 100)

Is this is a good practice ? Does it require that users of this wrapper will have to remember not to use this particular number (WM_APP + 100) ?

Wartin
  • 1,965
  • 5
  • 25
  • 40
  • If you're concerned about this, one alternative is to use Registered Windows Messages, which are guaranteed to be unique for different strings. http://msdn.microsoft.com/en-us/library/ms644947(v=vs.85).aspx – Nick Meyer Dec 16 '10 at 13:33

3 Answers3

3

No, it's not a good practice. The WM_USER range is more suitable. The WM_APP range is intended for messages that must be understood by multiple window classes in a single program. The WM_USER range is intended for messages that are intended for a single window class.

Therefore, you can safely use values in WM_USER range. You can write #define WM_CFW_MESSAGE (WM_USER+0) because you know that your window class has no other WM_USER messages. When you add a second custom message to the same window class, you can use (WM_USER+1), etcetera. If you implement another window class, it can start at WM_USER+0 again.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

"users of this wrapper will have to remember not to use this particular number" And why would they want to do that. Shouldn't you as the component writer, provide methods, rather than let your user accessing your control directly.

This is an "unwritten" rule that you might want to know. WM_USER. Use this message within you control. Supposed, you write a control and some methods INSIDE YOUR CONTROLS need to alter your control, you might want to use WM_USER.

WM_APP. If your application needs to send message to OBJECTS INSIDE YOU APPLICATION, you can use WM_APP.

RegisterWindowMessage If you have two (more then one) applications which need to communicate each other, you should REGISTER your WINDOWS MESSAGE with RegisterWindowMessage.

Examples. This example uses Delphi

{ WM_USER }
procedure TYourControl.RefreshColor;
begin
  SendMessage(Self.Handle,WM_USER+YourNumber,wParam,lParam);
  { Pay attention to Self.Handle, use WM_USER within YOUR CONTROL }
end;

{ WM_APP }
procedure CheckValue;
var
  IResult: Integer;
begin
  IResult:=SendMessage(OtherForm.Handle,WM_APP+YourConstant,wParam,lParam);
  { Watch OtherForm.Handle }
end;

{ RegisterWindowMessage }
procedure SendCommand(OtherAppHandle: Integer);
var
  MessageNumber: Integer;
  MessageName: ShortString;
begin
  MessageName:='YourMessageName';
  Inc(MessageName[0]);MessageName[Ord(MessageName[0])]:=#0;
  MessageNumber:=RegisterWindowMessage(@MessageName[1]);
  SendMessage(OtherAppHandle,MessageNumber,wParam,lParam);
end;

{ Hope this will help }
Steven
  • 1
  • 1
0

It would only be a problem if users tried to send that message to the window handle of your wrapper.

Ed Noepel
  • 425
  • 3
  • 9