如何在.NET中调用DrawThemeTextEx

发布于 2024-10-27 13:03:20 字数 687 浏览 5 评论 0原文

我需要在 Vista/7 玻璃窗中编写带有发光效果的文本,并且我正在尝试调用 API 来在那里编写一些文本。我在 CodeProject 中查看了一个很棒的 示例,但问题是我我正在使用 .NET 1(请不要问:-)

我需要将以下 .NET 2 代码转换为 PInvoke、.NET 1 代码。

// using System.Windows.Forms.VisualStyles
VisualStyleRenderer renderer = new VisualStyleRenderer(
                               VisualStyleElement.Window.Caption.Active);

// call to UxTheme.dll
DrawThemeTextEx(renderer.Handle, 
                memoryHdc, 0, 0, text, -1, (int)flags,    
                ref textBounds, ref dttOpts);

.NET 1 中不存在 VisualStyleRenderer 类,因此我需要以其他方式获取 renderer.Handle 。

I need to write a text with glow in a Vista/seven glass window, and I'm, trying to call the API to write some text there. I have checked out a great sample in CodeProject, but the problem is that I'm using .NET 1 (please, don't ask :-)

I need to translate the following .NET 2 code to PInvoke, .NET 1 code.

// using System.Windows.Forms.VisualStyles
VisualStyleRenderer renderer = new VisualStyleRenderer(
                               VisualStyleElement.Window.Caption.Active);

// call to UxTheme.dll
DrawThemeTextEx(renderer.Handle, 
                memoryHdc, 0, 0, text, -1, (int)flags,    
                ref textBounds, ref dttOpts);

The class VisualStyleRenderer does not exist in .NET 1, so I need to get the renderer.Handle in other way.

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

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

发布评论

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

评论(2

乞讨 2024-11-03 13:03:20

定义 OpenThemeData API 和 DrawThemeTextEx,以及一些必需的结构和常量:

    [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
    private static extern IntPtr OpenThemeData(IntPtr hwnd, string pszClassList);

    [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
    private extern static Int32 DrawThemeTextEx(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, string pszText, int iCharCount, uint flags, ref RECT rect, ref DTTOPTS poptions);

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct DTTOPTS
    {
      public int dwSize;
      public int dwFlags;
      public int crText;
      public int crBorder;
      public int crShadow;
      public int iTextShadowType;
      public int ptShadowOffsetX;
      public int ptShadowOffsetY;
      public int iBorderSize;
      public int iFontPropId;
      public int iColorPropId;
      public int iStateId;
      public bool fApplyOverlay;
      public int iGlowSize;
      public IntPtr pfnDrawTextCallback;
      public IntPtr lParam;
    }

    // taken from vsstyle.h
    private const int WP_CAPTION = 1;
    private const int CS_ACTIVE = 1;

然后,像这样调用它:

IntPtr handle = OpenThemeData(IntPtr.Zero, "WINDOW");
DrawThemeTextExt(handle, hdc, WS_CAPTION, CS_ACTIVE, ...)

WS_CAPTION 和 CS_ACTIVE 值分别与 .NET 2 的 Caption 和 Active 匹配。此处正式描述了值:部件和状态

Define the OpenThemeData API and DrawThemeTextEx, as well as some required structs and constants:

    [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
    private static extern IntPtr OpenThemeData(IntPtr hwnd, string pszClassList);

    [DllImport("uxtheme.dll", CharSet = CharSet.Unicode)]
    private extern static Int32 DrawThemeTextEx(IntPtr hTheme, IntPtr hdc, int iPartId, int iStateId, string pszText, int iCharCount, uint flags, ref RECT rect, ref DTTOPTS poptions);

    [StructLayout(LayoutKind.Sequential)]
    private struct RECT
    {
        public int left;
        public int top;
        public int right;
        public int bottom;
    }

    [StructLayout(LayoutKind.Sequential)]
    private struct DTTOPTS
    {
      public int dwSize;
      public int dwFlags;
      public int crText;
      public int crBorder;
      public int crShadow;
      public int iTextShadowType;
      public int ptShadowOffsetX;
      public int ptShadowOffsetY;
      public int iBorderSize;
      public int iFontPropId;
      public int iColorPropId;
      public int iStateId;
      public bool fApplyOverlay;
      public int iGlowSize;
      public IntPtr pfnDrawTextCallback;
      public IntPtr lParam;
    }

    // taken from vsstyle.h
    private const int WP_CAPTION = 1;
    private const int CS_ACTIVE = 1;

And then, call it like this:

IntPtr handle = OpenThemeData(IntPtr.Zero, "WINDOW");
DrawThemeTextExt(handle, hdc, WS_CAPTION, CS_ACTIVE, ...)

The WS_CAPTION and CS_ACTIVE values match .NET 2's Caption and Active respectively. Values are described here officially: Parts and States

残月升风 2024-11-03 13:03:20

简而言之,您可以通过调用 <代码>OpenThemeData()

要弄清楚所有细节,您可以更轻松地用 C++ 编写示例应用程序来了解如何从头开始驱动主题 API。网上有很多教程和大量示例代码。但是在 C++ 中进行,您将可以轻松使用所有功能。您最不想做的事情就是与 P/Invokes 作斗争,同时您还要掌握低级主题 API。

一旦你用 C++ 破解了它,然后继续使用 P/Invokes,如果遇到问题,可以很容易地返回到有效的 C++ 代码。

In short, you get what you want by calling OpenThemeData().

To work out all the details it would be much easier for you to write a sample app in C++ to get to know how to drive the theme API from the ground up. There are many tutorials and lots of sample code on the web. But do it in C++ where you will have all the functions readily available. The last thing you want to get doing is fighting with P/Invokes whilst you are also getting to grips with low-level theme API.

Once you get it cracked in C++, then move on to the P/Invokes and if you have trouble it will be easy to refer back to the C++ code that works.

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