如何更改主题DrawThemeTextEx字体颜色?

发布于 2024-11-25 08:32:02 字数 2473 浏览 3 评论 0 原文

我正在使用 DrawThemeTextEx 绘制文本。我正在尝试使用 DTTOPS 结构的 crText COLORREF 成员以特定颜色绘制它:

procedure DrawThemeText(dc: HDC; text: WideString; font: TFont; pt: TPoint; foreColor: COLORREF);
var
   R: TRect;
   dttOpts: TDttOpts;
   hOldFont: HFONT;
   oldColor: COLORREF;
begin
   foreColor := $FF00FF00; //bright lime green
   font.

   R := Rect(pt.x, pt.y, $7fffffff, $7fffffff);

   ZeroMemory(@dttOpts, SizeOf(TDTTOpts));
   dttOpts.dwSize := SizeOf(TDTTOpts);
   dttOpts.iGlowSize := 1;
   dttOpts.crText := foreColor;
   dttOpts.dwFlags := DTT_GLOWSIZE or DTT_TEXTCOLOR;

   hOldFont := SelectObject(dc, font.Handle);
   oldColor := SetTextColor(dc, foreColor);
   try
      hr := DrawThemeTextEx(ThemeServices.Theme[teWindow], DC, WP_CAPTION, CS_ACTIVE, 
            PWideChar(Text), Length(Text),
            DT_LEFT or DT_TOP or DT_SINGLELINE or DT_NOPREFIX, R, dttOpts);
   finally
      SetTextColor(dc, oldColor);
      SelectObject(dc, hOldFont);
   end;

不幸的是,文本颜色总是黑色,而不是明亮的石灰绿色。代码指定:

在此处输入图像描述

可以更改字体在设备上下文中选择新字体,即:

   SelectObject(dc, font.Handle);

但都不是 SetTextColor,也不设置 DTTOPS 结构,更改使用的文本颜色。

令人困惑的是 DTTOPS 结构允许我指定一种颜色

typedef 结构 _DTTOPTS
{
    DWORD dwSize; // 结构体的大小
    DWORD dwFlags; // 指定了哪些选项
    COLORREF crText; // 用于文本填充的颜色
    COLORREF crBorder; // 用于文本轮廓的颜色
    COLORREF crShadow; // 用于文本阴影的颜色
    ...

与 DTT_TEXTCOLOR 标志一起指示我正在使用该成员:

 #define DTT_TEXTCOLOR (1UL << 0) // crText 已指定

我想要完成的事情记录在案,但它无法正常工作。显然我做错了什么。

使用 DrawThemeTextEx

i am using DrawThemeTextEx to draw text. i am trying to draw it in a particular color using the crText COLORREF member of DTTOPS structure:

procedure DrawThemeText(dc: HDC; text: WideString; font: TFont; pt: TPoint; foreColor: COLORREF);
var
   R: TRect;
   dttOpts: TDttOpts;
   hOldFont: HFONT;
   oldColor: COLORREF;
begin
   foreColor := $FF00FF00; //bright lime green
   font.

   R := Rect(pt.x, pt.y, $7fffffff, $7fffffff);

   ZeroMemory(@dttOpts, SizeOf(TDTTOpts));
   dttOpts.dwSize := SizeOf(TDTTOpts);
   dttOpts.iGlowSize := 1;
   dttOpts.crText := foreColor;
   dttOpts.dwFlags := DTT_GLOWSIZE or DTT_TEXTCOLOR;

   hOldFont := SelectObject(dc, font.Handle);
   oldColor := SetTextColor(dc, foreColor);
   try
      hr := DrawThemeTextEx(ThemeServices.Theme[teWindow], DC, WP_CAPTION, CS_ACTIVE, 
            PWideChar(Text), Length(Text),
            DT_LEFT or DT_TOP or DT_SINGLELINE or DT_NOPREFIX, R, dttOpts);
   finally
      SetTextColor(dc, oldColor);
      SelectObject(dc, hOldFont);
   end;

Unfortunately the text color always comes out black, rather than the bright lime green color my code is specifying:

enter image description here

i can alter the font that is used by selecting the new font into the device context, i.e.:

   SelectObject(dc, font.Handle);

but neither SetTextColor, nor setting the crText and DTT_TEXTCOLOR options of the DTTOPS structure, alter the text color used.

What's confusing is that the DTTOPS structure allows me to specify a color:

typedef struct _DTTOPTS
{
    DWORD             dwSize;              // size of the struct
    DWORD             dwFlags;             // which options have been specified
    COLORREF          crText;              // color to use for text fill
    COLORREF          crBorder;            // color to use for text outline
    COLORREF          crShadow;            // color to use for text shadow
    ...

along with the DTT_TEXTCOLOR flag to indicate i'm using that member:

   #define DTT_TEXTCOLOR       (1UL << 0)      // crText has been specified

What i want to accomplish is documented, but it's not working right. Obviously i'm doing something wrong.

How do i specify the text color when drawing text using DrawThemeTextEx?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Hello爱情风 2024-12-02 08:32:02

crText 成员是一个ColorRefMSDN 说高位字节“必须为零”。

The crText member is a ColorRef. MSDN says the high-order byte "must be zero."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文