Экспертная система Delphi.int.ru

Сообщество программистов
Общение, помощь, обмен опытом

Логин:
Пароль:
Регистрация | Забыли пароль?

Delphi.int.ru Expert

Другие разделы портала

Переход к вопросу:

#   

Статистика за сегодня:  


Лучшие эксперты

Подробнее »



Вопрос # 2 139

/ вопрос открыт /

Здравствуйте, уважаемые эксперты! Как повернуть текст в StringGrid на 90 градусов?

alone Вопрос ожидает решения (принимаются ответы, доступен мини-форум)

Вопрос задал: alone (статус: Посетитель)
Вопрос отправлен: 28 ноября 2008, 21:40
Состояние вопроса: открыт, ответов: 1.

Ответ #1. Отвечает эксперт: Мережников Андрей

Здравствуйте, alone!
В приложении пример из DelphiWorld

Приложение:
  1. uses
  2. {...} Grids;
  3.  
  4. type
  5. TForm1 = class(TForm)
  6. StringGrid1: TStringGrid;
  7. procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  8. Rect: TRect; State: TGridDrawState);
  9. end;
  10.  
  11. {...}
  12.  
  13. implementation
  14.  
  15. {...}
  16.  
  17. // Display text vertically in StringGrid cells
  18. // Vertikale Textausgabe in den Zellen eines StringGrid
  19. procedure StringGridRotateTextOut(Grid: TStringGrid; ARow, ACol: Integer; Rect: TRect;
  20. Schriftart: string; Size: Integer; Color: TColor; Alignment: TAlignment);
  21. var
  22. lf: TLogFont;
  23. tf: TFont;
  24. begin
  25. // if the font is to big, resize it
  26. // wenn Schrift zu gro? dann anpassen
  27. if (Size > Grid.ColWidths[ACol] div 2) then
  28. Size := Grid.ColWidths[ACol] div 2;
  29. with Grid.Canvas do
  30. begin
  31. // Replace the font
  32. // Font setzen
  33. Font.Name := Schriftart;
  34. Font.Size := Size;
  35. Font.Color := Color;
  36. tf := TFont.Create;
  37. try
  38. tf.Assign(Font);
  39. GetObject(tf.Handle, SizeOf(lf), @lf);
  40. lf.lfEscapement := 900;
  41. lf.lfOrientation := 0;
  42. tf.Handle := CreateFontIndirect(lf);
  43. Font.Assign(tf);
  44. finally
  45. tf.Free;
  46. end;
  47. // fill the rectangle
  48. // Rechteck fullen
  49. FillRect(Rect);
  50. // Align text and write it
  51. // Text nach Ausrichtung ausgeben
  52. if Alignment = taLeftJustify then
  53. TextRect(Rect, Rect.Left + 2,Rect.Bottom - 2,Grid.Cells[ACol, ARow]);
  54. if Alignment = taCenter then
  55. TextRect(Rect, Rect.Left + Grid.ColWidths[ACol] div 2 - Size +
  56. Size div 3,Rect.Bottom - 2,Grid.Cells[ACol, ARow]);
  57. if Alignment = taRightJustify then
  58. TextRect(Rect, Rect.Right - Size - Size div 2 - 2,Rect.Bottom -
  59. 2,Grid.Cells[ACol, ARow]);
  60. end;
  61. end;
  62.  
  63. // 2. Alternative: Display text vertically in StringGrid cells
  64. // 2. Variante: Vertikale Textausgabe in den Zellen eines StringGrid
  65. procedure StringGridRotateTextOut2(Grid:TStringGrid;ARow,ACol:Integer;Rect:TRect;
  66. Schriftart:String;Size:Integer;Color:TColor;Alignment:TAlignment);
  67. var
  68. NewFont, OldFont : Integer;
  69. FontStyle, FontItalic, FontUnderline, FontStrikeout: Integer;
  70. begin
  71. // if the font is to big, resize it
  72. // wenn Schrift zu gro? dann anpassen
  73. If (Size > Grid.ColWidths[ACol] DIV 2) Then
  74. Size := Grid.ColWidths[ACol] DIV 2;
  75. with Grid.Canvas do
  76. begin
  77. // Set font
  78. // Font setzen
  79. If (fsBold IN Font.Style) Then
  80. FontStyle := FW_BOLD
  81. Else
  82. FontStyle := FW_NORMAL;
  83.  
  84. If (fsItalic IN Font.Style) Then
  85. FontItalic := 1
  86. Else
  87. FontItalic := 0;
  88.  
  89. If (fsUnderline IN Font.Style) Then
  90. FontUnderline := 1
  91. Else
  92. FontUnderline := 0;
  93.  
  94. If (fsStrikeOut IN Font.Style) Then
  95. FontStrikeout:=1
  96. Else
  97. FontStrikeout:=0;
  98.  
  99. Font.Color := Color;
  100.  
  101. NewFont := CreateFont(Size, 0, 900, 0, FontStyle, FontItalic,
  102. FontUnderline, FontStrikeout, DEFAULT_CHARSET,
  103. OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,
  104. DEFAULT_PITCH, PChar(Schriftart));
  105.  
  106. OldFont := SelectObject(Handle, NewFont);
  107. // fill the rectangle
  108. // Rechteck fullen
  109. FillRect(Rect);
  110. // Write text depending on the alignment
  111. // Text nach Ausrichtung ausgeben
  112. If Alignment = taLeftJustify Then
  113. TextRect(Rect,Rect.Left+2,Rect.Bottom-2,Grid.Cells[ACol,ARow]);
  114. If Alignment = taCenter Then
  115. TextRect(Rect,Rect.Left+Grid.ColWidths[ACol] DIV 2 - Size + Size DIV 3,
  116. Rect.Bottom-2,Grid.Cells[ACol,ARow]);
  117. If Alignment = taRightJustify Then
  118. TextRect(Rect,Rect.Right-Size - Size DIV 2 - 2,Rect.Bottom-2,Grid.Cells[ACol,ARow]);
  119.  
  120. // Recreate reference to the old font
  121. // Referenz auf alten Font wiederherstellen
  122. SelectObject(Handle, OldFont);
  123. // Recreate reference to the new font
  124. // Referenz auf neuen Font loschen
  125. DeleteObject(NewFont);
  126. end;
  127. end;
  128.  
  129. // Call the method in OnDrawCell
  130. // Methode im OnDrawCell aufrufen
  131. procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol,
  132. ARow: Integer; Rect: TRect; State: TGridDrawState);
  133. begin
  134.  
  135.  
  136. if ACol = 1 then
  137. StringGridRotateTextOut(StringGrid1, ARow, ACol, Rect, 'ARIAL',
  138. 12,clRed, taLeftJustify);
  139.  
  140. // In the third column: Center the text
  141. // Ausgabe zentriert in der dritten Spalte
  142. if ACol = 2 then
  143. StringGridRotateTextOut(StringGrid1, ARow, ACol, Rect, 'ARIAL', 12, clBlue, taCenter);
  144.  
  145. // In all other columns third row: right align the text
  146. // Ausgabe rechtsbundig in den restlichen Spalten
  147. if ACol > 2 then
  148. StringGridRotateTextOut(StringGrid1, ARow, ACol, Rect, 'ARIAL', 12,clGreen,
  149. taRightJustify);
  150. end;
  151.  
  152. end.
  153.  


Ответ отправил: Мережников Андрей (статус: Абитуриент)
Время отправки: 29 ноября 2008, 08:13
Оценка за ответ: 5


Мини-форум вопроса

Всего сообщений: 2; последнее сообщение — 29 ноября 2008, 11:45; участников в обсуждении: 2.
Вадим К

Вадим К (статус: Академик), 29 ноября 2008, 00:12 [#1]:

Написать собственную прорисовку текста. Иначе - только развернув монитор. А вот если ещё захочеться делать и редактирование боком, то тут будет сложнее.
Галочка "подтверждения прочтения" - вселенское зло.
Gooddy

Gooddy (статус: 3-ий класс), 29 ноября 2008, 11:45 [#2]:

короче в сетке текст стирать и самому вручную рисовать, поворачивать и вставлять:) поздравляю с геморроем.
Чисти код! Чисти код! Чисти код!

Чтобы оставлять сообщения в мини-форумах, Вы должны авторизироваться на сайте.

Версия движка: 2.6+ (26.01.2011)
Текущее время: 22 февраля 2025, 11:48
Выполнено за 0.03 сек.