怎么样在DBGrid添加图片?(85)

  • 主题发起人 主题发起人 terry_zhou82
  • 开始时间 开始时间
T

terry_zhou82

Unregistered / Unconfirmed
GUEST, unregistred user!
我的代码如下1.procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; 2. DataCol: Integer; Column: TColumn; State: TGridDrawState); 3.var 4. Bmp: TBitmap; 5.begin 6. DBGrid1.Canvas.FillRect(Rect); 7. if Column.FieldName = 'photo' then 8. begin 9. try 10. Bmp := TBitmap.Create; 11. Bmp.LoadFromFile('c:/down.bmp'); 12. DBGrid1.Canvas.StretchDraw(Rect,Bmp); 13. finally 14. Bmp.Free; 15. end; 16. end; 17.end; 18.end. 但为什么我添加进来的图片会占用很大的列,文件本身很小啊16x16的,我也调整了。DBGRID里面的列宽。还不行,而且添加的图片为什么也不是透明的,头大啊。
 
难道就没有高手?
 
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);var Bmp: TBitmap; OutRect: TRect; BmpWidth: Integer;begin // default output rectangle OutRect := Rect; if Column.Field = Table1Common_Name then begin // draw the image Bmp := TBitmap.Create; try Bmp.Assign (Table1Graphic); BmpWidth := (Rect.Bottom - Rect.Top) * 2; OutRect.Right := Rect.Left + BmpWidth; DBGrid1.Canvas.StretchDraw (OutRect, Bmp); finally Bmp.Free; end; // reset output rectangle, leaving space for the graphic OutRect := Rect; OutRect.Left := OutRect.Left + BmpWidth; end; // red font color if length > 100 if (Column.Field = Table1Lengthcm) and (Table1Lengthcm.AsInteger > 100) then DBGrid1.Canvas.Font.Color := clRed; // default drawing DBGrid1.DefaultDrawDataCell (OutRect, Column.Field, State);end;这段代码来自于mastering delphi6(delphi6从入门到精通),效果很好,建议好好研究一下
 
摘自网上: procedure TForm10.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;DataCol: Integer; Column: TColumn; State: TGridDrawState);var Im1: TBitmap; staff_id: string;beginIm1:=TBitmap.Create;if (dm.AQ_personal.RecordCount = 0) then exit;if Column.Field = dm.AQ_personal.FindField('autoid') then// if Column.Field = dm.AQ_personal.FindField('工号') thenbegin with DBGrid1.Canvas do begin Brush.Color:=clinfoBk; FillRect(Rect); staff_id:=dm.AQ_personal.FindField('staff_id').Asstring; if ((dm.AQ_personal.FindField('work_type').Asstring)='离职类别') then ImageList2.GetBitmap(0,Im1); if ((dm.AQ_personal.FindField('work_type').Asstring)='试用期员工') then ImageList2.GetBitmap(1,Im1); if ((dm.AQ_personal.FindField('work_type').Asstring)='未填在离职') then ImageList2.GetBitmap(2,Im1); if ((dm.AQ_personal.FindField('work_type').Asstring)='在职类别') then ImageList2.GetBitmap(3,Im1);// TextOut(Rect.Right-2-DBGrid1.Canvas.TextWidth(staff_id),Rect.Top+1,staff_id);// Draw(round((Rect.Left+Rect.Right-Im1.Width)/5),Rect.Top,Im1); Draw(round((Rect.Left+Rect.Right-Im1.Width)/2),Rect.Top,Im1); end;end;
 
上面的例子看不太懂'staff_id','work_type'怎么会有2个字段,'staff_id'我还看的懂,但'work_type'字段是什么啊
 
另外还有,我的图片只有16X16的,我把DBGRID的宽度也设为20,但是为什么一执行,他的列宽有会变得很宽,使图片都拉伸了。怎么样才可以固定下他的列宽呢?
 
上面的‘工号’是不是指数据库的表的名称啊?
 
后退
顶部