Page 1 of 1

combobox that show sample style text!

Posted: Tue Jan 17, 2012 7:56 am
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.

Posted: Tue Jan 17, 2012 1:08 pm
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));

  

Posted: Tue Jan 17, 2012 6:23 pm
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]);