Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > Delphi программирование > Компоненты Delphi
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 06.02.2011, 18:47   #1
Alter
Старожил
 
Аватар для Alter
 
Регистрация: 06.08.2007
Сообщений: 2,183
Стрелка ComboBox wordwrap

Как бы заставить корректно отображать текст с переносами(физически #13#10 в тексте нет).

Код:
type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    procedure FormCreate(Sender: TObject);
    procedure ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
      var Height: Integer);
    procedure ComboBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
  private
    { Private declarations }
  public
    A :string;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  S :string;
begin
 A := IncludeTrailingBackslash(ExtractFilePath(ParamStr(0)));
 S := A + 'SampleText.txt';
 if FileExists(S) then
  ComboBox1.Items.LoadFromFile(S);
 Caption := Format('ItemCount: %d', [ComboBox1.Items.Count]);
  //
 ComboBox1.Anchors := ComboBox1.Anchors + [akRight];
 ComboBox1.ItemHeight := 16;
 ComboBox1.DropDownCount := 10;
 ComboBox1.Style := csOwnerDrawVariable;
end;

procedure TForm1.ComboBox1MeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
var
  StrVal, StrTmp :string;
  I,L,ChL :Integer;
  Cb :TComboBox;
  R :TRect;
begin
 if Index >= 0 then
 begin
  Cb := (Control as TComboBox);
   // v1
//  with Cb do
//  begin
//   StrVal := Items.Strings[index];
//   DrawText(Canvas.Handle,
//            PAnsiChar(StrVal),
//            Length(StrVal),
//            R,
//            DT_LEFT or DT_TOP or DT_EXTERNALLEADING or
//            Dt_NoPrefix or DT_WORDBREAK or DT_CALCRECT
//           );
//  end;
//  Height := R.Bottom - R.Top;
  // v2
  with Cb do
  begin
    StrVal := Items.Strings[Index];
    ChL := Canvas.TextWidth('W');
    L := ClientWidth - 16 - 2;
    If Canvas.TextWidth(StrVal) <= L Then Exit;
    StrTmp := WrapText(StrVal, L div ChL);
    I := 1;
    while Pos(#$D#$A, StrTmp) > 0 do
    begin
     Inc(I);
     strTmp := Copy(strTmp, Pos(#13#10, strTmp) + 2, Length(strTmp));
    end;
  end;
  Height := I * Cb.ItemHeight + I*2;
 end;
end;

procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  R :TRect;  
begin
 With (Control as TComboBox) do
 begin
  if odSelected in State then
   Canvas.Brush.color := clMoneyGreen
  else
   Canvas.Brush.color := clWindow;
  Canvas.FillRect(Rect);    
  R := Rect;
  R.Left := R.Left + 16 + 2;
  SetBKMode(Canvas.Handle, TRANSPARENT);
  DrawText(Canvas.Handle,
           PAnsiChar(Items.Strings[index]),
           Length(Items.Strings[index]),
           R,
           DT_LEFT or DT_TOP or DT_EXTERNALLEADING or
           Dt_NoPrefix or DT_WORDBREAK
          );
 end;
end;
Вложения
Тип файла: rar WBCB.rar (7.7 Кб, 7 просмотров)
Alter вне форума Ответить с цитированием
Старый 06.02.2011, 19:55   #2
ArtGrek
DelphiProger
Участник клуба
 
Аватар для ArtGrek
 
Регистрация: 14.11.2010
Сообщений: 1,023
По умолчанию

может поможет

http://programmersforum.ru/showthread.php?t=126990
post #2
VirusN13
ArtGrek вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Вернуть число строк полученных в Label при WordWrap:=true; MyLastHit Общие вопросы Delphi 2 04.01.2011 13:49
Проблема с WordWrap y Label MyLastHit Общие вопросы Delphi 3 02.01.2011 23:17
Разделение ComboBox на 3 ComboBox-a artemavd Общие вопросы Delphi 1 21.07.2010 08:02
WordWrap SKS Общие вопросы Delphi 1 25.03.2009 17:30
Не могу внести строку из combobox в combobox!? tacer Помощь студентам 1 30.11.2007 19:45