uses windows, messages, Forms, Classes, SysUtils;
const WM_Negate=WM_User+42;
ErrorCode_Ok=0;
ErrorCode_Fail=-1;
type TWMNegate=packed
record
Msg:Cardinal;
dwIn:Integer;
dwOut:PInteger;
Result:Integer;
end;
type
TNegate=class(TObject)
constructor Create;
Destructor Destroy;
override;
private
FLastResult:Integer;
Fwnd:hWnd;
procedure WndProc(
var msg:TMessage);
procedure DoNegate(
var msg:TWMNegate);
message WM_Negate;
procedure setValue(value:Integer);
public
procedure DefaultHandler(
var msg);
override;
function Negate(value:Integer):Integer;
property LastResult:Integer
read FLastResult;
property Value:Integer
write setValue;
end;
implementation
{ TNegate }
constructor TNegate.Create;
begin
inherited;
Fwnd:=allocatehwnd(WndProc);
end;
procedure TNegate.DefaultHandler(
var msg);
var lmsg:TMessage;
begin
inherited;
lmsg:=TMEssage(msg);
lmsg.Result:=DefWindowProc(Fwnd,lmsg.Msg,lmsg.WParam,lmsg.LParam);
end;
destructor TNegate.Destroy;
begin
DeAllocateHWnd(Fwnd);
inherited;
end;
procedure TNegate.DoNegate(
var msg: TWMNegate);
begin
try
//Negiert wird natürlich mittels Bitoperationen (B-1 Komplement)
msg.dwOut^:=cardinal(msg.dwIn)
XOR $FFFFFFFF + 1;
msg.Result:=ErrorCode_ok;
except
msg.result:=ErrorCode_Fail;
//Exception to ErrorCode
end;
end;
function TNegate.Negate(value: Integer): Integer;
begin
if sendmessage(Fwnd,WM_Negate,
value,integer(@result))=ErrorCode_Fail
then
raise Exception.Create('
Idiot');
FLastResult:=Result;
end;
procedure TNegate.setValue(value: Integer);
begin
if sendmessage(Fwnd,WM_Negate,
value,integer(@FLastResult))=ErrorCode_Fail
then
raise Exception.Create('
Idiot');
end;
procedure TNegate.WndProc(
var msg: TMessage);
begin
try
Dispatch(msg);
except
Application.HandleException(self);
end;
end;