Results 1 to 1 of 1

Thread: Why does this not draw a rectangle?

  1. #1
    Join Date
    Aug 2009
    Location
    Nova Scotia, Canada
    Posts
    604
    Mentioned
    0 Post(s)
    Quoted
    56 Post(s)

    Default Why does this not draw a rectangle?

    Been reading/messing around with various bitmap/canvas/form tutorials on here. For the life of me I cannot get anything to draw on a Form's canvas. I can draw to a bitmap and slam that over, but not directly to the Form.

    What's wrong with this?
    Simba Code:
    program New;

    var
      Form: TForm;

    procedure InitForm;
    begin
      Form := TForm.Create(nil);
      with Form do
      begin
        Position := poScreenCenter;
        Width := 500;
        Height := 500;
        Caption := 'Form Test';
        BorderStyle := bsDialog;
        Color := clWhite;
        Canvas.Pen.Color := clBlack;
        Canvas.Pen.Width := 1;
        Canvas.Brush.Color := clBlack;
        Canvas.Brush.Style := bsSolid;
        Canvas.Rectangle(100, 100, 200, 200);
      end;
    end;

    procedure ShowFormModal;
    begin
      Form.ShowModal;
    end;

    procedure SafeProcCall(Proc: String);
    var
      V: TVariantArray;
    begin
      SetArrayLength(V, 0);
      ThreadSafeCall(Proc, V);
    end;

    begin
      SafeProcCall('InitForm');
      SafeProcCall('ShowFormModal');
    end.

    Edit: Solved my own problem: As someone answered in another thread the form gets cleared/erased on a repaint. Which means when I show the form (and the form gets repainted) my rectangle gets erased.

    Simba Code:
    procedure OnFormPaint(Sender: TObject);
    begin
      with Form.Canvas do
      begin
        Pen.Color := clBlack;
        Pen.Width := 1;
        Brush.Color := clBlack;
        Brush.Style := bsSolid;
        Rectangle(100, 100, 200, 200);
      end;
    end;

    // Down in InitForm
      Form.OnPaint := @OnFormPaint;
    Last edited by Bixby Sayz; 11-25-2011 at 03:17 AM.
    Never ever approach a computer saying or even thinking "I will just do this quickly".

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •