OSDN Git Service

no message
[passer/Passer2.git] / passer / ToolTipForm.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Diagnostics;
9 using Lugens.Utils;
10 using System.Runtime.InteropServices;
11
12 namespace Lugens.Passer
13 {
14     public partial class ToolTipForm : Form
15     {
16         private const int SWP_NOSIZE = 1;
17         private const int SWP_NOMOVE = 2;
18         private const int SWP_NOACTIVATE = 0x10;
19         private const int HWND_TOPMOST = -1;
20         private const int GWL_EXSTYLE = -20;
21
22         private Point drawPoint = new Point(0, 0);
23
24         //private Size calcSize;
25
26         private int visibledTime = 0;
27         private int displayedTime = 0;
28
29         /// <summary>
30         /// 表示されて可視状態かどうか
31         /// </summary>
32         private bool displayed = false;
33
34         /// <summary>
35         /// 表示時間(ms)
36         /// </summary>
37         private int duration = 5000;
38         public int Duration
39         {
40             get { return duration; }
41             set { duration = value; }
42         }
43         
44
45         /// <summary>
46         /// 表示までのディレイ時間(ms)
47         /// </summary>
48         private int startDelay = 200;
49         public int StartDelay
50         {
51             get { return startDelay; }
52             set { startDelay = value; }
53         }
54
55         private int borderSpace;
56         public int BorderSpace
57         {
58             get { return borderSpace; }
59             set
60             {
61                 borderSpace = value;
62                 drawPoint.X = drawPoint.Y = value;
63             }
64         }
65         private bool alive = true;
66         public bool Alive
67         {
68             get { return this.alive; }
69         }
70
71         public ToolTipForm() : this(false)
72         {
73         }
74
75         public ToolTipForm(bool top)
76         {
77             this.TopMost = top;
78             InitializeComponent();
79             this.Paint += this.OnPaint;
80             this.BorderSpace = 3;
81         }
82
83         protected override void CreateHandle()
84         {
85             base.CreateHandle();
86             Win32.SetWindowPos(new HandleRef(this, this.Handle), (IntPtr)HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE);
87         }
88
89         protected override CreateParams CreateParams
90         {
91             get
92             {
93                 CreateParams cp = base.CreateParams;
94                 cp.ClassStyle |= 0x00020000;
95                 cp.ExStyle |= Win32.WS_EX_NOACTIVATE;
96                 return cp;
97             }
98         }
99
100         protected override bool ShowWithoutActivation
101         {
102             get { return true; }
103         }
104
105         private void OnPaint(object sender, PaintEventArgs e)
106         {
107             
108             TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.drawPoint, this.ForeColor, TextFormatFlags.NoPrefix);
109             using(Pen pen = new Pen(this.ForeColor, 1.0F)){
110                 e.Graphics.DrawRectangle(pen, 0, 0, this.ClientSize.Width - 1, this.ClientSize.Height - 1);
111             }
112         }
113
114         public override string Text
115         {
116             get { return base.Text; }
117             set
118             {
119                 int borderSpace = this.borderSpace << 1;
120                 base.Text = value.Replace("\t", "    ");
121                 Size size = TextRenderer.MeasureText(base.Text, this.Font);
122                 size.Width += borderSpace;
123                 size.Height += borderSpace;
124                 this.Size = size;
125             }
126         }
127
128         public void Show(Point p, int duration, bool fadeIn)
129         {
130             if(fadeIn)
131                 this.Opacity = 0D;
132             this.displayed = false;
133             this.duration = duration;
134             this.Location = p;
135             this.visibledTime = System.Environment.TickCount;
136             this.timer1.Enabled = true;
137             //Size size = this.Size;
138             //this.Show();
139             //this.Size = size;
140             this.Invalidate();
141         }
142
143         public void Show(string text, Point p, int duration, bool fadeIn)
144         {
145             this.Text = text;
146             this.Show(p, duration, fadeIn);
147         }
148
149         private void timer1_Tick(object sender, EventArgs e)
150         {
151             int tickCount = System.Environment.TickCount;
152             if (tickCount > this.visibledTime + this.startDelay)
153             {
154                 if (!displayed){
155                     this.displayedTime = tickCount;
156                     this.displayed = true;
157                     Size size = this.Size;
158                     this.Show();
159                     this.Size = size;
160                 }
161
162                 if (tickCount - this.displayedTime > this.duration)
163                 {
164                     this.Opacity -= Program.FadeInSpeed;
165                     if (this.Opacity <= 0D)
166                     {
167                         this.timer1.Enabled = false;
168                         this.Close();
169                     }
170                 }
171                 else
172                 {
173                     if (this.Opacity + Program.FadeInSpeed < Program.FormOpacity)
174                         this.Opacity += Program.FadeInSpeed;
175                     else
176                         this.Opacity = Program.FormOpacity;
177                 }
178
179             }
180         }
181
182         private void ToolTipForm_Resize(object sender, EventArgs e)
183         {
184             this.Invalidate();
185         }
186
187         private void ToolTipForm_FormClosing(object sender, FormClosingEventArgs e)
188         {
189             this.alive = false;
190         }
191
192     }
193 }