简单组合框与位图 [英] ComboBox Simple with Bitmap
问题描述
我如何把一个位图与风格的组合框设置为简单?例如,谷歌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,也是位图仅在项目可见
。感谢。
...也是位图仅在项目可见。
块引用>要绘制位图的所有者绘制组合框的编辑,在主人检查
odComboBoxEdit
绘制状态:
键入
TComboBox =类(StdCtrls.TComboBox)
私人的
FBmp:TBitmap;
FBmpPos:TPoint;
保护
程序DRAWITEM(索引:整数;矩形:TRect;
状态:TOwnerDrawState);覆盖;
上市
构造函数创建(AOwner:TComponent);覆盖;
析构函数销毁;覆盖;
结束;构造TComboBox.Create(AOwner:TComponent);
开始
继承创建(AOwner);
FBmp:= TBitmap.Create;
FBmp.Canvas.Brush.Color:= clGreen;
FBmp.Width:= 16;
FBmp.Height:= 16;
结束;析构函数TComboBox.Destroy;
开始
FBmp.Free;
继承破坏;
结束;程序TComboBox.DrawItem(索引:整数;矩形:TRect;
状态:TOwnerDrawState);
开始
TControlCanvas(Canvas)的.UpdateTextFlags;
如果分配(的OnDrawItem),然后
的OnDrawItem(自考,指数,矩形,州)
其他
开始
Canvas.FillRect(矩形);
如果索引> = 0,则
Canvas.TextOut(Rect.Left + 2,Rect.Top,项目[指数]);
如果odComboBoxEdit在状态,那么
开始
DEC(Rect.Right,FBmp.Width + Rect.Left);
FBmpPos.X:= Rect.Right + Rect.Left;
FBmpPos.Y:=(身高 - FBmp.Height)DIV 2;
如果的ItemIndex>然后-1
Canvas.TextOut(Rect.Left + 2,Rect.Top,项目[的ItemIndex]);
Canvas.Draw(FBmpPos.X,FBmpPos.Y,FBmp);
结束;
结束;
结束;为了吸取非所有者绘制组合框的位图,你将不得不使用一个定制的
WM_NCPAINT
处理组合框本身的窗口作画。How do I put a bitmap in a combobox with style set to simple? For example, Google Chrome has the star on the right, Firefox has the arrow on the right. I tried this code:
procedure TForm2.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); var ComboBox: TComboBox; bitmap: TBitmap; begin ComboBox := (Control as TComboBox); Bitmap := TBitmap.Create; try ImageList1.GetBitmap(0, Bitmap); with ComboBox.Canvas do begin FillRect(Rect); if Bitmap.Handle <> 0 then Draw(Rect.Left + 2, Rect.Top, Bitmap); Rect := Bounds(Rect.Left + ComboBox.ItemHeight + 2, Rect.Top, Rect.Right - Rect.Left, Rect.Bottom - Rect.Top); DrawText(handle, PChar(ComboBox.Items[0]), length(ComboBox.Items[0]), Rect, DT_VCENTER+DT_SINGLELINE); end; finally Bitmap.Free; end; end;
works, but only with style: csOwnerDrawFixed and csOwnerDrawVariable, also the bitmaps are only visible on the items.
Thanks.
解决方案... also the bitmaps are only visible on the items.
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.这篇关于简单组合框与位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!