2014-09-03 94 views
0

我创建它看起来像这样的维恩图的创建区,鼠标点击的维恩图

enter image description here

我也创建区域对维恩的每个部分处理的点击。左/右和通用两部分。这里是我的代码,

private void panelControlVennDiagram_Paint(object sender, PaintEventArgs e) 
{ 
    Rectangle leftVenn = new Rectangle(20, 50, 130, 130); 
    Rectangle rightVenn = new Rectangle(60, 50, 130, 130); 
    commonRegion = new Region(); 

    Pen pen = new Pen(Color.Black,3); 
    using (Brush brushLeft = new SolidBrush(leftVennColor)) 
    { 
    ellipseLeftOnlyPath.AddEllipse(leftVenn); 
    leftOnlyRegion = new Region(ellipseLeftOnlyPath); 
    e.Graphics.FillEllipse(brushLeft, leftVenn); 
    e.Graphics.DrawEllipse(pen, leftVenn); 
    brushLeft.Dispose(); 
    } 

    using (Brush brushRight = new SolidBrush(rightVennColor)) 
    { 
    ellipseRightOnlyPath.AddEllipse(rightVenn); 
    rightOnlyRegion = new Region(rightVenn); 
    e.Graphics.FillEllipse(brushRight, rightVenn); 
    e.Graphics.DrawEllipse(pen, rightVenn); 
    brushRight.Dispose(); 
    } 

    using (GraphicsPath circle_path = new GraphicsPath()) 
    { 
    circle_path.AddEllipse(leftVenn); 
    commonRegion.Intersect(circle_path); 
    } 

    using (GraphicsPath circle_path = new GraphicsPath()) 
    { 
    circle_path.AddEllipse(rightVenn); 
    commonRegion.Intersect(circle_path); 
    } 

    leftOnlyRegion.Exclude(commonRegion); 
    rightOnlyRegion.Exclude(commonRegion); 

    using (Brush brushCommon = new SolidBrush(commonColor)) 
    { 
    e.Graphics.FillRegion(brushCommon, commonRegion); 
    brushCommon.Dispose(); 
    } 

    using (Brush brushLeftOnly = new SolidBrush(leftRightCommonColor)) 
    { 
    ellipseLeftRightCommonPath.AddEllipse(65, 80, 35, 40); 
    e.Graphics.FillEllipse(brushLeftOnly, 65, 80, 35, 40); 
    e.Graphics.DrawEllipse(pen, 65, 80, 35, 40); 
    brushLeftOnly.Dispose(); 
    } 

    using (Brush brushRightOnly = new SolidBrush(rightLeftCommonColor)) 
    { 
    ellipseRightLeftCommonPath.AddEllipse(105, 110, 35, 40); 
    e.Graphics.FillEllipse(brushRightOnly, 105, 110, 35, 40); 
    e.Graphics.DrawEllipse(pen, 105, 110, 35, 40); 
    brushRightOnly.Dispose(); 
    } 

    Brush brush = new SolidBrush(Color.Black); 
    Font stringFont = new Font("Calibri", 9, FontStyle.Bold); 
    Font stringFontCommon = new Font("Calibri", 8, FontStyle.Bold); 

    e.Graphics.DrawString(leftValue.ToString(), stringFont, brush, 40, 90); 
    e.Graphics.DrawString(rightValue.ToString(), stringFont, brush, 160, 90); 
    e.Graphics.DrawString(leftRightValue.ToString(), stringFontCommon, brush, 70, 115); 
    e.Graphics.DrawString(rightLeftValue.ToString(), stringFontCommon, brush, 110, 115); 

    brush.Dispose(); 
    stringFont.Dispose(); stringFontCommon.Dispose(); 
    pen.Dispose(); 
} 

这是为了处理鼠标点击。基本上我只想在左/右部分处理点击,而不是交点。但即使这样我不会收到消息框。有时候,如果我设置了一个断点,它确实会打到messagebox代码,但有时候不会。

private void panelControlVennDiagram_MouseClick(object sender, MouseEventArgs e) 
{ 
    if (e.Button == System.Windows.Forms.MouseButtons.Left) 
    { 
    if (ellipseLeftRightCommonPath.IsVisible(e.Location)) 
     MessageBox.Show("Left - Right Common"); 
    else if (ellipseRightLeftCommonPath.IsVisible(e.Location)) 
     MessageBox.Show("Right - Left Common"); 
    else if (leftOnlyRegion.IsVisible(e.Location)) 
     MessageBox.Show("Left Only"); 
    else if (rightOnlyRegion.IsVisible(e.Location)) 
     MessageBox.Show("Right Only"); 
    } 
} 

这里有什么错?请帮忙。

+0

在'paint'方法中,需要重置'MouseClick'方法中使用的路径/区域对象。例如。 'ellipseLeftRightCommonPath = new GraphicsPath();' – Loathing 2014-09-03 07:32:02

+0

*使用语句*你不会处理对象(使用dispose)。这是*使用*语句的目的。 – 2014-09-03 08:39:29

+0

您正在* commonRegion.Intersect(circle_path); *中使用* commonRegion *,而无需首先进行初始化。 * circle_path *与什么相交? – 2014-09-03 08:52:45

回答

0

我直接使用区域而不是使用GraphicsPath ..它的工作原理。

leftOnlyRegion = new Region(leftVenn);