• 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;
  • 相关阅读:
    TL 重构
    eclipse中使用Lombok
    一个成功的 Git 分支模型
    《重构,改善既有代码的设计》读书笔记
    重构——改善既有代码的设计
    安装Mysql5.7并修改初始密码
    支付宝支付-PC电脑网站支付
    sqlite3 线程模型
    sqlite 常用的一些语句
    Electron 入门第一篇
  • 原文地址:https://www.cnblogs.com/starluck/p/3925863.html
Copyright © 2020-2023  润新知