2016-10-22 3 views
-2

WinForms 앱이 있는데 특정 영역 위에 프로그램 방식으로 원을 그려야합니다. 나는 몇 가지 문제를 겪고 있으며 어떤 통찰력도 인정 될 것입니다!양식 위에 원 그리기

1) 원을 그리거나 지울 코드가 있지만 (아래 참조) 원은 내 컨트롤 뒤에 표시됩니다. 나는 그들이 모든 경우에 "최상위"로서 그려지기를 바랍니다. 어떻게해야합니까?

2) 내 앱이 시작되면 당장 그려야 할 서클이 있습니다. Form Load 이벤트에서 그리기 위해 아무런 노력을하지 않았습니다. 그러나 여기 (Form graphics not set when form loads)에 관해서는 Paint 이벤트에서 그려 봅니다. 이것이 합리적으로 잘 작동하는 동안 (bool은 첫 번째 작업에만 해당), this.Invalidate();에 문제가있는 것으로 보입니다 (서클이 그려지지 않으므로). 더 좋은 방법이 있습니까? 사용자 정의 투명 패널을 만들고 상단에 배치 할 수 귀하의 요구 사항을 달성하는 방법을 작동 아직

private void parseText() 
{ 
    this.Invalidate(); 
    List<string> lines = new List<string>(richTextBoxRaw.Text.Split(new string[] { Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)); 

    foreach (string s in lines) 
    { 
     switch (s) 
     { 
      case "<draw1>": 
       drawCircle(107, 26, 25); 
       break; 
      default: 
       break; 
     } 
    } 
} 

private void drawCircle(int x, int y, int transparency) 
{ 
    if (transparency < 0) 
     transparency = 0; 
    else if (transparency > 255) 
     transparency = 255; 

    SolidBrush brush = new SolidBrush(Color.FromArgb(transparency, 255,0,0)); 
    Graphics graphics = this.CreateGraphics(); 

    graphics.FillEllipse(brush, new Rectangle(x, y, 25, 25)); 
    brush.Dispose(); 
    graphics.Dispose(); 
} 

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    if (starting) 
     parseText(); 

    starting = false; 
} 
+1

당신은 몇 가지 기본을 이해할 필요가있다. 1. 폼로드 (paint load)가 아닌 페인트 이벤트 (Paint Event)에서 코드를 호출해야하기 때문에 화면 새로 고침이 필요할 때 항상 올바르게 업데이트됩니다. 2. 폼에는 자체 페인트 루틴이있는 컨트롤이 있다는 것을 기억하십시오. 그러면 잉크가 깨끗해질 것입니다. 컨트롤의 페인트 이벤트에 등록하고 컨트롤에 그릴 수 있습니다. 이제 폼의 모든 컨트롤 (Z 순서 0)을 오버레이하고 거기에 원을 그리는 usercontrol을 만들 수 있습니다. 그런 다음 UserControl 아래의 컨트롤에 이벤트를 전달하는 논리가 필요합니다. 간단한 해결책이 없습니다. – Vikhram

답변

2

그렇게 복잡하지 중 하나 : 여기 내 코드 (콤보 상자의 인덱스 변화에 parseText 실행이)입니다 빨간색 원이 그려지는 곳을 제어합니다.

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void DrawCircle(int x, int y, int transparency, Graphics graphics) 
    { 
     if (transparency < 0) 
      transparency = 0; 
     else if (transparency > 255) 
      transparency = 255; 

     SolidBrush brush = new SolidBrush(Color.FromArgb(transparency, 255, 0, 0)); 

     graphics.FillEllipse(brush, new Rectangle(x, y, 25, 25)); 
     brush.Dispose(); 
     graphics.Dispose(); 
    } 

    private void TransparentPanel1_Paint(object sender, PaintEventArgs e) 
    { 
     DrawCircle(10, 10, 255, e.Graphics); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     transparentPanel1.Enabled = false; 
     transparentPanel1.Paint += TransparentPanel1_Paint; 
     transparentPanel1.BringToFront(); 
    } 
} 

public class TransparentPanel : Panel 
{ 
    protected override CreateParams CreateParams 
    { 
     get 
     { 
      CreateParams cp = base.CreateParams; 
      cp.ExStyle |= 0x00000020; // WS_EX_TRANSPARENT 
      return cp; 
     } 
    } 
    protected override void OnPaintBackground(PaintEventArgs e) 
    { 
     //base.OnPaintBackground(e); 
    } 
} 

enter image description here

+0

그래서 당신의 제안을 구현했습니다 그리고 내가 가진 탭 컨트롤을 제외하고 위대한 작동하는 것 - 탭 컨트롤을 transparentPanel "BringToFront"무시하는 것 같다 및 tabControl 'transparentPanel.BringToFront();'를 넣을 수 있음) – derekantrican

+0

'TabControl.TabPages [0] .Paint'를 구독하고'transparePanel.Refresh();'를 호출 할 수 있습니다. –