2017-12-21 21 views
0

C#のWinformプロジェクトに読み込まれたイメージを含むピクチャボックスがあります。私はフォームを初期化するときに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

したがって、基本的には、MouseBox(hoover用)とPictureBoxのClickイベントを購読する必要があります。現在のカーソルが治療領域にあるかどうかを指定し、これを処理する必要があります。

私はあなたのコードを採用し、小さなデモを作成しました。 注意C#命名規則に一致するように、TreatmentZone変数の名前を変更しました。また、それらをプロパティに変更しました。

注意:フォームにツールチップを追加する必要があります。

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

}

そして、これはあなたのフォームです: お知らせ私のPictureBoxはちょうどpictureBox1

公共部分クラスForm1に命名された:フォーム {

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

優れた親愛なる親友!これであなたの助けをありがとう!これは本当に役に立ちます! :-) – lenvdb

+0

こんにちはすべて あなたの助けてくれてありがとう - 私はあなたの提案を組み込み、私はそれが完璧に働くようになった - また、私は構造体ではなくリストオブジェクトを使用します。ずっといい。 – lenvdb

関連する問題