c++ builder中StringGrid控制元件中的資料如何垂直居中
uj5u.com熱心網友回復:
在stringgrid控制元件中使文字內容在cell中做到上下居中經多次實踐,偶終于找到了解決使文字內容在每個cell單元中上下居中的辦法:
就是利用畫板的draw方法,通過“畫”出文字解決了這個問題。哈,不算是旁門左道吧~
實作非常簡單:
設一全句變數: var Iscentre:boolean;
在需要上下居中顯示文字內容時,用陳述句:
cells[x,y]:=, 并置Iscentre為真;
(注意:一定要賦空值!! 原因見下) ~~~~~x,y為區域變數
給Form1上的stringgrid控制元件添加OnDrawCell事件,程序代碼:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if iscentre then
stringgrid1.Canvas.TextOut(rect.left+14,rect.top+8,標題居中 );
end; ~~~~~~~~~~~~~~~~~~~~~~~~~
請根據具體設定調節!
因為此事件被觸發的條件,是發生cell被賦值時, 所以必須先將其置空值以觸發Stringgrid
的onDrawCell事件!
uj5u.com熱心網友回復:
在Draw方法中實作;uj5u.com熱心網友回復:
void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
int ARow, TRect &Rect, TGridDrawState State)
{
DrawText(StringGrid1-> Canvas-> Handle, StringGrid1-> Cells[ACol][ARow].c_str(),
-1, (RECT*)&Rect, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/122232.html
標籤:基礎類
