form1中有stringgrid1和listbox1,listbox1中有若干项记录如下:
----------
|1       |
|4       |
|7       |
|9       |
|14      |
----------   若干项。如何在stringgrid1重画的时候使这些项居中显示。谢谢!
在线等!!

解决方案 »

  1.   

    //当然现在假设stringgrid1中的行足够多。
    //以下代码用于建立环境,方面大家测试。procedure TForm1.FormCreate(Sender: TObject);
    var
      r,c:integer;
    begin
       for r:=0 to stringgrid1.RowCount-1 do
        for c:=0 to stringgrid1.ColCount-1 do
          stringgrid1.Cells[c,r]:=inttostr(r)+' 行';
    end;procedure TForm1.stringgrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
    Rect: TRect; State: TGridDrawState);
    var
      area:trect;
    begin
    stringgrid1.Canvas.Font.Assign(stringgrid1.Font);
       with stringgrid1,stringgrid1.Canvas do
       begin
         FillRect(Rect);
         Area:= Rect;
         InflateRect(Area, -2, -2);
         if (arow=0) then
            DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_LEFT)
         else if (arow=1) then
            DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_CENTER)
         else if (arow<>1) then
            DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_RIGHT);
         end;
    end;
      

  2.   

    用 stringgrid1 的
     Canvas.TextHeight()
     Canvas.TextWidth()
     還有Cell 的寬度, 高度來計算, 就可得到第一個字符應該顯示的位置
      

  3.   

    應該大概如下:
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      s: string;
      w, h: integer;
    begin
      with StringGrid1 do
      begin
        s := Cells[ACol, ARow];
        w := Canvas.TextWidth(s);
        h := Canvas.TextHeight(s);
        if (ACol <> 0) and (ARow <> 0) then
        begin
          if ACol mod 2 = 0 then
            Canvas.Brush.Color := clYellow
          else
            Canvas.Brush.Color := clGreen;
          Canvas.FillRect(Rect);
          Canvas.TextOut((Rect.Right - Rect.Left - w) div 2, (Rect.Bottom - Rect.Top) div 2, s);
        end;
      end;
    end; 
      

  4.   

    //使用DrawText方便一些
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      S: string;
    begin
      with TStringGrid(Sender) do
      begin
        S := Cells[ACol, ARow];
        Canvas.FillRect(Rect);
        DrawText(Canvas.Handle, PChar(S), Length(S), Rect,
          DT_CENTER or DT_VCENTER or DT_SINGLELINE);
      end;
    end;
      

  5.   

    TO aiirii(ari-爱的眼睛) :
       可能是我表达得不正确,让你误解我的意思了
     
    我其实只是想根据listbox1中有的1 4 7 9 14 这些数字,让stringgrid的这些行的字符居中。listbox1中有若干项记录如下:
    ----------
    |1       |
    |4       |
    |7       |
    |9       |
    |14      |
    ----------   若干项
      

  6.   

    当然LISTBOX1里的数据不一定只有5项!
      

  7.   

    我真的還不是很明白, 代碼中有一句:
      s := Cells[ACol, ARow];如果你是想, 是 1 4 7 9 14 这些数字就居中, 那麼,
    那句判斷:
    if ACol mod 2 = 0 then
    可改為:
    if (s=1) or (s=4) ... then如果你是想是數字的, 就居中, 那麼可:
    if tryStrToInt(s, i) then ...如果你是想, 只有其中的某一行居中而已, 那可:
    if ACol = 0 then ....
      

  8.   

    TO aiirii(ari-爱的眼睛) :
       我是想只有其中的某一行居中而已, 那可:
         if ACol = 0 then ....
    的确是这样,但不知道代码怎么写,因为是要根据listbox1里的数据来判断哪些行居中
      

  9.   

    我要先取得listbox1中的1 4 7 9 17 ,然后strtoint
    然后再把这些行居中,但这个IF 语句比想象中难很多