2017-07-18 18 views
1

矩形をフォームに描画するためにネイティブメソッドを使用しています。私はgraphics.DrawRectangleメソッドを使用すると、四角形が適切に描画されます。しかし、以下のコードのようなネイティブメソッドを使用すると、常に四角形の内側の白い背景が塗りつぶされます。ネイティブメソッドを描画する矩形は、白い背景で塗りつぶします。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     this.BackColor = Color.Yellow; 
    } 

protected override void OnPaint(PaintEventArgs e) 
{ 
    Rectangle rect = new Rectangle(20,20,100,30); 
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), rect); 
    IntPtr hdc = e.Graphics.GetHdc(); 
    DrawRectangle(hdc, Pens.Red, new Rectangle(25, 60, 100, 30)); 
    e.Graphics.ReleaseHdc(); 
    base.OnPaint(e); 
} 

public void DrawRectangle(IntPtr hdc, Pen pen, Rectangle rect) 
{ 
    IntPtr hpen = IntPtr.Zero; 
    try 
    { 
     hpen = CreatePen((int)pen.DashStyle, (int)pen.Width, (int)new RGB(pen.Color).ToInt32()); 
     SelectObject(hdc, hpen); 
     RectangleCE(hdc, rect.Left, rect.Top, rect.Right, rect.Bottom); 
    } 
    finally 
    { 
     if (hpen != IntPtr.Zero) 
      DeleteObject(hpen); 
    } 
} 

[DllImport("gdi32")] 
public static extern IntPtr CreatePen(int penStyle, int width, int color); 


[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] 
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj); 

[DllImport("gdi32.dll", EntryPoint = "Rectangle", SetLastError = true)] 
public static extern uint RectangleCE(IntPtr hdc, int leftRect, int topRect, int rightRect, int bottomRect); 

[DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] 
public static extern int DeleteObject(IntPtr hObject); 

/// <summary> 
/// Selects a red, green, blue (RGB) color based on the arguments supplied and the color capabilities of the output device 
/// </summary> 
[StructLayout(LayoutKind.Sequential)] 
public struct RGB 
{ 
    /// <summary> 
    /// The reserved fields. 
    /// </summary> 
    private byte r, g, b, reserved; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="RGB"/> struct. 
    /// </summary> 
    /// <param name="colorIn">The color value.</param> 
    public RGB(Color colorIn) 
    { 
     r = colorIn.R; 
     g = colorIn.G; 
     b = colorIn.B; 
     reserved = 0; 
    } 

    /// <summary> 
    /// Convert the RGB color value to integer value. 
    /// </summary> 
    /// <returns>Returns the converted value.</returns> 
    public int ToInt32() 
    { 
     var colors = new byte[4]; 
     colors[0] = r; 
     colors[1] = g; 
     colors[2] = b; 
     colors[3] = reserved; 
     return BitConverter.ToInt32(colors, 0); 
    } 
} 

}

形態は黄色で埋めたもので、添付された画像を見つけて下さい。最初の1つの矩形はgraphics.DrawRectangleメソッドを使用して描画され、2つ目は上記のネイティブメソッドを使用して描画されます。

enter image description here

誰もが上記のネイティブメソッドを使用して白色の背景を埋めることなく、四角形を描画する方法を提案してください?

+0

私はあなたが透明ブラシを作成する必要がありますね:) –

答えて

1

矩形は、現在のペンで矩形を描画し、は現在のブラシで内部を塗りつぶします。

https://msdn.microsoft.com/en-us/library/aa269219(v=vs.60).aspx

あなたは以下のようにSelectObjectを使用して、現在のブラシを変更することができます。しかし、Color.Transparentの設定はWhite Colorとみなされます。

var yColor = (uint) new RGB(Color.Yellow).ToInt32(); 
SelectObject(hdc, CreateSolidBrush(yColor)); 
hpen = CreatePen((int)pen.DashStyle, (int)pen.Width, (uint)new RGB(pen.Color).ToInt32()); 
SelectObject(hdc, hpen); 
RectangleCE(hdc, rect.Left, rect.Top, rect.Right, rect.Bottom); 

あなたが内部を埋めるためにしたくない場合は、FrameRect機能を使用する必要があります。しかし、この関数を使うと、線の太さは常に1になります。調整することはできません。

https://msdn.microsoft.com/en-us/library/dd144838(v=vs.85).aspx

var rect2 = new RECT(rect); 
FrameRect(hdc, ref rect2, CreateSolidBrush((uint)new RGB(Color.Red).ToInt32())); 
関連する問題