comment:='1';
comment:=comment + #13 + #10 + '2';
StringGrid.cells[1,1]:=comment原意想让它分成两行显示,可是好象没效果,仍然在一行里显示,应该怎样?

解决方案 »

  1.   

    嘻嘻。好解决,看
    const
      { DrawText() Format Flags }
      {$EXTERNALSYM DT_TOP}
      DT_TOP = 0;
      {$EXTERNALSYM DT_LEFT}
      DT_LEFT = 0;
      {$EXTERNALSYM DT_CENTER}
      DT_CENTER = 1;
      {$EXTERNALSYM DT_RIGHT}
      DT_RIGHT = 2;
      {$EXTERNALSYM DT_VCENTER}
      DT_VCENTER = 4;
      {$EXTERNALSYM DT_BOTTOM}
      DT_BOTTOM = 8;
      {$EXTERNALSYM DT_WORDBREAK}
      DT_WORDBREAK = $10;
      {$EXTERNALSYM DT_SINGLELINE}
      DT_SINGLELINE = $20;
      {$EXTERNALSYM DT_EXPANDTABS}
      DT_EXPANDTABS = $40;
      {$EXTERNALSYM DT_TABSTOP}
      DT_TABSTOP = $80;
      {$EXTERNALSYM DT_NOCLIP}
      DT_NOCLIP = $100;
      {$EXTERNALSYM DT_EXTERNALLEADING}
      DT_EXTERNALLEADING = $200;
      {$EXTERNALSYM DT_CALCRECT}
      DT_CALCRECT = $400;
      {$EXTERNALSYM DT_NOPREFIX}
      DT_NOPREFIX = $800;
      {$EXTERNALSYM DT_INTERNAL}
      DT_INTERNAL = $1000;
      {$EXTERNALSYM DT_HIDEPREFIX}
      DT_HIDEPREFIX = $00100000;
      {$EXTERNALSYM DT_PREFIXONLY}
      DT_PREFIXONLY = $00200000;  {$EXTERNALSYM DT_EDITCONTROL}
      DT_EDITCONTROL = $2000;
      {$EXTERNALSYM DT_PATH_ELLIPSIS}
      DT_PATH_ELLIPSIS = $4000;
      {$EXTERNALSYM DT_END_ELLIPSIS}
      DT_END_ELLIPSIS = $8000;
      {$EXTERNALSYM DT_MODIFYSTRING}
      DT_MODIFYSTRING = $10000;
      {$EXTERNALSYM DT_RTLREADING}
      DT_RTLREADING = $20000;
      {$EXTERNALSYM DT_WORD_ELLIPSIS}
      DT_WORD_ELLIPSIS = $40000;      begin
            TempR := StringGrid1.CellRect(ACol,ARow);
            FillRect(TempR);//清除原有内容
            Canvas.Font.Assign(FontDialog1.Font);
            DrawText(Handle, PChar(Cells[ACol, ARow]),
              Length(Cells[ACol, ARow]), R,  DT_SINGLELINE or//这是单行的,换Ing....
              FormatView_V );
            //Canvas.Font.Color := ColorBox1.Color;
          end;
      

  2.   

    参考贴子。
    http://expert.csdn.net/Expert/topic/1525/1525860.xml?temp=.5110132
      

  3.   

    DT_EXPANDTABS = $40;
    DT_EXPANDTABS or 40
      

  4.   

    ihihonline(小小->记好啊.有空多来灌水:
     你好,能不能稍微解释一下啊,你的代码是什么事件的?上面一堆的const又是什么啊?
      

  5.   

    是我自定义的一个事件,不过,你也可以写在OnDraw事件里;
    那些Const就是显示格试的常量.....
      

  6.   

    Const不要你写的啊,那是系统自己的,为了让你更明白我才贴上的
      

  7.   

    FillRect(TempR);  通不过编译,参数不对  :(
    DrawText(Handle, PChar(Cells[ACol, ARow]),
              Length(Cells[ACol, ARow]), R,  DT_SINGLELINE or//这是单行的,换Ing....
              FormatView_V );R又是什么啊?
      

  8.   

    还有到底写在哪个事件里啊?onDrawCell吗?
      

  9.   

    在StringGrid的OnDrawCell事件中:
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      with StringGrid1 do begin
        Canvas.FillRect(Rect);
        DrawText(GetDC(Handle),
                 PChar(Cells[ACol, ARow]),
                 Length(Cells[ACol, ARow]),
                 Rect,
                 DT_WORDBREAK);
      end;
    end;然后你的
    comment:='1';
    comment:=comment + #13 + #10 + '2';
    StringGrid.cells[1,1]:=comment;
    就能换行了
      

  10.   

    Tstringgrid.ondrawcell事件:
    DrawText(StringGrid1.Canvas.Handle,pchar(StringGrid1.Cells[Acol,Arow]),Length(StringGrid1.Cells[Acol,Arow]),Rect,DT_WORDBREAK or DT_LEFT);//文字分行
      

  11.   

    procedure TForm1.FormatToPaint;
      Function Result_Value(Temp_Array : Array of Boolean) : Integer;
      var
        I , J :Integer;
      begin
        J := 0;
        For I := Low(Temp_Array) to High(Temp_Array) do
        begin
          if Temp_Array[I] then
          Inc(J);
        end;
        Result := J;
      end;
    var
      Index ,TempValue : Integer;
    begin
      TempValue := 0;
      Result_R := Nil;
      SetLength(Result_R , Result_Value(TempArray_R));
      For Index := Low(TempArray_R) to High(TempArray_R) do
      begin
        if TempArray_R[Index] then
        begin
          Result_R[TempValue] := Index;
          Inc(TempValue);
        end;
      end;
      {---------------------------垂直------------------------------------------}
      TempValue := 0;
      Result_C := Nil;
      SetLength(Result_C , Result_Value(TempArray_C));
      For Index := Low(TempArray_C) to High(TempArray_C) do
      begin
        if TempArray_C[Index] then
        begin
          Result_C[TempValue] := Index;
          Inc(TempValue);
        end;
      end;
    end;procedure TForm1.SetGridViewFormat(ColArray_R: array of Integer;
      ColArray_C : Array of Integer);
      Function R_ResultV(ColArray_RV : Array of Integer;
        ColArray_CV : Array of Integer):Integer;
      var
        Rslt : Integer;
      begin
        Rslt := 0;
        if High(ColArray_RV) - Low(ColArray_RV) > 0 then
        begin
          SetLength(Result_R , High(ColArray_RV) - Low(ColArray_RV) + 1);
          Inc(Rslt);
        end;
        if High(ColArray_CV) - Low(ColArray_CV) > 0 then
        begin
          Inc(Rslt);
          SetLength(Result_C , High(ColArray_CV) - Low(ColArray_CV) + 1);
        end;
        Result := Rslt;
      end;
    Var
      Index : Integer;
      C_Result : String;
    begin
      //..
      if R_ResultV(ColArray_R,ColArray_C) = 2 then
      begin
        FFormat := 3;
        For Index := Low(ColArray_R) to High(ColArray_R) do
        begin
          Result_R[Index] := ColArray_R[Index];
        end;
        For Index := Low(ColArray_C) to High(ColArray_C) do
        begin
          Result_C[Index] := Result_C[Index];
        end;
      end else
      if R_ResultV(ColArray_R,ColArray_C) = 1 then
      begin
        if  high(ColArray_R) + 1 = Length(ColArray_R) then 
        begin
          For Index := Low(ColArray_R) to High(ColArray_R) do
          begin
            Result_R[Index] := ColArray_R[Index];
            FFormat := 2;////////居右
          end;
        end else
        begin
          For Index := Low(ColArray_C) to High(ColArray_C) do
          begin
            Result_C[Index] := ColArray_C[Index];
            FFormat := 1; 
          end;
        end;
      end;
    end;
      

  12.   

    procedure TForm1.DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState; StringGrid: TStringGrid);
    var
      R: TRect;
      Index ,Index_Grid : Integer;
      TempR : TRect;
    begin
      with TStringGrid(StringGrid) do
      begin
        with TStringGrid(StringGrid),TStringGrid(StringGrid).Canvas do
        begin
          R := Rect;//R用来限定文字显示区域
          InflateRect(R, -2, -2);//为文字边缘留些空隙      //TextRect(Rect, Rect.Left + 2, Rect.Top + 2, Cells[ACol, ARow]);
          begin
            TempR := StringGrid1.CellRect(ACol,ARow);
            FillRect(TempR);//清除原有内容
            Canvas.Font.Assign(FontDialog1.Font);
            DrawText(Handle, PChar(Cells[ACol, ARow]),
              Length(Cells[ACol, ARow]), R,  DT_SINGLELINE or
              FormatView_V );
            //Canvas.Font.Color := ColorBox1.Color;
          end;
          //************************************************************************
          For Index_Grid := 0 to StringGrid1.ColCount do
          begin
            if FormatRC(Index_Grid) then
            begin          if FFormat = 3 then
              begin
                For Index := Low(Result_R) to High(Result_R) do
                begin
                  if ACol = Result_R[Index] then
                  begin
                    TempR := StringGrid1.CellRect(Result_R[Index],ARow);
                    FillRect(TempR);//清除原有内容
                    if ACol = Result_R[Index] then
                    begin
                      DrawText(Handle, PChar(Cells[ACol, ARow]),
                      Length(Cells[ACol, ARow]), R, DT_RIGHT or DT_SINGLELINE or
                      FormatView_V )
                    end;
                  end;
                end;
                For Index := Low(Result_C) to High(Result_C) do
                begin
                  if ACol = Result_C[Index] then
                  begin
                    TempR := StringGrid1.CellRect(ACol,ARow);
                    FillRect(TempR);//清除原有内容
                    if ACol = Result_C[Index] then
                    begin
                      DrawText(Handle, PChar(Cells[ACol, ARow]),
                      Length(Cells[ACol, ARow]), R,DT_CENTER  or DT_SINGLELINE or
                      FormatView_V)
                    end;
                  end;
                end;
              end else
              if FFormat = 2 then
              begin
                For Index := Low(Result_R) to High(Result_R) do
                begin
                  if ACol = Result_R[Index] then
                  begin
                    TempR := StringGrid1.CellRect(Result_R[Index],ARow);
                    FillRect(TempR);//清除原有内容
                    if ACol = Result_R[Index] then
                    begin
                      DrawText(Handle, PChar(Cells[ACol, ARow]),
                      Length(Cells[ACol, ARow]), R, FFormat or DT_SINGLELINE or
                      FormatView_V);
                    end;
                  end;
                end;
              end else
              if FFormat = 1 then
              begin
                For Index := Low(Result_C) to High(Result_C) do
                begin
                  if ACol = Result_C[Index] then
                  begin
                    TempR := StringGrid1.CellRect(Result_R[Index],ARow);
                    FillRect(TempR);//清除原有内容
                    if ACol = Result_C[Index] then
                    begin
                      DrawText(Handle, PChar(Cells[ACol, ARow]),
                      Length(Cells[ACol, ARow]), R, 1 or DT_SINGLELINE or
                      FormatView_V)
                    end;
                  end;
                end;
              end ;
            end else
            begin
              //if (FFormat <> 2) and (FFormat <> 3) and (FFormat<>1) then
             { begin
                TempR := StringGrid1.CellRect(ACol,ARow);
                FillRect(TempR);//清除原有内容
                DrawText(Handle, PChar(Cells[ACol, ARow]),
                        Length(Cells[ACol, ARow]), R,  DT_SINGLELINE or
                        FormatView_V );
              end;     }
            end;
          end;
        end;
      end;
    end;
      

  13.   

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      DrawCell(Sender,ACol,ARow,Rect,State,StringGrid1);
    end;procedure TForm1.SpeedButton13Click(Sender: TObject);
    begin
      FormatView_V := 0;//显示控制,0上、8下、4、居中
      StringGridPaint;
    end;procedure TForm1.SpeedButton14Click(Sender: TObject);
    begin
      FormatView_V := 4;//显示控制,0上、8下、4、居中
      StringGridPaint;
    end;procedure TForm1.SpeedButton15Click(Sender: TObject);
    begin
      FormatView_V := 8;//显示控制,0上、8下、4、居中
      StringGridPaint;    
    end;我写的处理过程.....
    给你了很多的功能.....