Board index » delphi » Assigning Prect

Assigning Prect

hi,

I have a problem with assigning PRect.

Problem:

Following procedure causes access violation.

Procedure form1.area(sender:tobject);

var
  Tr:tRect;
  Pr:Prect;

begin
   Tr:=rect(0,0,400,400);  // compiled ok ran ok.
   Clipcursor(nil);              // compiled ok and ran ok.

  Pr^:=rect(0,0,400,400);  // Compiled ok but causes access violation.
                                         // without this line the program
runs ok and limits the
                                         // cursor to the top line of the
screen.

  Clipcursor(pr);

end;

Regards
Patrick

 

Re:Assigning Prect


Hi,

The problem you are having is quite easy, the PRect is a pointer, and as
such is not "Created" by simply declaring it (when you are using it is
most probably = nil!).  To "create" a PRect (and destroy it) try the
following:

I am using ClipCursor in some of my own code, and goes a bit like this:
{
Prevent the cursor leaving the client area of this control.

Quote
}

procedure TMyCustomControl.RestrictCursor;
var
  Val : PRect;
begin
  Val := New(PRect);
  try
    //At this point Val is not nil
    Val.TopLeft := ClientToScreen(Point(0,0));
    Val.BottomRight := ClientToScreen(Point(ClientWidth,ClientHeight));
    ClipCursor(Val);
  finally
    Dispose(Val);
  end;
end;

I hope this helps,
Jacko

Other Threads