简体   繁体   中英

Mouse clicks on the created regions in Venn diagram

I have created a venn diagram which looks like this,

在此处输入图片说明

I have also created regions for handling clicks on each part of venn. Left / Right and common two parts. Here is the code I have,

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

This is to handle the mouse clicks. Basically I want to have a click handled only on left / right parts and not their intersection. But even with this I dont get the message box. Sometimes if I put a break point it does hit messagebox code but sometimes not.

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

Whats wrong here? Please help.

我直接使用区域而不是使用GraphicsPath。

    leftOnlyRegion = new Region(leftVenn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM