用tooltip实现winform中的层的概念,在tooltip中绘制图形

今天做个项目,想将用户体验做好点,涉及到tooltip显示图片,查阅msdn后写出如下代码:效果不错:D(<==自我感觉良好= =#)呵呵!

完成以后猛然发现这个效果不正是前段时间苦苦寻觅的winform中的"层"效果吗?T_T 100分就这样飘远了~~

直奔主题

 

  1.  private void Form1_Load(object sender, EventArgs e)
  2.         {
  3.             this.toolTip1.SetToolTip( button1,"hi there!");
  4.             
  5.         }
  6.         private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
  7.         {
  8.             if (e.AssociatedControl == button1)
  9.             {
  10.                 
  11.                 e.DrawBackground();
  12.                 e.Graphics.DrawLines(SystemPens.ControlLightLight, new Point[] {
  13.                     new Point (0, e.Bounds.Height - 1), 
  14.                     new Point (0, 0), 
  15.                     new Point (e.Bounds.Width - 1, 0)
  16.                 });
  17.                 
  18.                 e.Graphics.DrawImage(bit, new Point(0, 0));
  19.                 
  20.                 e.Graphics.DrawLines(SystemPens.ControlDarkDark, new Point[] {
  21.                     new Point (0, e.Bounds.Height - 1), 
  22.                     new Point (e.Bounds.Width - 1, e.Bounds.Height - 1), 
  23.                     new Point (e.Bounds.Width - 1, 0)
  24.                 });
  25.                 // Specify custom text formatting flags.
  26.                 TextFormatFlags sf = TextFormatFlags.Right;//|
  27.                                      //TextFormatFlags.HorizontalCenter |
  28.                                      //TextFormatFlags.NoFullWidthCharacterBreak;
  29.                 // Draw the standard text with customized formatting options.
  30.                 e.DrawText(sf);
  31.                 
  32.                 
  33.             }
  34.         }
  35.         Bitmap bit;
  36.         private void toolTip1_Popup(object sender, PopupEventArgs e)
  37.         {
  38.             bit = new Bitmap(@"C:/Documents and Settings/Administrator/My Documents/My Pictures/e/resizable.gif");
  39.             int widt = TextRenderer.MeasureText(toolTip1.GetToolTip(e.AssociatedControl), this.Font).Width;
  40.             Size si = new Size(bit.Width+widt+20,bit.Height);
  41.             e.ToolTipSize = si;
  42.         }
posted @ 2008-08-04 16:43  Sean.Z  阅读(531)  评论(0)    收藏  举报