2014-03-06 1 views
1

BrushHPEN에서 Pen으로 가져 오려면 어떻게해야합니까?
두면 사이에 어떤 연결이 있습니까?WinApi에서 C# 펜과 브러시 사용?

은 (내가 .NET's System.Drawing 같은 DrawRoundRect/CreateRoundRectRgn에서 구현되지 않은 WINAPI 기능을 사용하려는 내 자신을 만드는 대신 그것을 위해 .NET에서 브러쉬를 사용하고 싶습니다.)

답변

1

I 돈 더 이상 원본 소스가 없으며 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 객체를 건네하고있는 Paint 또는 PaintBackground 이벤트 내에서 전화를 걸 수 : 어쨌든, 여기에 클래스 누군가가 내가 사용하는 것이 다시 잠시를 제공합니다. 그것을 다른 것과 함께 전달하고 둥근 물건을 그립니다.

외부 P/호출없이 .NET에서 모든 것을 유지하는 옵션입니다.

+0

확인하십시오. 그러나이 질문에 대한 답이 될 수는 없습니다 (브러쉬와 핸들에 관한 것). 공유 Thx! – Bitterblue

+0

필자는 P/invoke route를 사용하는 이유는 무엇입니까? 더 나은 결과를 얻으려는 번거 로움처럼 보입니다. 더 똑똑하고 열심히 일하지 않습니까? – DonBoitnott

+0

내 프로젝트를 "취소"하기에는 너무 늦었습니다. WinApi를 가져 오는 큰 라이브러리가 있습니다. lib를 가져 와서 새로운 프로젝트를 시작합니다. 그러나 당신의 대답은 좋았고 나를 도왔습니다. 그것은 내가 요구 한 것이 아닙니다. :) – Bitterblue

2

Brush 클래스에는 HBRUSH 핸들을 IntPtr로 유지하는 nativeBrush이라는 전용 필드가 있습니다. 그것은 개인 필드이기 때문에

, 그것은 닷넷에 대한 개정으로 변경 될 수 있습니다,하지만 당신은 그 값에 얻기 위해 몇 가지 사기, 부서지기 쉬운 반사를 사용하여 괜찮다면, 당신은이 같은 불쾌한 무언가 할 수있는 :

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

유사한 반사 코드를 사용하여 액세스 할 수있는 이라는 유사한 필드가 nativePen이라고합니다.

그러나 Don으로 위의 그림과 같이 GraphicsPath를 사용할 수 있으면 위험이 훨씬 적습니다.

또한 이것은 내가 지금 이후 해요 점에 좋은 대안이다 this article on creating rounded rectangles using GraphicsPath

+0

'hbrush' 값을 썼는데 유효한 포인터 주소처럼 보이지만 WinApi가 내 브러시의 색으로 채울 수 없습니다. .NET을 올바르게 채 웁니다. – Bitterblue

관련 문제