2010-11-28 16 views
1

こんにちは、これは可能かどうかわかりませんが、GraphicsメソッドDrawImageを使用してイメージにツールチップを動的に追加しようとしています。私はイメージが何かにぶつかったり、何かが始まるかわからないときのためのプロパティやイベントは見ません。 WinForms(C# - .NET 3.5)を使用しています。任意のアイデアや提案をいただければ幸いです。ありがとう。.DrawImage()を使用してツールチップにマウスを追加するにはどうすればいいですか?

答えて

1

UserControlのようなものがあり、OnPaintメソッドでDrawImage()と呼んでいると思います。

これを考えると、ツールチップを明示的に制御する必要があります。基本的にフォーム上にTooltipを作成し、プロパティを使用してコントロールに渡し、コントロールがMouseHoverイベントを受け取ったときにツールチップを表示し、MouseLeaveイベントを受け取ったときにツールチップを非表示にします。このような

何か:

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() { 
     InitializeComponent(); 
    } 

    protected override void OnPaint(PaintEventArgs e) { 
     base.OnPaint(e); 

     // draw image here 
    } 

    public ToolTip ToolTip { get; set; } 

    protected override void OnMouseLeave(EventArgs e) { 
     base.OnMouseLeave(e); 

     if (this.ToolTip != null) 
      this.ToolTip.Hide(this); 
    } 

    protected override void OnMouseHover(EventArgs e) { 
     base.OnMouseHover(e); 

     if (this.ToolTip == null) 
      return; 

     Point pt = this.PointToClient(Cursor.Position); 
     String msg = this.CalculateMsgAt(pt); 
     if (String.IsNullOrEmpty(msg)) 
      return; 

     pt.Y += 20; 
     this.ToolTip.Show(msg, this, pt); 
    } 

    private string CalculateMsgAt(Point pt) { 
     // Calculate the message that should be shown 
     // when the mouse is at thegiven point 
     return "This is a tooltip"; 
    } 
} 
+0

ありがとう、私は明日それを試して、報告します。 – Travyguy9

1

あなたは店舗の境界あなたは を描いているとmouseMove eventチェックでその領域でのcurrent Mouse cursorの場所は、その後、他のツールヒントを表示する場合は画像のに持って、覚えておいてくださいそれを隠す。

ToolTip t; 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     t = new ToolTip(); //tooltip to control on which you are drawing your Image 
    } 

    Rectangle rect; //to store the bounds of your Image 
    private void Panel1_Paint(object sender, PaintEventArgs e) 
    { 
     rect =new Rectangle(50,50,200,200); // setting bounds to rect to draw image 
     e.Graphics.DrawImage(yourImage,rect); //draw your Image 
    } 

    private void Panel1_MouseMove(object sender, MouseEventArgs e) 
    { 

     if (rect.Contains(e.Location)) //checking cursor Location if inside the rect 
     { 
      t.SetToolTip(Panel1, "Hello");//setting tooltip to Panel1 
     } 
     else 
     { 
      t.Hide(Panel1); //hiding tooltip if the cursor outside the rect 
     } 
    } 
関連する問題