本文介绍了简单组合框与位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何把一个位图与风格的组合框设置为简单?例如,谷歌Chrome浏览器的右边都有明星,Firefox已经右边的箭头。我想这code:

 程序TForm2.ComboBox1DrawItem(控制:TWinControl;指数:整数;
RECT:TRect;状态:TOwnerDrawState);
VAR
组合框:TComboBox;
位图:TBitmap;
开始
组合框:=(控制为TComboBox);
位图:= TBitmap.Create;
尝试
ImageList1.GetBitmap(0,位图);
与ComboBox.Canvas做
开始
  FillRect(矩形);
  如果Bitmap.Handle<> 0然后绘制(Rect.Left + 2,Rect.Top,位图);
  矩形:=边界(Rect.Left + ComboBox.ItemHeight + 2,Rect.Top,Rect.Right - Rect.Left,Rect.Bottom - Rect.Top);
  DrawText的(手柄,PChar类型(ComboBox.Items [0]),长(ComboBox.Items [0]),矩形,DT_VCENTER + DT_SINGLELINE);
结束;
最后
Bitmap.Free;
  结束;
结束;

的作品,但只有风格:csOwnerDrawFixed和csOwnerDrawVariable,也是位图仅在项目可见

感谢。


解决方案

To draw your bitmap in the editor of an owner drawn combo box, check for odComboBoxEdit in the owner draw state:

type
  TComboBox = class(StdCtrls.TComboBox)
  private
    FBmp: TBitmap;
    FBmpPos: TPoint;
  protected
    procedure DrawItem(Index: Integer; Rect: TRect;
      State: TOwnerDrawState); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

constructor TComboBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FBmp := TBitmap.Create;
  FBmp.Canvas.Brush.Color := clGreen;
  FBmp.Width := 16;
  FBmp.Height := 16;
end;

destructor TComboBox.Destroy;
begin
  FBmp.Free;
  inherited Destroy;
end;

procedure TComboBox.DrawItem(Index: Integer; Rect: TRect;
  State: TOwnerDrawState);
begin
  TControlCanvas(Canvas).UpdateTextFlags;
  if Assigned(OnDrawItem) then
    OnDrawItem(Self, Index, Rect, State)
  else
  begin
    Canvas.FillRect(Rect);
    if Index >= 0 then
      Canvas.TextOut(Rect.Left + 2, Rect.Top, Items[Index]);
    if odComboBoxEdit in State then
    begin
      Dec(Rect.Right, FBmp.Width + Rect.Left);
      FBmpPos.X := Rect.Right + Rect.Left;
      FBmpPos.Y := (Height - FBmp.Height) div 2;
      if ItemIndex > -1 then
        Canvas.TextOut(Rect.Left + 2, Rect.Top, Items[ItemIndex]);
      Canvas.Draw(FBmpPos.X, FBmpPos.Y, FBmp);
    end;
  end;
end;

In order to draw a bitmap on non-owner drawn combo boxes, you will have to paint in the window of the combo box itself by using a customized WM_NCPAINT handler.

这篇关于简单组合框与位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 12:49