public FmTester() { this.DoubleBuffered = true; InitializeComponent(); _style = new TipStyle(); propertyGrid1.SelectedObject = _style; }
/// <summary> /// 显示消息 /// </summary> /// <param name="text">消息文本</param> /// <param name="style">消息样式。不指定则使用默认样式</param> /// <param name="delay">消息停留时长(ms)。为负时使用全局时长</param> /// <param name="floating">是否漂浮,不指定则使用全局设置</param> /// <param name="point">消息窗显示位置。不指定则智能判定,当由工具栏项(ToolStripItem)弹出时,请指定该参数或使用接收控件的重载</param> /// <param name="centerByPoint">是否以point参数为中心进行呈现。为false则是在其附近呈现</param> public static void Show(string text, TipStyle style = null, int delay = -1, bool?floating = null, Point?point = null, bool centerByPoint = false) { var basePoint = point ?? DetemineActive(); new Thread(arg => { LayeredWindow layer = null; try { layer = new LayeredWindow(CreateTipImage(text ?? string.Empty, style ?? DefaultStyle ?? TipStyle.Gray, out Rectangle contentBounds)) { Alpha = 0, Location = GetLocation(contentBounds, basePoint, centerByPoint, out var floatDown), MouseThrough = true, TopMost = true, Tag = new object[] { delay < 0 ? Delay : delay, floating ?? Floating, floatDown } }; layer.Showing += layer_Showing; layer.Closing += layer_Closing; layer.Show(); } finally { if (layer != null) { layer.Showing -= layer_Showing; layer.Closing -= layer_Closing; layer.Dispose(); } } }) { IsBackground = true, Name = "T_Showing" }.Start();
/// <summary> /// 在指定控件附近显示消息 /// </summary> /// <param name="controlOrItem">控件或工具栏项</param> /// <param name="text">消息文本</param> /// <param name="style">消息样式。不指定则使用默认样式</param> /// <param name="delay">消息停留时长(ms)。为负时使用全局时长</param> public static void Show(Component controlOrItem, string text, TipStyle style = null, int delay = -1) { if (controlOrItem == null) { throw new ArgumentNullException("controlOrItem"); } Show(text, style, delay, GetCenterPosition(controlOrItem), !(controlOrItem is ButtonBase || controlOrItem is ToolStripItem)); }
/// <summary> /// 在指定控件附近显示消息 /// </summary> /// <param name="controlOrItem">控件或工具栏项</param> /// <param name="text">消息文本</param> /// <param name="style">消息样式。不指定则使用默认样式</param> /// <param name="delay">消息停留时长(ms)。为负时使用全局时长</param> /// <param name="floating">是否漂浮,不指定则使用全局设置</param> /// <param name="centerInControl">是否在控件中央显示,不指定则自动判断</param> public static void Show(Component controlOrItem, string text, TipStyle style = null, int delay = -1, bool?floating = null, bool?centerInControl = null) { if (controlOrItem == null) { throw new ArgumentNullException("controlOrItem"); } Show(text, style, delay, floating, GetCenterPosition(controlOrItem), centerInControl ?? IsContainerLike(controlOrItem)); }
/// <summary> /// 显示消息 /// </summary> /// <param name="text">消息文本</param> /// <param name="style">消息样式。不指定则使用默认样式</param> /// <param name="delay">消息停留时长(ms)。为负时使用全局时长</param> /// <param name="floating">是否漂浮,不指定则使用全局设置</param> /// <param name="point">消息窗显示位置。不指定则智能判定,当由工具栏项(ToolStripItem)弹出时,请指定该参数或使用接收控件的重载</param> /// <param name="centerByPoint">是否以point参数为中心进行呈现。为false则是在其附近呈现</param> public static void Show(string text, TipStyle style = null, int delay = -1, bool?floating = null, Point?point = null, bool centerByPoint = false) { var basePoint = point ?? DetemineActive(); new Thread(arg => { var args = (object[])arg; var tBasePoint = (Point)args[0]; var tStyle = (TipStyle)args[1]; var tDelay = (int)args[2]; var tFloating = (bool)args[3]; var tCenterByPoint = (bool)args[4]; LayeredWindow layer = null; try { bool floatDown; Rectangle contentBounds; layer = new LayeredWindow(CreateTipImage(text ?? string.Empty, tStyle ?? DefaultStyle ?? TipStyle.Gray, out contentBounds)) { Alpha = 0, Location = GetLocation(contentBounds, tBasePoint, tCenterByPoint, out floatDown), MouseThrough = true, TopMost = true, Tag = new object[] { tDelay, tFloating, floatDown } }; layer.Showing += layer_Showing; layer.Closing += layer_Closing; layer.Show(); } finally { if (layer != null) { layer.Showing -= layer_Showing; layer.Closing -= layer_Closing; layer.Dispose(); } } }) { IsBackground = true, Name = "T_Showing" }.Start(new object[] { basePoint, style, delay < 0?DefaultDelay:delay, floating ?? Floating, centerByPoint }); }
private static TipStyle CreatePresetsStyle(int index) { var style = new TipStyle { Icon = PresetsResources.Icons[index], BorderColor = PresetsResources.Colors[index, 0], ShadowColor = PresetsResources.Colors[index, 2], }; style.BackBrush = r => { var brush = new LinearGradientBrush(r, PresetsResources.Colors[index, 1], Color.White, LinearGradientMode.Horizontal); brush.SetBlendTriangularShape(0.5f); return(brush); }; return(style); }
private void btnRestore_Click(object sender, EventArgs e) { _style.Clear(); _style = new TipStyle(); propertyGrid1.SelectedObject = _style; }
/// <summary> /// 创建消息窗图像,同时输出内容区,用于外部定位 /// </summary> private static Bitmap CreateTipImage(string text, TipStyle style, out Rectangle contentBounds) { var size = Size.Empty; var iconBounds = Rectangle.Empty; var textBounds = Rectangle.Empty; if (style.Icon != null) { size = style.Icon.Size; iconBounds.Size = size; textBounds.X = size.Width; } if (text.Length != 0) { if (style.Icon != null) { size.Width += style.IconSpacing; textBounds.X += style.IconSpacing; } textBounds.Size = Size.Truncate(GraphicsUtils.MeasureString(text, style.TextFont ?? DefaultFont, 0, DefStringFormat)); size.Width += textBounds.Width; if (size.Height < textBounds.Height) { size.Height = textBounds.Height; } else if (size.Height > textBounds.Height)//若文字没有图标高,令文字与图标垂直居中,否则与图标平齐 { textBounds.Y += (size.Height - textBounds.Height) / 2; } textBounds.Offset(style.TextOffset); } size += style.Padding.Size; iconBounds.Offset(style.Padding.Left, style.Padding.Top); textBounds.Offset(style.Padding.Left, style.Padding.Top); contentBounds = new Rectangle(Point.Empty, size); var fullBounds = GraphicsUtils.GetBounds(contentBounds, style.Border, style.ShadowRadius, style.ShadowOffset.X, style.ShadowOffset.Y); contentBounds.Offset(-fullBounds.X, -fullBounds.Y); iconBounds.Offset(-fullBounds.X, -fullBounds.Y); textBounds.Offset(-fullBounds.X, -fullBounds.Y); var bmp = new Bitmap(fullBounds.Width, fullBounds.Height); Graphics g = null; Brush backBrush = null; Brush textBrush = null; try { g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.HighQuality; g.PixelOffsetMode = PixelOffsetMode.HighQuality; backBrush = (style.BackBrush ?? (r => new SolidBrush(style.BackColor)))(contentBounds); GraphicsUtils.DrawRectangle(g, contentBounds, backBrush, style.Border, style.CornerRadius, style.ShadowColor, style.ShadowRadius, style.ShadowOffset.X, style.ShadowOffset.Y); if (style.Icon != null) { //DEBUG: g.DrawRectangle(new Border(Color.Red) { Width = 1, Direction = Direction.Inner }.Pen, iconBounds); g.DrawImageUnscaled(style.Icon, iconBounds.Location); } if (text.Length != 0) { textBrush = new SolidBrush(style.TextColor); //DEBUG: g.DrawRectangle(new Border(Color.Red){ Width=1, Direction= Direction.Inner}.Pen, textBounds); g.DrawString(text, style.TextFont ?? DefaultFont, textBrush, textBounds.Location, DefStringFormat); } g.Flush(FlushIntention.Sync); return(bmp); } finally { if (g != null) { g.Dispose(); } if (backBrush != null) { backBrush.Dispose(); } if (textBrush != null) { textBrush.Dispose(); } } }