Seti Net
Seti Net

Reputation: 743

Window Message to Send and recover Boolean and Integer

How can I Post a Windows message that contains a boolean and an integer. I do understand how to Post and Recover strings. This is my code for strings.

procedure TForm5.Button1Click(Sender: TObject);
var
   LParam: string;
   WParam: string;
   pLParam: pChar;
   pWParam: pChar;
begin
   Memo.Clear;
   LParam := 'Now is the time for all good men...';
   Length(LParam);
   GetMem(pLParam, (Length(LParam) + 1) * SizeOf(Char));
   Move(LParam[1], pLParam^, (Length(LParam) + 1) * SizeOf(Char));

   WParam := 'This is the WParam. ';
   Length(WParam);
   GetMem(pWParam, (Length(WParam) + 1) * SizeOf(Char));
   Move(WParam[1], pWParam^, (Length(WParam) + 1) * SizeOf(Char));

   PostMessage(Handle, WM_SETPAUSE_MESSAGE, Integer(pWParam),Integer(pLParam));
end;

and this to recover the text...

procedure TForm5.WMsetPause(var MESSAGE: TMessage) { message WM_SETPAUSE_MESSAGE };
var
 pLParam: pChar;
 pWParam: pChar;
begin
 try
   pLParam := pChar(Message.LParam) ;
   Memo.Lines.Add( pLParam) ;
   Freemem(pLParam);
 except
   on E: Exception do
    Memo.Lines.Add(E.ClassName + ': ' + E.MESSAGE);
 end;

  try
   pWParam := pChar(Message.WParam);
   Memo.Lines.Add(pWParam);
   Freemem(pWParam);
  except
  on E: Exception do
   Memo.Lines.Add(E.ClassName + ': ' + E.MESSAGE);
  end;
 end;

Upvotes: 0

Views: 999

Answers (1)

MBo
MBo

Reputation: 80187

This task is much simpler than string sending.

You don't need to allocate memory for these types, because message parameters are already Integer, and Boolean needs just simple casting.

PostMessage(Handle, WM_MY_MESSAGE, IntegerParam, Integer(BooleanParam));
....
//in WM_MY_MESSAGE handler:
IntVar := Message.WParam;
BooleanVar := Boolean(Message.WParam);

Upvotes: 6

Related Questions