combobox that show sample style text!

General TRichView support forum. Please post your questions here
Post Reply
mohsen24000
Posts: 75
Joined: Fri Jan 06, 2012 8:13 pm

combobox that show sample style text!

Post by mohsen24000 »

hi dears.
how can i implement a combobox that's show sample text for any textstyle?

for example:
This is a sample
This is a sample
This is a sample
This is a sample

thanks a lot.
mohsen24000
Posts: 75
Joined: Fri Jan 06, 2012 8:13 pm

Post by mohsen24000 »

Code: Select all

procedure TForm1.rvcmbDrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  ItemString: string;
begin
  TComboBox(Control).Canvas.FillRect(Rect);
  TComboBox(Control).Canvas.Font.Size:=rvstyle1.TextStyles[index].Size;
  TComboBox(Control).Canvas.Font.Name:=rvstyle1.TextStyles[index].FontName;
  TComboBox(Control).Canvas.Font.Style := rvstyle1.TextStyles[index].Style;
  TComboBox(Control).Canvas.Font.Color := rvstyle1.TextStyles[index].Color;
  ItemString := TComboBox(Control).Items.Strings[Index];
  DrawText(TComboBox(Control).Canvas.Handle, PChar(ItemString), - 1, Rect, DT_WORDBREAK);
end;


procedure TForm1.rvcmbMeasureItem(Control: TWinControl; Index: Integer;
  var Height: Integer);
var
  ItemString: string;
  MyRect: TRect;
  MyImage: TImage;
  MyCombo: TComboBox;
begin
  // Don't waste time with this on Index = -1
  if (Index > -1) then
  begin
    MyCombo := TComboBox(Control);
    // Create a temporary canvas to calculate the height
    MyImage := TImage.Create(MyCombo);
    try
      MyRect := MyCombo.ClientRect;
      ItemString := MyCombo.Items.Strings[Index];
      MyImage.Canvas.Font.Name := rvstyle1.TextStyles[index].FontName;
      MyImage.Canvas.Font.Style := rvstyle1.TextStyles[index].Style;
      MyImage.Canvas.Font.Color:=rvstyle1.TextStyles[index].color;
      MyImage.Canvas.Font.Size:=rvstyle1.TextStyles[index].Size;
      // Calc. using this ComboBox's font size
      Height := DrawText(MyImage.Canvas.Handle, PChar(ItemString),
        - 1, MyRect, DT_CALCRECT or DT_WORDBREAK);
    finally
      MyImage.Free;
    end;
  end;
end;
fill combo box:

Code: Select all

  for i := 0 to rvstyle1.textStyles.Count-1 do 
    rvcmb.AddItem('This is a sample',pointer(i));

  
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

A small note:
instead of

Code: Select all

      MyImage.Canvas.Font.Name := rvstyle1.TextStyles[index].FontName; 
      MyImage.Canvas.Font.Style := rvstyle1.TextStyles[index].Style; 
      MyImage.Canvas.Font.Color:=rvstyle1.TextStyles[index].color; 
      MyImage.Canvas.Font.Size:=rvstyle1.TextStyles[index].Size;
you can write:

Code: Select all

MyImage.Canvas.Font.Assign(rvstyle1.TextStyles[index]);
Post Reply