2011-09-14 38 views
1

我已經將圖標放在字符串網格中,但是我遇到了並非所有圖形都對齊的問題。我試圖重新將文字居中以使圖標對齊,但沒有運氣。我試圖研究位圖及其功能,但我沒有(所以我認爲)發現任何可以幫助我的東西。任何人都可以幫助我嗎?字符串網格和單元格中的圖形

EDIT(從錯誤的答案添加到問題的代碼):

bitmap := Tbitmap.Create; 
bitmap.LoadFromFile('equal.bmp'); 
bitmap.SetSize(150,60); 
stringgrid1.Canvas.StretchDraw(stringgrid1.CellRect(3,J), bitmap); 
SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER); 
StringGrid1.Canvas.TextRect(stringgrid1.CellRect(3,J), 
    (stringgrid1.CellRect(3,J).Left+stringgrid1.CellRect(3,J).Right) div 2, 

stringgrid1.CellRect(3,J).Top + 5,StringGrid1.Cells[3,J]); 
SetTextAlign(StringGrid1.Canvas.Handle, TA_LEFT); 
+1

歡迎StackOverflow上。你可以編輯你的問題來添加TStringGrid.OnDrawCell事件處理程序到目前爲止你已經獲得的代碼的屏幕截圖,所以我們可以看到你想要解決的問題是什麼?這會讓您更好地回答問題。謝謝。 :) –

+1

將代碼從下面的答案移動到這一個。如果這是您的實際代碼,那是非常錯誤的,並且似乎來自OnDrawCell事件處理程序以外的其他位置。另外,請編輯您的文章並添加屏幕截圖,以瞭解您的代碼目前的功能,因爲我們沒有'equal.bmp'或者瞭解您的網格佈局。 (如果您使用用於發佈原始問題的相同ID登錄,則可以編輯;您應該編輯以澄清或添加新信息,而不是發佈答案。) –

回答

3

下面是一個例子(Delphi 7中,因爲這是我不得不派上用場,但代碼應在D2010工作):

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; 
    Rect: TRect; State: TGridDrawState); 
var 
    Bmp: TBitmap; 
    CellText: string; 
    R: TRect; 
const 
    L_PAD = 5; // Amount between right side of image and start of text 
    T_PAD = 5; // Amount between top of cell and top of text 
begin 
    // Some text to display in cells. 
    CellText := Format('Row: %d Col: %d', [ARow, ACol]); 

    // Draw an image along the left side of each cell in the first 
    // col (not the fixed ones, which we'll leave alone) 
    if ((ACol = 1) or (ACol = 3)) and (ARow > 0) then 
    begin 
    Bmp := TBitmap.Create; 
    try 
     Bmp.LoadFromFile('C:\glyfx\common\bmp\24x24\favorites24.bmp'); 
     if ACol = 1 then // left align image 
     begin 
     R.Top := Rect.Top + 1; 
     R.Left := Rect.Left + 1; 
     R.Right := R.Left + Bmp.Width; 
     R.Bottom := R.Top + Bmp.Height; 
     StringGrid1.Canvas.StretchDraw(R, Bmp); 
     StringGrid1.Canvas.TextOut(R.Right + L_PAD, R.Top + T_PAD, CellText); 
     end 
     else 
     begin // right align image 
     StringGrid1.Canvas.TextOut(Rect.Left + L_PAD, 
            Rect.Top + L_PAD, 
            CellText); 
     R.Top := Rect.Top + 1; 
     R.Left := Rect.Right - Bmp.Width - 1; 
     R.Right := Rect.Right - 1; 
     R.Bottom := R.Top + L_PAD + Bmp.Height; 
     StringGrid1.Canvas.StretchDraw(R, Bmp); 
     end; 
    finally 
     Bmp.Free; 
    end; 
    end 
    else 
    StringGrid1.Canvas.TextOut(Rect.Left + L_PAD, Rect.Top + T_PAD, CellText); 
end; 

這裏是什麼樣子:

StringGrid with image aligned

相關問題