如何在使用DrawThemeBackground时将元素部分绘制为从右到左的(RTL)方向?

4

我正在尝试以从右到左的方向绘制Explorer::Treeview类的ttGlyphClosed元素(就像当BiDiModebdLeftToRight时一样)。 我有一个问题,我不知道如何使我的离屏位图透明。 位图的背景始终是白色。

我使用以下代码来镜像图像:

procedure TForm5.FormPaint(Sender: TObject);
var
  bm: TBitmap;
  ARect: TRect;
  Details: TThemedElementDetails;
begin    
  if ExplorerTreeviewhTheme = 0 then
    ExplorerTreeviewhTheme := OpenThemeData(0, 'Explorer::Treeview');

  ARect := Rect(20, 20, 40, 40);
  Details := ThemeServices.GetElementDetails(ttGlyphClosed);
  DrawThemeBackground(ExplorerTreeviewhTheme, Canvas.Handle,
    Details.Part, Details.State, ARect, nil); //Ok

  bm := TBitmap.Create;
  try
    bm.Width := 20;
    bm.Height := 20;

    ARect := Rect(00, 00, 20, 20);
    DrawThemeBackground(ExplorerTreeviewhTheme, bm.Canvas.Handle,
      Details.Part, Details.State, ARect, nil);

    // rendered result has white background
    Canvas.Draw(60, 10, bm);    
    // rendered result is mirrored but has also white background
    StretchBlt(Canvas.Handle, 100, 10, -20, 20, bm.Canvas.Handle, 0, 0, 20, 20, SRCCOPY);
  finally
    bm.Free;
  end;    
end;

问题在于如何镜像由DrawThemeBackground函数绘制的元素(用于RTL阅读),或者如何使用此函数进行RTL(从右到左)呈现?
2个回答

5

在你绘制图像之前,使用 TLama 展示的 SetLayout 方法来切换画布的布局。

function SetLayout(hdc: HDC; dwLayout: DWORD): DWORD; stdcall;
  external 'gdi32' name 'SetLayout';

const
  LAYOUT_RTL = $00000001;

procedure TForm1.FormPaint(Sender: TObject);
var
  ExplorerTreeviewhTheme: HTHEME;
  Details: TThemedElementDetails;
  ARect: TRect;
  Size: TSize;
begin
  ExplorerTreeviewhTheme := OpenThemeData(Handle, 'Explorer::Treeview');
  Details := ThemeServices.GetElementDetails(ttGlyphClosed);
  GetThemePartSize(ExplorerTreeviewhTheme, Canvas.Handle, Details.Part,
      Details.State, nil, TS_DRAW, Size);

  ARect := Rect(20, 30, 20 + Size.cx, 30 + Size.cy);
  
  // normal layout
  DrawThemeBackground(ExplorerTreeviewhTheme, Canvas.Handle,
                      Details.Part, Details.State, ARect, nil);

  // switched layout
  SetLayout(Canvas.Handle, LAYOUT_RTL);
  
  // calculate the rectangle for RTL as if it's in LTR
  OffsetRect(ARect, 0, Size.cy); // align to the bottom of the first image so that we can see
  ARect.Left := ClientWidth - ARect.Left - Size.cx;
  ARect.Right := ARect.Left + Size.cx;

  DrawThemeBackground(ExplorerTreeviewhTheme, Canvas.Handle,
                      Details.Part, Details.State, ARect, nil);
  
  // restore layout
  SetLayout(Canvas.Handle, 0);
  CloseThemeData(ExplorerTreeviewhTheme);
end;

输出: 在此输入图片描述

主题API在16px的部分大小(W7-aero)中绘制一个6px宽的三角形。由于您无法知道图像在部件中的位置,因此您将无法更好地对齐它。


1

你在 stretchblt 调用中使用了 srccopy,但我认为你可能需要考虑其他选择,包括可能使用掩码

我已经有一段时间没有做这些事情了,所以我记不清楚了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接