OSDN Git Service

装備の改修効果が索敵値に正しく反映されていないのを直す
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / ResizableToolTip.cs
1 // Copyright (C) 2018 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 //\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
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\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
14 \r
15 using System;\r
16 using System.Drawing;\r
17 using System.Runtime.InteropServices;\r
18 using System.Windows.Forms;\r
19 \r
20 namespace KancolleSniffer\r
21 {\r
22     public class ResizableToolTip : ToolTip\r
23     {\r
24         private const TextFormatFlags TfFlags =\r
25             TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.NoPrefix;\r
26 \r
27         private readonly Brush _backBrush;\r
28         private Size _padding;\r
29         private int _iconSize;\r
30         private int _iconPadding;\r
31 \r
32         public Font Font { get; set; }\r
33 \r
34         public ResizableToolTip()\r
35         {\r
36             OwnerDraw = true;\r
37             Font = SystemFonts.StatusFont;\r
38             Popup += OnPopup;\r
39             Draw += OnDraw;\r
40             DwmIsCompositionEnabled(out var aero);\r
41             _backBrush = new SolidBrush(aero ? Color.White : BackColor);\r
42         }\r
43 \r
44         private void OnPopup(object sender, PopupEventArgs e)\r
45         {\r
46             if (ToolTipIcon == ToolTipIcon.Error)\r
47             {\r
48                 _iconSize = 16;\r
49                 _iconPadding = (int)Math.Round((Font.Height - _iconSize) / 2.0);\r
50             }\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
53             {\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
58             }\r
59         }\r
60 \r
61         private void OnDraw(object sender, DrawToolTipEventArgs e)\r
62         {\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
72                 return;\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
76         }\r
77 \r
78         [DllImport("dwmapi.dll")]\r
79         private static extern IntPtr DwmIsCompositionEnabled(out bool pfEnabled);\r
80     }\r
81 }