Recent

Author Topic: Paint problems with TCustomControl descendant  (Read 3503 times)

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 592
    • Double Dummy Solver - free download
Paint problems with TCustomControl descendant
« on: November 24, 2015, 07:47:44 am »
I've created a TCard component that's a TCustomControl descendant. Below I show the gist of the Paint procedure. Depending on suit and rank I copy a certain portion of an ImageList into a previously defined BMP and then Draw the BMP. This works perfectly.

I also want to put a small green square on the card and write a number ('1' in this example) in that square. This is not working. What happens is - the square portion becomes transparent. If the card is on a gray background - that's the color of the square. The rest of the card is still what I expect.

This all worked fine in Delphi but I'm doing something wrong for Lazarus. Anyone have an idea?

Code: Pascal  [Select][+][-]
  1.    Deck.CardList.GetBitmap(ord(FSuit) * 15 + FRank, FCardBMP);
  2.    with FCardBMP.Canvas do begin
  3.        Brush.color := clGreen;
  4.        FillRect(Bounds(1, 28, 16, 14));
  5.        Font.Style := [fsBold];
  6.        Font.Color:=clBlack;
  7.        TextOut(2, 28, '1');  
  8.    end;
  9.    Canvas.Draw(0, 0, FCardBMP);  
  10.  
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Paint problems with TCustomControl descendant
« Reply #1 on: November 24, 2015, 08:37:30 am »
Try using TextRect with TextStyle parameter instead of TextOut - that gives you better control of the text - something like this (untested):
Code: Pascal  [Select][+][-]
  1. var
  2.   TextStyle: TTextStyle;
  3.   Text: String;
  4.   S: TSize;
  5.   R: TRect;
  6. ...
  7.  
  8.    Deck.CardList.GetBitmap(ord(FSuit) * 15 + FRank, FCardBMP);
  9.    with FCardBMP.Canvas do begin
  10.        Brush.color := clGreen;
  11.        FillRect(Bounds(1, 28, 16, 14));
  12.        Font.Style := [fsBold];
  13.        Font.Color:=clBlack;
  14.        //TextOut(2, 28, '1');  // remove this
  15.    end;
  16. // insert these lines:
  17.    TextStyle := FCardBMP.Canvas.TextStyle;
  18.    TextStyle.Opaque := True;
  19.  
  20.    Text := '1';
  21.    S := FCardBMP.Canvas.TextExtent(Text); //the size of text
  22.    R := Rect(2, 28, 2 + S.cx, 28 + S.cy);
  23.    FCardBMP.Canvas.TextRect(R, R.Left, R.Top, Text, TextStyle);
  24. /////
  25.  
  26.    Canvas.Draw(0, 0, FCardBMP);
  27.  
             

sysrpl

  • Sr. Member
  • ****
  • Posts: 315
    • Get Lazarus
Re: Paint problems with TCustomControl descendant
« Reply #2 on: November 24, 2015, 08:57:41 am »
TextRect as compared to TextOut.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Paint problems with TCustomControl descendant
« Reply #3 on: November 24, 2015, 12:44:37 pm »
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

sysrpl

  • Sr. Member
  • ****
  • Posts: 315
    • Get Lazarus
Re: Paint problems with TCustomControl descendant
« Reply #4 on: November 24, 2015, 05:00:13 pm »
envy, that documentation is very old. i built new documentation last friday. additoinally i created an incremental search index.

ublock origin warns of malware by default at sourceforge:

http://cache.getlazarus.org/images/sourceforge-malware.png

wikipeida details sourceforge hijackings:

http://cache.getlazarus.org/images/sourceforge-hijack.png
« Last Edit: November 24, 2015, 05:40:07 pm by sysrpl »

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 592
    • Double Dummy Solver - free download
Re: Paint problems with TCustomControl descendant
« Reply #5 on: November 24, 2015, 05:29:45 pm »
Here's my code now - still the same problem. The Rect shows whatever color is behind the card - as if I'm making the Rect transparent.

Code: Pascal  [Select][+][-]
  1.    FCardBMP.Canvas.Brush.color := clGreen;
  2.    FCardBMP.Canvas.FillRect(Bounds(1, 28, 16, 14));
  3.    FCardBMP.Canvas.Font.Style := [fsBold];
  4.    FCardBMP.Canvas.Font.Color := clWhite;
  5.    FCardBMP.Canvas.Brush.Style := bsSolid;
  6.    TextStyle := FCardBMP.Canvas.TextStyle;
  7.    TextStyle.Opaque := true;
  8.    S := FCardBMP.Canvas.TextExtent(Text); //the size of text
  9.    R := Rect(2, 28, 2 + S.cx, 28 + S.cy);
  10.    FCardBMP.Canvas.TextRect(R, R.Left, R.Top,Text, TextStyle)
  11.  
« Last Edit: November 24, 2015, 05:45:25 pm by bobonwhidbey »
Lazarus 3.0RC2, FPC 3.2.2 x86_64-win64-win32/win64

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Paint problems with TCustomControl descendant
« Reply #6 on: November 25, 2015, 01:01:33 pm »
Perhaps you should set FCanvas.Brush.Style before you call FillRect:
Code: Pascal  [Select][+][-]
  1.    FCardBMP.Canvas.Brush.color := clGreen;
  2.    FCardBMP.Canvas.Brush.Style := bsSolid; // now here
  3.    FCardBMP.Canvas.FillRect(Bounds(1, 28, 16, 14));
  4.    FCardBMP.Canvas.Font.Style := [fsBold];
  5.    FCardBMP.Canvas.Font.Color := clWhite;
  6. //   FCardBMP.Canvas.Brush.Style := bsSolid;
  7.    TextStyle := FCardBMP.Canvas.TextStyle;
  8.    TextStyle.Opaque := true;
  9.    S := FCardBMP.Canvas.TextExtent(Text); //the size of text
  10.    R := Rect(2, 28, 2 + S.cx, 28 + S.cy);
  11.    FCardBMP.Canvas.TextRect(R, R.Left, R.Top,Text, TextStyle)
  12.  

zeljko

  • Hero Member
  • *****
  • Posts: 1596
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Paint problems with TCustomControl descendant
« Reply #7 on: November 25, 2015, 01:41:13 pm »
Perhaps you should set FCanvas.Brush.Style before you call FillRect:

No, as soon as you set Color to the brush it sets style to bsSolid.

 

TinyPortal © 2005-2018