簡體   English   中英

在WinApi中使用C#筆和畫筆?

[英]Using C# Pens and Brushes in WinApi?

我怎樣才能得到HBRUSH畫筆HPEN
我想知道雙方有什么聯系嗎?

(我想使用未在.NET's System.Drawing實現的WinApi函數,例如DrawRoundRect / CreateRoundRectRgn ,我想使用.NET中的畫筆而不是自己創建它們。)

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,那么風險就會大大降低。

另請參閱有關使用GraphicsPath創建圓角矩形的文章

我不再擁有原始資源,甚至可能是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);
    }
}

只需在您傳遞Graphics對象的任何PaintPaintBackground事件中進行調用。 將其與其他任何內容一起傳遞,並繪制圓形內容。

它可以選擇在沒有外部P / Invokes的情況下將其全部保存在.NET中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM