1 // Copyright (C) 2018 Kazuhiro Fujieda <fujieda@users.osdn.me>
\r
3 // Licensed under the Apache License, Version 2.0 (the "License");
\r
4 // you may not use this file except in compliance with the License.
\r
5 // You may obtain a copy of the License at
\r
7 // http://www.apache.org/licenses/LICENSE-2.0
\r
9 // Unless required by applicable law or agreed to in writing, software
\r
10 // distributed under the License is distributed on an "AS IS" BASIS,
\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 // See the License for the specific language governing permissions and
\r
13 // limitations under the License.
\r
16 using System.Drawing;
\r
17 using System.Runtime.InteropServices;
\r
18 using System.Windows.Forms;
\r
20 namespace KancolleSniffer.View
\r
22 public class ResizableToolTip : ToolTip
\r
24 private const TextFormatFlags TfFlags =
\r
25 TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPrefix;
\r
27 private readonly Brush _backBrush;
\r
28 private Size _padding;
\r
29 private int _iconSize;
\r
30 private int _iconPadding;
\r
32 public Font Font { get; set; }
\r
34 public ResizableToolTip()
\r
37 Font = SystemFonts.StatusFont;
\r
40 DwmIsCompositionEnabled(out var aero);
\r
41 _backBrush = new SolidBrush(aero ? Color.White : BackColor);
\r
43 AutoPopDelay = 30000;
\r
46 private void OnPopup(object sender, PopupEventArgs e)
\r
48 if (ToolTipIcon == ToolTipIcon.Error)
\r
51 _iconPadding = (int)Math.Round((Font.Height - _iconSize) / 2.0);
\r
53 _padding = new Size((int)Math.Round(Font.Height * 0.2), (int)Math.Round(Font.Height * 0.15));
\r
54 using (var g = Graphics.FromHwnd(e.AssociatedWindow.Handle))
\r
56 var size = TextRenderer.MeasureText(g, GetToolTip(e.AssociatedControl), Font,
\r
57 new Size(int.MaxValue, int.MaxValue), TfFlags);
\r
58 e.ToolTipSize = new Size(size.Width + _padding.Width * 2 + _iconSize,
\r
59 size.Height + _padding.Height * 2);
\r
63 private void OnDraw(object sender, DrawToolTipEventArgs e)
\r
65 e.Graphics.FillRectangle(_backBrush, e.Bounds);
\r
66 e.Graphics.DrawRectangle(SystemPens.ControlDarkDark,
\r
67 new Rectangle(e.Bounds.Location, new Size(e.Bounds.Width - 1, e.Bounds.Height - 1)));
\r
68 TextRenderer.DrawText(e.Graphics, e.ToolTipText, Font,
\r
69 new Rectangle(e.Bounds.X + _padding.Width + _iconSize,
\r
70 e.Bounds.Y + _padding.Height, e.Bounds.Width - _padding.Width * 2,
\r
71 e.Bounds.Height - _padding.Height * 2),
\r
72 Color.Black, TfFlags);
\r
73 if (ToolTipIcon != ToolTipIcon.Error)
\r
75 e.Graphics.DrawIcon(SystemIcons.Error,
\r
76 new Rectangle(e.Bounds.X + _padding.Width + _iconPadding,
\r
77 e.Bounds.Y + _padding.Height + _iconPadding, _iconSize, _iconSize));
\r
80 [DllImport("dwmapi.dll")]
\r
81 private static extern IntPtr DwmIsCompositionEnabled(out bool pfEnabled);
\r