0

我有一些 TCard(自定义组件),在重绘时我在卡片上绘制了一些文本。底部和顶部使用了相同的组件。但是底部的 TCards,当我绘制文本时,为文本放置一个白色背景.. 而顶部的 tcards 没有.. 我不确定这是在哪里完成的或为什么。 在此处输入图像描述 他们都使用相同的油漆..

好的,我从添加底部卡片开始。

/shows the form and sets up the cards.
procedure TFGame.FormShow(Sender: TObject);
var i: integer;
num : TValue;
db: widestring;
begin
  Maindir := label1.Caption;   //sets main dir
   db:= maindir;
   waitingforatarget := false;

  //Sets all oponents cards.
  OCardRed.background.loadfromfile(Maindir+'\pics\backcard.jpg');
  //..........


  //sets yoru cards
  Cardlava1.background.loadfromfile(Maindir+'\pics\backcard.jpg');
  Cardlava1.Repaint;
  //....

    getstartingcards;
  end;

入门卡..

// gets all your starting cards and puts them into the corret rows.
procedure TFGame.GetStartingCards;
const
  ManaTypes : array [0..3] of string = ('Lava','Water','Dark','Nature');
var
   i: integer;
   z:integer;
   Cards: TObjectList<Tcard>;
begin
   // showmessage('list being created');
  Cards := TObjectList<TCard>.Create;
  z:=0;
  // add all tcards (Desgin ) to this list in order Lava,water,dark,nature
  cards.Add(cardLava1);
  cards.Add(cardlava2);
  cards.Add(cardlava3);
  cards.Add(cardlava4);
  cards.Add(cardwater1);
  cards.Add(cardwater2);
  cards.Add(cardwater3);
  cards.Add(cardwater4);
  cards.Add(carddark1);
  cards.Add(carddark2);
  cards.Add(carddark3);
  cards.Add(carddark4);
  cards.Add(cardnature1);
  cards.Add(cardnature2);
  cards.Add(cardnature3);
  cards.Add(cardnature4);
 //get data from DB
  for i := 0 to Length(ManaTypes) - 1 do
  begin
   // showmessage('getting manna types');
    with adoquery1 do
    begin
      close;
      sql.Clear;
      sql.Add('SELECT TOP 4 * FROM Cards WHERE Color = "'+ManaTypes[i]+'" ORDER BY Rnd(-(1000*ID)*Time())');
      open;
    end;

 //return the result of everything for giving mana type..
     if adoquery1.RecordCount = 0 then
        Showmessage('Error no cards in db');
     adoquery1.First;

     while not adoquery1.Eof do
     begin
        ///showmessage('writeing query to card '+inttostr(z));
        cards[z].Cname := adoquery1.FieldByName('Name').AsString;
        cards[z].Ccost := adoquery1.Fieldbyname('Cost').AsInteger;
        cards[z].Ctext := adoquery1.FieldByName('Text').AsString;
        cards[z].Ccolor := adoquery1.FieldByName('Color').AsString;
        cards[z].Cinplay := false; //in the play area
        if adoquery1.fieldbyname('Power').asstring <> '' then
            cards[z].Cpower := adoquery1.FieldByName('Power').AsInteger;
        if adoquery1.fieldbyname('Def').asstring <> '' then
            cards[z].Cdef := adoquery1.FieldByName('Def').AsInteger;
        if adoquery1.FieldByName('Type').AsString = 'Spell' then
           cards[z].Cspell := true
        else
           cards[z].Cspell := false;
        if adoquery1.FieldByName('Target').AsString = 'yes' then
            cards[z].SetTargetTrue
        else
            cards[z].settargetfalse;


        //based on color change background
        if cards[z].Ccolor = 'Lava' then
           cards[z].Background.loadfromfile(maindir+'\pics\lava.png');
        if cards[z].Ccolor = 'Water' then
           cards[z].Background.loadfromfile(maindir+'\pics\water.png');
        if cards[z].Ccolor = 'Dark' then
           cards[z].Background.loadfromfile(maindir+'\pics\dark.png');
        if cards[z].Ccolor = 'Nature' then
           cards[z].Background.loadfromfile(maindir+'\pics\nature.png');
       adoquery1.Next;
       cards[z].repaint;
       z:=z+1;
     end;
  end;
end;

这表明底部的卡片 TCard 重绘如下..

{TCard paint procedure}
//------------------------------------------------------------
procedure Tcard.Paint;
//------------------------------------------------------------
  begin
    Canvas.StretchDraw(ClientRect, FBackground.Graphic);
    if FBackground.Graphic = nil then
      begin
        Canvas.Brush.Style := bsClear;
        Canvas.Pen.Style := psDash;
        Canvas.Rectangle(ClientRect);
      end ;
    if (FBackground.Graphic <> nil) and (ownercard = false) then
     begin
      if (CSpell = false) then
       begin
         SetLpower(inttostr(CPower));   //if monster card
         SetLDefence(inttostr(CDef));  //if monster card
       end;
      SetLCost(inttostr(CCost));
      setLText(CText);
     end;
  end;

所有数字的绘图都是这样的..这个是中心数字,但都一样

{Sets and draws the cost to the TCard}
//-------------------------------------------------------------
procedure TCard.SetLCost(value :string); //cost
//-------------------------------------------------------------
  begin
  if fbigcard = false then
    begin
     canvas.font.size := 8;
     Canvas.textout(19,1,inttostr(CCost));
    end
  else
    begin
     canvas.font.size := 12;
     canvas.font.color := TColor($FFFFFF);
     Canvas.textout(65,7,inttostr(CCost));
     canvas.Font.Color := CLBlack;
    end;
  end;

我认为这就是所有代码。如果您需要我遗漏的东西,请告诉我谢谢格伦

4

1 回答 1

3

在您的组件的Paint()方法中,在绘制文本之前,直接设置Canvas.Brush.Style := bsClear或调用SetBkMode(Canvas.Handle, TRANSPARENT)。您当前仅设置Brush.Style如果没有背景图形,但如果有背景图形,则绘制所有文本。

顺便说一句,单独测试Graphic <> nil不足以检查是否指定了背景。您还需要检查Graphic.Empty何时Graphic <> nil

试试这个:

procedure Tcard.Paint;
begin
  Canvas.Brush.Style := bsClear;

  if (FBackground.Graphic <> nil) and (not FBackground.Graphic.Empty) then
  begin
    Canvas.StretchDraw(ClientRect, FBackground.Graphic);
  end else
  begin
    Canvas.Pen.Style := psDash;
    Canvas.Rectangle(ClientRect);
  end;

  if (not ownercard) then
  begin
    if (not CSpell) then
    begin
      SetLpower(IntToStr(CPower));   //if monster card
      SetLDefence(IntToStr(CDef));  //if monster card
    end;
    SetLCost(IntToStr(CCost));
    SetLText(CText);
  end;
end;
于 2013-10-31T00:36:52.587 回答