在WinApi中使用C#笔和画笔?

时间:2014-03-06 13:58:24

标签: c# winforms winapi gdi

如何从Brush PenHBRUSH获取HPEN? 我想知道双方是否有任何联系?

(我想使用.NET's System.Drawing中没有实现的WinApi函数,例如DrawRoundRect / CreateRoundRectRgn,我想使用.NET中的画笔而不是创建他们是我自己的。)

2 个答案:

答案 0 :(得分:2)

Brush类确实有一个名为nativeBrush的私有字段,它将HBRUSH句柄保存为IntPtr。

因为它是一个私有字段,它可能随着.Net的任何修订而改变,但是如果你不介意使用一些狡猾的,脆弱的反射来获得它的价值,你可以做一些令人讨厌的事情:

Brush brush = ...the brush that you want to get at the handle for
FieldInfo field = typeof(Brush).GetField("nativeBrush",BindingFlags.NonPublic|BindingFlags.Instance);
IntPtr hbrush = (IntPtr)field.GetValue(brush);

Pen有一个名为nativePen的类似字段,您可以使用类似的反映代码访问该字段。

但是,如果您可以像上面显示的Don那样使用GraphicsPath,那么风险就会小得多。

另请查看this article on creating rounded rectangles using GraphicsPath

答案 1 :(得分:1)

我不再拥有原始资源,甚至可能是SO项目。无论如何,这里有一个人提供了一段时间我使用的课程:

/// <summary>
/// Collection of often-used classes and methods for easy access.
/// </summary>
public class RoundedDrawing
{
    private static GraphicsPath GetRoundedRectanglePath(Int32 x, Int32 y, Int32 width, Int32 height, Int32 radius)
    {
        GraphicsPath path = new GraphicsPath();
        path.AddLine(x + radius, y, x + width - radius, y);
        if (radius > 0)
            path.AddArc(x + width - 2 * radius, y, 2 * radius, 2 * radius, 270.0f, 90.0f);
        path.AddLine(x + width, y + radius, x + width, y + height - radius);
        if (radius > 0)
            path.AddArc(x + width - 2 * radius, y + height - 2 * radius, 2 * radius, 2 * radius, 0.0f, 90.0f);
        path.AddLine(x + width - radius, y + height, x + radius, y + height);
        if (radius > 0)
            path.AddArc(x, y + height - 2 * radius, 2 * radius, 2 * radius, 90.0f, 90.0f);
        path.AddLine(x, y + height - radius, x, y + radius);
        if (radius > 0)
            path.AddArc(x, y, 2 * radius, 2 * radius, 180.0f, 90.0f);
        return path;
    }

    /// <summary>
    /// Fills the interior of a rounded rectangle.
    /// </summary>
    public static void FillRoundedRectangle(Graphics graphics, Brush brush, float x, float y, float width, float height, float radius)
    {
        FillRoundedRectangle(graphics, brush, (Int32)x, (Int32)y, (Int32)width, (Int32)height, (Int32)radius);
    }

    /// <summary>
    /// Fills the interior of a rounded rectangle.
    /// </summary>
    public static void FillRoundedRectangle(Graphics graphics, Brush brush, Rectangle rect, Int32 radius)
    {
        FillRoundedRectangle(graphics, brush, rect.Left, rect.Top, rect.Width, rect.Height, radius);
    }

    /// <summary>
    /// Fills the interior of a rounded rectangle.
    /// </summary>
    public static void FillRoundedRectangle(Graphics graphics, Brush brush, RectangleF rect, float radius)
    {
        FillRoundedRectangle(graphics, brush, (Int32)rect.Left, (Int32)rect.Top, (Int32)rect.Width, (Int32)rect.Height, (Int32)radius);
    }

    /// <summary>
    /// Fills the interior of a rounded rectangle.
    /// </summary>
    public static void FillRoundedRectangle(Graphics graphics, Brush brush, Int32 x, Int32 y, Int32 width, Int32 height, Int32 radius)
    {
        using (GraphicsPath path = GetRoundedRectanglePath(x, y, width, height, radius))
            graphics.FillPath(brush, path);
    }

    /// <summary>
    /// Draws the outline of a rounded rectangle.
    /// </summary>
    public static void DrawRoundedRectangle(Graphics graphics, Pen pen, float x, float y, float width, float height, float radius)
    {
        DrawRoundedRectangle(graphics, pen, (Int32)x, (Int32)y, (Int32)width, (Int32)height, (Int32)radius);
    }

    /// <summary>
    /// Draws the outline of a rounded rectangle.
    /// </summary>
    public static void DrawRoundedRectangle(Graphics graphics, Pen pen, Rectangle rect, Int32 radius)
    {
        DrawRoundedRectangle(graphics, pen, rect.Left, rect.Top, rect.Width, rect.Height, radius);
    }

    /// <summary>
    /// Draws the outline of a rounded rectangle.
    /// </summary>
    public static void DrawRoundedRectangle(Graphics graphics, Pen pen, RectangleF rect, float radius)
    {
        DrawRoundedRectangle(graphics, pen, (Int32)rect.Left, (Int32)rect.Top, (Int32)rect.Width, (Int32)rect.Height, (Int32)radius);
    }

    /// <summary>
    /// Draws the outline of a rounded rectangle.
    /// </summary>
    public static void DrawRoundedRectangle(Graphics graphics, Pen pen, Int32 x, Int32 y, Int32 width, Int32 height, Int32 radius)
    {
        using (GraphicsPath path = GetRoundedRectanglePath(x, y, width, height, radius))
            graphics.DrawPath(pen, path);
    }
}

只需在您提交Paint个对象的任何PaintBackgroundGraphics事件中拨打电话即可。将其与其他任何内容一起传递,并绘制圆形内容。

可以选择在没有外部P / Invokes的情况下将其全部保存在.NET中。