📄 impsballoon.cs
字号:
namespace Imps.Client.Pc.Controls
{
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class ImpsBalloon : IDisposable
{
private BalloonAlignment _alignment = BalloonAlignment.BottomMiddle;
private BalloonWindow _balloonWindow = new BalloonWindow();
private bool _disposed;
private int _duration = 0xbb8;
private BalloonIcon _icon;
private bool _isPositionAbsolute;
private bool _isStemCentered;
private int _maxWidth = 250;
private Control _parentControl;
private BalloonPosition _position;
private string _text;
private Timer _timer;
private const int _timerInterval = 100;
private string _title;
private TOOLINFO _toolInfo;
public ImpsBalloon()
{
this._balloonWindow.WindowClosed += new BalloonWindowClosedEventHandler(this.Close);
this._timer = new Timer();
this._timer.Interval = 100;
this._timer.Tick += new EventHandler(this._timer_Tick);
}
private void _timer_Tick(object sender, EventArgs e)
{
this.Duration -= 100;
if (this.Duration <= 0)
{
this.Close();
}
}
private void BaseForm_Deactivate(object sender, EventArgs e)
{
this.Close();
}
public void Close()
{
IntPtr zero = IntPtr.Zero;
try
{
this._timer.Stop();
if (!this._balloonWindow.Handle.Equals(IntPtr.Zero))
{
zero = Marshal.AllocHGlobal(Marshal.SizeOf(this._toolInfo));
Marshal.StructureToPtr(this._toolInfo, zero, false);
ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x411, 0, zero);
}
}
catch (Exception)
{
}
finally
{
if (zero != IntPtr.Zero)
{
Marshal.FreeHGlobal(zero);
}
this._balloonWindow.DestroyHandle();
}
}
private void Control_HandleDestroyed(object sender, EventArgs e)
{
this.Close();
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
this.Close();
}
this._disposed = true;
}
private void SetBalloonPosition(Imps.Client.Pc.Controls.RECT rect)
{
int loWord = 0;
int hiWord = 0;
switch (this._alignment)
{
case BalloonAlignment.TopLeft:
loWord = rect.left;
hiWord = rect.top;
break;
case BalloonAlignment.TopMiddle:
loWord = rect.left + (rect.right / 2);
hiWord = rect.top;
break;
case BalloonAlignment.TopRight:
loWord = rect.left + rect.right;
hiWord = rect.top;
break;
case BalloonAlignment.LeftMiddle:
loWord = rect.left;
hiWord = rect.top + (rect.bottom / 2);
break;
case BalloonAlignment.RightMiddle:
loWord = rect.left + rect.right;
hiWord = rect.top + (rect.bottom / 2);
break;
case BalloonAlignment.BottomLeft:
loWord = rect.left;
hiWord = rect.top + rect.bottom;
break;
case BalloonAlignment.BottomMiddle:
loWord = rect.left + (rect.right / 2);
hiWord = rect.top + rect.bottom;
break;
case BalloonAlignment.BottomRight:
loWord = rect.left + rect.right;
hiWord = rect.top + rect.bottom;
break;
}
int num3 = ImpsNativeMethods.MAKELONG(loWord, hiWord);
IntPtr lParam = new IntPtr(num3);
ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x412, 0, lParam);
}
public void Show()
{
this.Close();
if (!this._timer.Enabled)
{
this._timer.Start();
}
CreateParams cp = new CreateParams();
cp.ClassName = "tooltips_class32";
cp.Style = -2147483453;
this._balloonWindow.CreateHandle(cp);
this._toolInfo = new TOOLINFO();
this._toolInfo.cbSize = Marshal.SizeOf(this._toolInfo);
this._toolInfo.uFlags = 0x1131;
if (this.IsPositionAbsolute)
{
this._toolInfo.uFlags |= 0x80;
}
if (this._isStemCentered)
{
this._toolInfo.uFlags |= 2;
}
this._toolInfo.uId = this._balloonWindow.Handle;
this._toolInfo.lpszText = this.Text;
this._toolInfo.hwnd = this._parentControl.Handle;
ImpsNativeMethods.GetClientRect(this._parentControl.Handle, ref this._toolInfo.rect);
ImpsNativeMethods.ClientToScreen(this._parentControl.Handle, ref this._toolInfo.rect);
ImpsNativeMethods.SetWindowPos(this._balloonWindow.Handle, ImpsNativeMethods.HWND_TOPMOST, 0, 0, 0, 0, 0x13);
IntPtr zero = IntPtr.Zero;
IntPtr lParam = IntPtr.Zero;
IntPtr ptr = IntPtr.Zero;
try
{
zero = Marshal.AllocHGlobal(Marshal.SizeOf(this._toolInfo));
Marshal.StructureToPtr(this._toolInfo, zero, false);
ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x432, 0, zero);
this._toolInfo = (TOOLINFO) Marshal.PtrToStructure(zero, typeof(TOOLINFO));
ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x418, 0, new IntPtr(this._maxWidth));
lParam = Marshal.StringToHGlobalAuto(this.Title);
ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x421, (int) this.Icon, lParam);
this.SetBalloonPosition(this._toolInfo.rect);
ptr = Marshal.AllocHGlobal(Marshal.SizeOf(this._toolInfo));
Marshal.StructureToPtr(this._toolInfo, ptr, false);
ImpsNativeMethods.SendMessage(this._balloonWindow.Handle, 0x411, -1, ptr);
}
finally
{
if (zero != IntPtr.Zero)
{
Marshal.FreeHGlobal(zero);
}
if (lParam != IntPtr.Zero)
{
Marshal.FreeHGlobal(lParam);
}
if (ptr != IntPtr.Zero)
{
Marshal.FreeHGlobal(ptr);
}
}
GC.KeepAlive(this);
}
public BalloonAlignment Alignment
{
get
{
return this._alignment;
}
set
{
this._alignment = value;
}
}
public int Duration
{
get
{
return this._duration;
}
set
{
if (value >= 0)
{
this._duration = value;
}
}
}
public BalloonIcon Icon
{
get
{
return this._icon;
}
set
{
this._icon = value;
}
}
public bool IsPositionAbsolute
{
get
{
return this._isPositionAbsolute;
}
set
{
this._isPositionAbsolute = value;
}
}
public bool IsStemCentered
{
get
{
return this._isStemCentered;
}
set
{
this._isStemCentered = value;
}
}
public Control ParentControl
{
get
{
return this._parentControl;
}
set
{
if ((value != null) && value.IsHandleCreated)
{
value.HandleDestroyed += new EventHandler(this.Control_HandleDestroyed);
if (value.FindForm() != null)
{
value.FindForm().Deactivate += new EventHandler(this.BaseForm_Deactivate);
value.FindForm().Move += new EventHandler(this.BaseForm_Deactivate);
}
}
if (this._parentControl != null)
{
this._parentControl.HandleDestroyed -= new EventHandler(this.Control_HandleDestroyed);
if (value.FindForm() != null)
{
this._parentControl.FindForm().Deactivate -= new EventHandler(this.BaseForm_Deactivate);
this._parentControl.FindForm().Move += new EventHandler(this.BaseForm_Deactivate);
}
}
this._parentControl = value;
}
}
public BalloonPosition Position
{
get
{
return this._position;
}
set
{
this._position = value;
}
}
public string Text
{
get
{
return this._text;
}
set
{
this._text = value;
}
}
public string Title
{
get
{
return this._title;
}
set
{
this._title = value;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -