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
44 private void OnPopup(object sender, PopupEventArgs e)
\r
46 if (ToolTipIcon == ToolTipIcon.Error)
\r
49 _iconPadding = (int)Math.Round((Font.Height - _iconSize) / 2.0);
\r
51 _padding = new Size((int)Math.Round(Font.Height * 0.2), (int)Math.Round(Font.Height * 0.15));
\r
52 using (var g = Graphics.FromHwnd(e.AssociatedWindow.Handle))
\r
54 var size = TextRenderer.MeasureText(g, GetToolTip(e.AssociatedControl), Font,
\r
55 new Size(int.MaxValue, int.MaxValue), TfFlags);
\r
56 e.ToolTipSize = new Size(size.Width + _padding.Width * 2 + _iconSize,
\r
57 size.Height + _padding.Height * 2);
\r
61 private void OnDraw(object sender, DrawToolTipEventArgs e)
\r
63 e.Graphics.FillRectangle(_backBrush, e.Bounds);
\r
64 e.Graphics.DrawRectangle(SystemPens.ControlDarkDark,
\r
65 new Rectangle(e.Bounds.Location, new Size(e.Bounds.Width - 1, e.Bounds.Height - 1)));
\r
66 TextRenderer.DrawText(e.Graphics, e.ToolTipText, Font,
\r
67 new Rectangle(e.Bounds.X + _padding.Width + _iconSize,
\r
68 e.Bounds.Y + _padding.Height, e.Bounds.Width - _padding.Width * 2,
\r
69 e.Bounds.Height - _padding.Height * 2),
\r
70 Color.Black, TfFlags);
\r
71 if (ToolTipIcon != ToolTipIcon.Error)
\r
73 e.Graphics.DrawIcon(SystemIcons.Error,
\r
74 new Rectangle(e.Bounds.X + _padding.Width + _iconPadding,
\r
75 e.Bounds.Y + _padding.Height + _iconPadding, _iconSize, _iconSize));
\r
78 [DllImport("dwmapi.dll")]
\r
79 private static extern IntPtr DwmIsCompositionEnabled(out bool pfEnabled);
\r