private void TipCapture() { IntPtr handle = Handle; if (handle == IntPtr.Zero) { return; } RECT rect = new RECT(); NativeMethodsToop.GetWindowRect(handle, ref rect); Size size = new Size( rect.Right - rect.Left, rect.Bottom - rect.Top); _backDc = new ImageDc(size.Width, size.Height); IntPtr pD = NativeMethodsToop.GetDesktopWindow(); IntPtr pH = NativeMethodsToop.GetDC(pD); NativeMethodsToop.BitBlt( _backDc.Hdc, 0, 0, size.Width, size.Height, pH, rect.Left, rect.Top, 0xCC0020); NativeMethodsToop.ReleaseDC(pD, pH); }
private void ToolTipExDraw( object sender, DrawToolTipEventArgs e) { Graphics g = e.Graphics; g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; g.SmoothingMode = SmoothingMode.HighQuality; Rectangle bounds = e.Bounds; int alpha = (int)(_opacity * 255); int defaultXOffset = 12; int defaultTopHeight = 36; int tipTextXOffset = 3; int tipTextYOffset = 3; if (Handle != IntPtr.Zero && _opacity < 1D) { IntPtr hDC = g.GetHdc(); NativeMethodsToop.BitBlt( hDC, 0, 0, bounds.Width, bounds.Height, _backDc.Hdc, 0, 0, 0xCC0020); g.ReleaseHdc(hDC); } Color backNormalColor = Color.FromArgb( alpha, ColorTable.BackNormal); Color baseColor = Color.FromArgb( alpha, ColorTable.BackHover); Color borderColor = Color.FromArgb( alpha, ColorTable.Border); #region 自画三角 //int spos = 3; //Rectangle tmp=new Rectangle(bounds.Location,new Size(bounds.Width-1,bounds.Height-1)); //int tmpVal = 2; //PointF[] pitems = new PointF[]{ //new Point(0,tmpVal), new Point(tmpVal,0), //new Point(tmp.Width-tmpVal,0), new Point(tmp.Width,tmpVal), //new Point(tmp.Width,tmp.Height-tmpVal), new Point(tmp.Width-tmpVal,tmp.Height), //new Point(tmpVal,tmp.Height), new Point(0,tmp.Height-tmpVal) //}; //switch (d) //{ // case dur.left: // //pitems_left // pitems = new PointF[]{ // new Point(spos,tmpVal),new Point(spos+tmpVal,0), // new Point(tmp.Width-tmpVal,0), new Point(tmp.Width,tmpVal), // new Point(tmp.Width,tmp.Height-tmpVal),new Point(tmp.Width-tmpVal,tmp.Height), // new Point(spos+tmpVal,tmp.Height), new Point(spos,tmp.Height-tmpVal), // new PointF(spos,tmp.Height/2-3),new PointF(0,tmp.Height/2),new PointF(spos,tmp.Height/2+3) //}; // break; // case dur.top: // //pitems_Top // pitems = new PointF[]{ // new Point(0,spos+tmpVal),new Point(tmpVal,spos), // new PointF((tmp.Width-spos)/2-3,spos),new PointF((tmp.Width-spos)/2,0),new PointF((tmp.Width-spos)/2+3,spos), // //new PointF((tmp.Width-spos)*4/9,spos),new PointF((tmp.Width-spos)/2,0),new PointF((tmp.Width-spos)*5/9,spos), // new Point(tmp.Width-tmpVal,spos), new Point(tmp.Width,spos+tmpVal), // new Point(tmp.Width,tmp.Height-tmpVal), // new Point(tmp.Width-tmpVal,tmp.Height), // new Point(tmpVal,tmp.Height), // new Point(0,tmp.Height-tmpVal) //}; // break; // case dur.right: // //pitems_Right // pitems = new PointF[]{ // new Point(0,tmpVal),new Point(tmpVal,0), // new Point((tmp.Width-spos)-tmpVal,0), new Point((tmp.Width-spos),tmpVal), // new PointF((tmp.Width-spos),tmp.Height/2-3),new PointF((tmp.Width),tmp.Height/2),new PointF((tmp.Width-spos),tmp.Height/2+3), // //new PointF((tmp.Width-spos),tmp.Height*3/7),new PointF((tmp.Width),tmp.Height/2),new PointF((tmp.Width-spos),tmp.Height*4/7), // new Point(tmp.Width-spos,tmp.Height-tmpVal), new Point(tmp.Width-spos-tmpVal,tmp.Height), // new Point(tmpVal,tmp.Height), new Point(0,tmp.Height-tmpVal) //}; // break; // case dur.bottom: // //pitems_Bottom // pitems = new PointF[]{ // new Point(0,tmpVal),new Point(tmpVal,0), // new Point((tmp.Width)-tmpVal,0), new Point((tmp.Width),tmpVal), // new PointF(tmp.Width,tmp.Height-spos-tmpVal), new PointF(tmp.Width-tmpVal,tmp.Height-spos), // new Point(tmp.Width/2-3,tmp.Height-spos), new Point(tmp.Width/2,tmp.Height), new Point(tmp.Width/2+3,tmp.Height-spos), // //new Point(tmp.Width*3/7,tmp.Height-spos), new Point(tmp.Width/2,tmp.Height), new Point(tmp.Width*4/7,tmp.Height-spos), // new Point(tmpVal,tmp.Height-spos),new Point(0,tmp.Height-spos-tmpVal) // }; // break; //} #endregion Bitmap Img = Resources.DNAniu_Bottom; switch (d) { case dur.bottom: Img = Resources.DNAniu_Bottom; break; case dur.left: Img = Resources.left_82; break; case dur.right: Img = Resources.right_82; break; case dur.top: Img = Resources.down_82; break; default: Img = Resources.down_82; break; } //背景 using (LinearGradientBrush brush = new LinearGradientBrush( bounds, backNormalColor, baseColor, LinearGradientMode.Vertical)) { if (_opacity == 1D) { TipCapture(); IntPtr hDC = g.GetHdc(); NativeMethodsToop.BitBlt( hDC, 0, 0, bounds.Width, bounds.Height, _backDc.Hdc, 0, 0, 0xCC0020); g.ReleaseHdc(hDC); } if (d != dur.normal) { g.DrawImage(Img, bounds); } else { //int spos = 3; Rectangle tmp = new Rectangle(bounds.Location, new Size(bounds.Width - 1, bounds.Height - 1)); int tmpVal = 2; PointF[] pitems = new PointF[] { new Point(0, tmpVal), new Point(tmpVal, 0), new Point(tmp.Width - tmpVal, 0), new Point(tmp.Width, tmpVal), new Point(tmp.Width, tmp.Height - tmpVal), new Point(tmp.Width - tmpVal, tmp.Height), new Point(tmpVal, tmp.Height), new Point(0, tmp.Height - tmpVal) }; g.FillPolygon(new SolidBrush(Color.White), pitems); g.DrawPolygon(new Pen(FrmStyle.BorderColor), pitems); } } #region border // g.DrawPolygon(new Pen(borderColor), pitems); #endregion //背景 //using (LinearGradientBrush brush = new LinearGradientBrush( // bounds, // backNormalColor, // baseColor, // LinearGradientMode.Vertical)) //{ // g.FillPolygon(brush, pitems); //} Rectangle imageRect = Rectangle.Empty; Rectangle titleRect; Rectangle tipRect; if (base.ToolTipIcon != ToolTipIcon.None) { tipTextXOffset = defaultXOffset; tipTextYOffset = defaultTopHeight; imageRect = new Rectangle( bounds.X + defaultXOffset - (ImageSize.Width - 16) / 2, bounds.Y + (defaultTopHeight - _imageSize.Height) / 2, _imageSize.Width, _imageSize.Height); Image image = _image; bool bDispose = false; if (image == null) { Icon icon = GetIcon(); if (icon != null) { image = icon.ToBitmap(); bDispose = true; } } if (image != null) { using (InterpolationModeGraphics ig = new InterpolationModeGraphics(g)) { if (_opacity < 1D) { RenderHelper.RenderAlphaImage( g, image, imageRect, (float)_opacity); } else { g.DrawImage( image, imageRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); } } if (bDispose) { image.Dispose(); } } } StringFormat sf = new StringFormat(); sf.LineAlignment = StringAlignment.Center; int sposTitle = 2; if (!string.IsNullOrEmpty(base.ToolTipTitle)) { tipTextXOffset = defaultXOffset; tipTextYOffset = defaultTopHeight; int x = imageRect.IsEmpty ? defaultXOffset : imageRect.Right + 3; titleRect = new Rectangle( x + sposTitle, bounds.Y + sposTitle, bounds.Width - x, defaultTopHeight); Color foreColor = Color.FromArgb( alpha, ColorTable.TitleFore); using (Brush brush = new SolidBrush(foreColor)) { g.DrawString( base.ToolTipTitle, _titleFont, brush, titleRect, sf); } } if (!string.IsNullOrEmpty(e.ToolTipText)) { tipRect = new Rectangle( bounds.X + sposTitle + tipTextXOffset, bounds.Y + tipTextYOffset, bounds.Width - tipTextXOffset * 2, bounds.Height - tipTextYOffset); sf = StringFormat.GenericTypographic; Color foreColor = Color.FromArgb( alpha, ColorTable.TipFore); //sf.Alignment = StringAlignment.Center; using (Brush brush = new SolidBrush(foreColor)) { g.DrawString( e.ToolTipText, _tipFont, brush, tipRect, sf); } } }