AdvStringGrid单元格如何添加图片,并且点击图片触发一个事件?
不是单元格背景添加图片,而是在单元格里面加一行字,然后换行加一个图片,然后点击这个图片触发一个事件。
请问这个功能这样能实现吗?
如果不能实现,把图片换成字,也就是第一行是XXXXX字,第二行是XXXX字并有下划线,然后点击第二行的字,触发一个事件。这样能否实现呢?
分不是很多,全部送上。
AdvStringGrid.Cells[2,3] := 'Here we have the '#13'<img src="idx:0" ></img>';//这个图片是ImageList里面的

解决方案 »

  1.   

    procedure TForm1.AdvStringGrid1DrawCell(Sender: TObject; ACol,
     ARow: Integer; Rect: TRect; State: TGridDrawState);
    var
     d: TBitmap;
    Const
     Path= 'C:\Documents and Settings\Administrator\桌面\a.bmp';
    begin
     d:= TBitmap.Create;
     d.LoadFromFile(Path);
     if (ACol = 1) and (ARow = 1)  then
     begin
       AdvStringGrid1.Canvas.Draw(Rect.Left,Rect.Top,d);
     end;
     d.Free;
    end; 
      

  2.   

    你这个是向单元格添加背景图片,我要的是第二行单独加个图片,
    而且点击图片触发一个事件(showmessage(‘test’))//这个是最重要的。
    AdvStringGrid能实现这个功能吗?
    我上网查了好久
      

  3.   

    把圖標畫上去(stringgrid也可以把圖標畫進單元格內),至於事件你得研究一下(比如說用hook到圖片元素再觸發事件)
    procedure TForm1.AdvStringGrid1DrawCell(Sender: TObject; ACol,
     ARow: Integer; Rect: TRect; State: TGridDrawState);
    var
     d: TBitmap;
    Const
     Path= 'C:\01.bmp';
    begin
     d:= TBitmap.Create;
     //d.LoadFromStream(imagelist2);
     d.LoadFromFile(Path);
     if (ACol = 2) and (ARow = 2)  then
     begin
       AdvStringGrid1.Canvas.Draw(Rect.Left,Rect.Top,d);
       AdvStringGrid1.Canvas.Draw(Rect.right-16,Rect.Top,d);
       AdvStringGrid1.Canvas.Draw(Rect.left,Rect.top+16,d);
       AdvStringGrid1.Canvas.Draw(Rect.right-16,Rect.top+16,d);
    //    AdvStringGrid1.Canvas.Draw(Rect.left,Rect.bottom,d);
    //    AdvStringGrid1.Canvas.Draw(Rect.right-16,Rect.bottom,d);
     end;
     d.Free;end; 
      

  4.   

    stringgrid的procedure TSMTXB_F.strg1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      bitmap:TBitmap;
    begin
      WITH Sender AS TStringGrid,Canvas DO
      begin
        SetTextAlign(Handle,TA_LEFT);
        if ElasticForm1.fResize>0 then
          Font.Size:=round(24*ElasticForm1.fResize)
        else
          Font.Size:=24;
        if (ARow=1)or(ARow=2) then
          Font.Color:=clBlue;
        if (ARow=1)and(Cells[ACol,1]<>'') then
        begin
          qry_temp.Close;
          //重點工單:qry_temp.SQL.Text:='select qty from WKO_ORDER_WO where (WO_Old='+QuotedStr(LeftStr(Cells[ACol,1],6))+' or WO_New='+QuotedStr(LeftStr(Cells[ACol,1],6))+') and IsZD=1';
          qry_temp.SQL.Text:='select top 1 WKO_TYPE from WKO_ORDER where WKO_ORDERNO='+ QuotedStr(LeftStr(Cells[ACol,1],6));
          qry_temp.Open;
          if qry_temp.FieldByName('WKO_TYPE').AsString='WT' then
            Font.Color:=clRed;
          qry_temp.Close;
        end;
        if (ARow=2)or(ARow=3) then
        begin
          if ElasticForm1.fResize>0 then
            Font.Size:=Round(24*ElasticForm1.fResize)
          else
            Font.Size:=24;
        end;
        if (ARow=5) then
        begin
          if ElasticForm1.fResize>0 then
            Font.Size:=round(24*ElasticForm1.fResize)
          else
            Font.Size:=24;
          Font.Color:=clGreen;
          if YCC[ACol]='' then
            YCC[ACol]:=Cells[acol,arow] else
          begin
            if YCC[ACol]<>Cells[acol,arow] then
              Brush.Color:=RGB(249,140,149);
            YCC[ACol]:=Cells[acol,arow];
          end;
        end;
        if (ARow=6)and(Cells[ACol,ARow]<>'') then
        begin
          if Cells[ACol,ARow]='0' then
            Brush.Color:=RGB(121,253,154)
          else if StrToInt(Cells[ACol,ARow])<0 then
            Font.Color:=clRed;
        end;
        if (ARow=9)and(Cells[ACol,ARow]<>'') then
        begin
          if IsProgress then
          begin
            if StrToFloat(Copy(Cells[acol,arow],0,Pos('%',Cells[acol,arow])-1))=100.0 then
              Font.Color:=clGreen;
          end else
          begin
            if (StrToFloat(Copy(Cells[acol,arow],0,Pos('%',Cells[acol,arow])-1))<90.0)or(StrToFloat(Copy(Cells[acol,arow],0,Pos('%',Cells[acol,arow])-1))>180.0) then
              Font.Color:=clRed;
          end;
        end;
        FillRect(Rect);
        //if ARow=5 then
        TextRect(Rect,Rect.Left+4,Rect.Top+round(RowHeights[ARow]/2)-17,Cells[aCol,aRow]);
        //else
          //TextRect(Rect,Rect.Left+round(ColWidths[ACol]/2)-2,Rect.Top+round(RowHeights[ARow]/2)-15,Cells[aCol,aRow]);
        if ARow=5 then
        begin
          if Cells[ACol,2]<>'' then
          begin
            qry_temp.Close;
            qry_temp.SQL.Text:='select Qpa from RES_QPA where Lh='+ QuotedStr(Cells[ACol,2]);
            qry_temp.Open;
            if qry_temp.RecordCount=1 then
            begin
              bitmap:=TBitmap.Create;
              case qry_temp.FieldByName('Qpa').AsInteger of
                2:bitmap.Assign(img2.Picture);
                3:bitmap.Assign(img3.Picture);
                5:bitmap.Assign(img5.Picture);
                10:bitmap.Assign(img10.Picture);
                20:bitmap.Assign(img20.Picture);
                50:bitmap.Assign(img50.Picture);
                80:bitmap.Assign(img80.Picture);
                100:bitmap.Assign(img100.Picture);
              end;
              try
                Draw(Rect.Right-16,Rect.Top,bitmap);
              finally
                bitmap.Free;
              end;
            end;
          end;
        end;  
      end;
    end;
      

  5.   

    比如說用hook到圖片元素再觸發事件)
    菜鸟人,看不懂,上网查了好久都没查到相关资料
      

  6.   

    找了一些资料,也想了一些
    下面这个可以实现
    procedure TForm1.AdvStringGrid1CustomCellDraw(Sender: TObject; Canvas: TCanvas;
      ACol, ARow: Integer; AState: TGridDrawState; ARect: TRect; Printing: Boolean);
    var
      Str: String;
      iX,iY: Integer;
    begin
      with TAdvStringGrid(Sender) do
      begin
        if (ARow = 0) and (ACol = 0) then
        begin
          Str := 'adfdsafsaaaaaaaaaaaaaaaaaaaaaaa';
          iX := (ARect.Right + ARect.Left - Canvas.TextWidth(Str)) div 2;
          iY := 10;
          Canvas.Brush.Style := bsClear;
          Canvas.TextOut(iX, iY, Str);
          if ACol = 0 then
          begin
            Canvas.Draw(iX, cntYImg, Image1.Picture.Graphic);
            Canvas.Draw(iX + Image1.Width + 10, cntYImg, Image2.Picture.Graphic);
          end;
        end;
      end;
    end;点击的是通过查找位置来实现的
    var
      IsInImg: Boolean;
      ARect,ARect1,ARect2: TRect;
      APt: TPoint;
      Str: String;
    begin
      with AdvStringGrid1 do
      if (ARow = 0) and (ACol = 0) then
      begin
        Str := 'adfdsafsaaaaaaaaaaaaaaaaaaaaaaa';
        ARect := CellRect(ARow,ACol);
        ARect1.Left := (ARect.Right + ARect.Left) div 2 - Canvas.TextWidth(Str) div 2;
        ARect1.Right := ARect1.Left + Image1.Width;
        ARect1.Top := ARect.Top + cntYImg;
        ARect1.Bottom := ARect1.Top + Image1.Height;    APt := ScreenToClient(Mouse.CursorPos);
        if Windows.PtInRect(ARect1,APt) then
        begin
          ShowMessage('发送短信');
        end;    ARect2 := ARect1;
        ARect2.Left := ARect1.Right + 10;
        ARect2.Right := ARect2.Left + Image2.Width;
        if Windows.PtInRect(ARect2,APt) then
        begin
          ShowMessage('立即通话');
        end;
      end;