【问题标题】:How to save all nodes of TVirtualStringTree to BMP in Delphi 6?Delphi 6中如何将TVirtualStringTree的所有节点保存到BMP?
【发布时间】:2019-09-13 06:59:52
【问题描述】:

我使用 Delphi 6.0,并且我有 TPanel,其中包含 TVirtualStringTree,其中包含许多节点(超过 1 个屏幕)。

我需要将此面板(包含所有节点)作为图片保存到 BMP。

我用这个方法保存面板:

procedure TfrmDidTreeTime.cmSaveGantClick(Sender: TObject);
var
        bmp : tBitmap;
        cnt : Integer;
        Dc  : HDC;
        R   : TRect;
begin
        inherited;

        DC := GetDC ( pnlChart.handle);

        R := pnlChart.boundsRect;
        bmp := tBitmap.create;
        bmp.width := R.Right-R.Left;
        bmp.Height := R.Bottom - R.Top;
        Bitblt(bmp.canvas.handle,0,0,bmp.Width,bmp.height,dc,r.left,r.top,srccopy);

        spdSaveGraph.DefaultExt := 'bmp';
        spdSaveGraph.FileName := 'Gant.bmp';
        spdSaveGraph.Filter := 'Bitmap Picture|*.bmp';

        if spdSaveGraph.Execute then
                bmp.saveToFile (spdSaveGraph.FileName);

        bmp.free;
end;

但是这种方法只保存屏幕上显示的节点,但我需要保存所有节点。

【问题讨论】:

  • 您将无法使用 Bitblt 将 TVirualStringTree 的 nterie 内容保存到位图中。主要原因是 TVirtualStringTree 仅将其部分内容呈现到其 Windows 上。这样做是为了在您有大量字符串时不会影响性能。
  • 也许你可以重写 TVirtualStringTree 默认的 Paint 方法来强制它把它的全部内容绘制到你的 BMP 中。我自己从来没有这样做过

标签: delphi tvirtualstringtree


【解决方案1】:
procedure TfrmDidTreeTime.cmSaveDidTreeTimeClick(Sender: TObject);
var
        bmp_total: tBitmap;
        bmp_current : tBitmap;

        r: TRect;
        dc: HDC;

        date_start: TDateTime;
        h: integer;

        bmp_current_position: integer;
        scroll_position_current: integer;
        scroll_position_total: integer;
        screen_count: integer;
begin
        inherited;

        date_start := dtpDateStart.Value;

        dc := GetDC (tvDidTimeTree.handle);
        r := tvDidTimeTree.boundsRect;

        bmp_total := tBitmap.create;
        bmp_total.width := r.Right - r.Left - sbxVertical.Width - 3;
        h := r.Bottom - r.Top - tvDidTimeTree.Header.Height;
        if h < 0 then
                h := 0;

        SendMessage(tvDidTimeTree.Handle, WM_VSCROLL, SB_BOTTOM, 0);

        scroll_position_total := GetScrollPos(tvDidTimeTree.Handle, sb_Vert);
        screen_count := round(scroll_position_total / h);

        bmp_total.Height := h * (screen_count + 1);
        bmp_current_position := 0;

        SendMessage(tvDidTimeTree.Handle, WM_VSCROLL, SB_TOP, 0);

        tvDidTimeTree.Repaint;

        while (scroll_position_current < scroll_position_total) do
        begin
                r := tvDidTimeTree.boundsRect;
                dc := GetDC (tvDidTimeTree.handle);

                bmp_current := tBitmap.create;
                bmp_current.width := r.Right - r.Left - sbxVertical.Width - 3;
                bmp_current.Height := h - 5;

                Bitblt(bmp_total.canvas.handle, 0, bmp_current_position, bmp_current.Width, bmp_current.height, dc, r.left, r.top, srccopy);
                scroll_position_current := GetScrollPos(tvDidTimeTree.Handle, sb_Vert);

                SendMessage(tvDidTimeTree.Handle, WM_VSCROLL, SB_PAGEDOWN, 0);

                tvDidTimeTree.Repaint;

                bmp_current_position := bmp_current_position + bmp_current.Height;
        end;

        spdSaveGraph.DefaultExt := 'bmp';
        spdSaveGraph.FileName := DateToStr(date_start) + '—' + DateToStr(IncMonth(date_start, 1)) + '.bmp';
        spdSaveGraph.Filter := 'Bitmap Picture|*.bmp';

        if spdSaveGraph.Execute then
                bmp_total.saveToFile (spdSaveGraph.FileName);

        bmp_total.free;
end;

【讨论】:

    猜你喜欢
    • 2012-11-29
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-11
    • 1970-01-01
    相关资源
    最近更新 更多