本文介绍了如何在 .NET 中调用 DrawThemeTextEx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :-)

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

I need to translate the follwing .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);

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

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

推荐答案

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

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;

然后,这样称呼它:

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

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

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

这篇关于如何在 .NET 中调用 DrawThemeTextEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 23:35