2011-11-25 116 views
6

我正在使用Windows窗体在c#中进行项目工作。 我和我所在的小组希望这样做,以便当用户将鼠标悬停在图像上时(在我们的情况下为卡片)时,该卡片的较大图像会出现在鼠标箭头旁边,非常类似于工具提示将工作。 我不认为你可以使用工具提示来做到这一点我已经试过到处寻找, 任何意见或例子将是巨大非常感谢你在windows窗体中的鼠标悬停上显示图像?

回答

7

你可能想看看这个Code Project Article

它展示了如何创建一个OwnerDrawn工具提示使用的图像。

+0

+1 @MarkHall伟大的小费! –

2

一个简单的方法做的是隐藏/显示图片框在指定的位置。另一种方法是使用GDI API加载&绘制(绘制)图像。

4

感谢您的回复,我已经弄清楚了一切。 我想要做的是,当我在某个区域上挖掘某个区域的不同图像时,会以与工具提示相同的方式弹出。所以经过一番研究之后,我想出了如何创建我自己的工具提示类。

这里有一个例子。

public partial class Form1 : Form 
{ 

    public Form1() 
    { 
     InitializeComponent(); 

     CustomToolTip tip = new CustomToolTip(); 
     tip.SetToolTip(button1, "text"); 
     tip.SetToolTip(button2, "writing"); 
     button1.Tag = Properties.Resources.pelican; // pull image from the resources file 
     button2.Tag = Properties.Resources.pelican2;  
    } 
} 

class CustomToolTip : ToolTip 
{ 
    public CustomToolTip() 
    { 
     this.OwnerDraw = true; 
     this.Popup += new PopupEventHandler(this.OnPopup); 
     this.Draw +=new DrawToolTipEventHandler(this.OnDraw); 
    } 

    private void OnPopup(object sender, PopupEventArgs e) // use this event to set the size of the tool tip 
    { 
     e.ToolTipSize = new Size(600, 1000); 
    } 

    private void OnDraw(object sender, DrawToolTipEventArgs e) // use this to customzie the tool tip 
    { 
     Graphics g = e.Graphics; 

     // to set the tag for each button or object 
     Control parent = e.AssociatedControl; 
     Image pelican = parent.Tag as Image; 

     //create your own custom brush to fill the background with the image 
     TextureBrush b = new TextureBrush(new Bitmap(pelican));// get the image from Tag 

     g.FillRectangle(b, e.Bounds); 
     b.Dispose(); 
    } 
} 

}

相关问题