2012-03-29 6 views
2

내 응용 프로그램에서 stringgrid를 사용합니다. 데이터는 데이터베이스 (백엔드 mysql)에서 가져와 stringgrid에 표시됩니다.문자열 격자 셀에 이미지 삽입

enter image description here

나는 각 행의 상태 셀에 이미지를 삽입 할 수 있습니다. 즉

 if status =online then -->image1 
     else --->image2 

누구나 어떻게해야합니까?

답변

6

OnDrawCell 이벤트를 구현해야합니다.

예 :

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Longint; 
    Rect: TRect; State: TGridDrawState); 
var 
    s: string; 
    aCanvas: TCanvas; 
begin 
    if (ACol <> 1) or (ARow = 0) then 
    Exit; 
    s := (Sender as TStringGrid).Cells[ACol, ARow]; 

    // Draw ImageX.Picture.Bitmap in all Rows in Col 1 
    aCanvas := (Sender as TStringGrid).Canvas; // To avoid with statement 
    // Clear current cell rect 
    aCanvas.FillRect(Rect); 
    // Draw the image in the cell 
    if (s = 'online') then 
    aCanvas.Draw(Rect.Left, Rect.Top, Image1.Picture.Bitmap) 
    else 
    aCanvas.Draw(Rect.Left, Rect.Top, Image2.Picture.Bitmap); 
end; 
+1

+1하지만 난 나쁜 예로서 **와 ** 사용하여 찾을하지만이 영업 이익은 수정할 수 없습니다 것을 의미하지 않는다 ('aCanvas에와 – ComputerSaysNo

+1

변경 : = (Sender as TStringGrid) .Canvas' 그리고'Canvas' 대신'aCanvas'를 써서'with'를 없애십시오. –

+1

@DorinDuminica와 Warren, 당신 말이 맞아요. 내가 with 문을 사용하는 유일한 경우입니다 왜냐하면 나는 helper var을 정의하는 것을 게으 르기 때문이다. 그렇지 않으면 많은 이유로 금지되어야한다. 고마워. –