2017-12-21 22 views
0

C#의 Winform 프로젝트에로드 된 이미지가있는 그림 상자가 있습니다. Form을 초기화 할 때 Picturebox에로드하는 몇 개의 타원 (X 점, Y 점, 너비, 높이, ZoneID, ZoneDescription)을 포함하는 Struct을 정의했습니다.Picturebox 이미지의 영역을 정의합니다 - 툴팁 C#

2 가지를 시도하고 싶습니다. 1) 사용자가 그림 상자의 그림 위에 마우스로 움직일 수있게하십시오. 마우스가 구조체에서 식별 된 영역에 들어가 자마자 구조체의 ZoneDescription을 보여주는 툴팁을 표시해야합니다.

2) 사용자가 여러 구역 중 하나를 클릭하면 응답을 생성하여 ZoneID를 트랩 할 수 있습니다.이 구역은 이미 구조체에 정의되어 있습니다. 그런 다음이를 데이터 집합에 추가합니다. 내가 할 수있는이 부분.

나는 Pt1에 대해 어떻게 해야할지 모르겠다. 어딘가에서 모든 영역에 대해 MouseEnter 이벤트 핸들러와 MouseLeave EventHandler를 정의해야하지만, 어떻게해야할지 모르겠습니다. 이미 양식에 툴팁이 있습니다. 여기

구조체의 정의는 영역을 포함한다 :

public struct TreatmentZone 
    { 

     public int nZoneID; 
     public string sZoneCode; 
     public string sZoneDesc; 
     public Color sbPaintbrush; 
     public int nZonewidth; 
     public int nZoneheight; 
     public int nZoneX; 
     public int nZoneY; 

     public TreatmentZone(int _ZoneID, string _sZoneCode,string _ZoneDesc, Color _sbBrush, int _ZoneX, int _ZoneY, int _Zonewidth, int _Zoneheight) 
     { 
      this.nZoneID = _ZoneID; 
      this.sZoneCode = _sZoneCode; 
      this.sZoneDesc = _ZoneDesc; 
      this.sbPaintbrush = _sbBrush; 
      this.nZoneX = _ZoneX; 
      this.nZoneY = _ZoneY; 
      this.nZonewidth = _Zonewidth; 
      this.nZoneheight = _Zoneheight; 

     } 
    }; 
    TreatmentZone[] tZone = { 
           new TreatmentZone(1,"R1","Brain", Color.AliceBlue,155,35,30,20), 
           new TreatmentZone(5,"R2", "Hypothalamus", Color.AliceBlue,184,55,12,12)         
           }; 
    private void pic_TreatmentZones1_RightSole_Paint(object sender, PaintEventArgs e) 
    { 
     e.Graphics.FillEllipse(new System.Drawing.SolidBrush(tZone[0].sbPaintbrush), tZone[0].nZoneX, tZone[0].nZoneY, tZone[0].nZonewidth, tZone[0].nZoneheight); 
     e.Graphics.FillEllipse(new System.Drawing.SolidBrush(tZone[1].sbPaintbrush), tZone[1].nZoneX, tZone[1].nZoneY, tZone[1].nZonewidth, tZone[1].nZoneheight); 
    } 

어떻게 PT1에 필요한 단계가 할 것인가? 누구나 내가 사용할 수있는 코드 샘플을 가지고 있습니까?

답변

0

기본적으로 MouseMove (hoover 용)에 가입하고 PictureBox의 이벤트를 클릭해야합니다. 그런 다음 현재 커서가 치료 영역 위에 있는지 여부를 지정하고이를 처리해야합니다.

나는 코드를 채택하고 작은 데모를 만들었습니다. 주의 Treatment TreatmentZone 변수의 일부 이름을 C# 명명 규칙과 일치하도록 변경했습니다. 또한 속성으로 변경했습니다.

주의 사항 : 양식에 툴팁을 추가해야합니다.

public struct TreatmentZone 
{ 
#region members 

private readonly GraphicsPath path; 

#endregion 

//changed to properties 

public int Id { get; private set; } 
public string Code { get; private set; } 
public string Description { get; private set; } 
public Color Color { get; set; } 
public int Width { get; private set; } 
public int Height { get; private set; } 
public int X { get; private set; } 
public int Y { get; private set; } 

public TreatmentZone(int zoneId, string zoneCode, string description, Color color, int x, int y, int width, int height) 
{ 
    this.Id = zoneId; 
    this.Code = zoneCode; 
    this.Description = description; 
    this.Color = color; 
    this.X = x; 
    this.Y = y; 
    this.Width = width; 
    this.Height = height; 
    this.path = new GraphicsPath(); 

    //needed for hittest 
    this.path.AddEllipse(this.X, this.Y, this.Width, this.Height); 
} 

//https://stackoverflow.com/questions/13285007/how-to-determine-if-a-point-is-within-an-ellipse 
//This will check if the point (from mouse) is over ellips or not 
public bool HitTest(Point point) 
{ 
    var x = point.X - this.X; 
    var y = point.Y - this.Y; 
    return this.path.IsVisible(x, y); 
} 

}

그리고 이것은 당신의 양식입니다 : {

private readonly List<TreatmentZone> treatmentZones = new List<TreatmentZone>(); 

public Form1() 
{ 
    this.InitializeComponent(); 
    this.treatmentZones.Add(new TreatmentZone(1, "a", "b", Color.Blue, 10, 10, 200, 400)); 
} 

private void pictureBox1_Paint(object sender, PaintEventArgs e) 
{ 
    foreach (var treatmentZone in this.treatmentZones) 
    { 
    using(var brush = new SolidBrush(treatmentZone.Color)) 
     e.Graphics.FillEllipse(brush, treatmentZone.X, treatmentZone.Y, treatmentZone.Width, treatmentZone.Height); 
    } 
} 

private void pictureBox1_MouseMove(object sender, MouseEventArgs e) 
{ 
    foreach (var treatmentZone in this.treatmentZones) 
    { 
    if(treatmentZone.HitTest(e.Location)) 
     this.toolTip1.Show(treatmentZone.Description, this.pictureBox1, e.Location); 
    } 
} 

private void pictureBox1_Click(object sender, EventArgs e) 
{ 
    var location = this.pictureBox1.PointToClient(Cursor.Position); 
    foreach (var treatmentZone in this.treatmentZones) 
    { 
    if (treatmentZone.HitTest(location)) 
     MessageBox.Show("Handle the click of " + treatmentZone.Code + "."); 
    } 

} 

}

+0

양식 : 내 PictureBox를 단지 pictureBox1

공공 부분 Form1 클래스에 주의라는 Excellent 친애하는 Feal! 이걸 도와 주셔서 정말 고마워요! 이것은 정말 도움이 될 것입니다! :-) – lenvdb

+0

모두 안녕 많은 도움을 주셔서 감사합니다 - 나는 당신의 제안을 통합했고 완벽하게 작동하도록했습니다 - 또한 구조체가 아닌 List 개체를 사용합니다. 훨씬 낫다. – lenvdb