首页 > 代码库 > TcxGrid Column动态添加Image
TcxGrid Column动态添加Image
MyCol := TcxColumn.Create; ... MyCol.PropertiesClass := TcxImageProperties; ImageProps := TcxImageProperties(MyCol.Properties); ImageProps.Center := True; ImageProps.GraphicClassName := ‘‘; ImageProps.OnGetGraphicClass := GetThumbnailGraphicClass; ImageProps.Stretch := True; ...Procedure GetThumbnailGraphicClass:procedure TCORSA.GetThumbnailGraphicClass(AItem: TObject; ARecordIndex: Integer; APastingFromClipboard: Boolean; var AGraphicClass: TGraphicClass);begin if AnsiSAmeText(FThumbNailExtension, ‘.TIF‘) then AGraphicClass := TGraphicClass(GetClass(‘TTiffGraphic‘)) else if AnsiSAmeText(FThumbNailExtension, ‘.JPG‘) then AGraphicClass := TGraphicClass(GetClass(‘TJPEGImage‘))end;The actual thumbnail data is loaded into the grid via streams: MStream := TMemoryStream.Create; Stream := TStringStream.Create(‘‘); MStream.LoadFromFile(ThumbNail); Stream.CopyFrom(MStream, MStream.Size); FActiveGrid.DataController.SetValue(RowInfo.RecordIndex, ThumbCol, Stream.DataString);
改进后的:
function StreamToVar(Stream: TStream): OleVariant;var P: Pointer;begin Result := VarArrayCreate([0, Stream.size -1],Varbyte); P := VarArrayLock(Result); Try Stream.Position := 0; Stream.Read(P^, Stream.size); Finally VarArrayUnlock(Result); end;end;procedure TForm1.Button1Click(Sender: TObject);var IRecIdx : Integer; stream : TMemoryStream;begin with cxGrid1TableView1.DataController do begin IRecIdx := AppendRecord; stream := TMemoryStream.Create(); stream.LoadFromFile(‘H:\pic\随拍\IMAG0002.jpg‘); stream.Position := 0; Values[IRecIdx,0] := StreamToVar(stream); stream.Free; Post; end;end;
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。