鼠标点击维恩图中创建的区域

时间:2014-09-03 07:04:18

标签: c# winforms graphics

我创建了一个看起来像这样的维恩图,

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

这是为了处理鼠标点击。基本上我想要只在左/右部分处理点击而不是它们的交集。但即便如此,我也没有得到消息框。有时,如果我设置了一个断点,它确实会触及消息框代码,但有时却没有。

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

这里有什么不对?请帮忙。

1 个答案:

答案 0 :(得分:0)

我直接使用区域而不是使用GraphicsPath ..它可以工作。

    leftOnlyRegion = new Region(leftVenn);