using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using TigerLeqClassLibrary.Utils.Win32;
namespace TigerLeqWindowsControlLibrary.Controls
{
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(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;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TigerLeqWindowsControlLibrary.Controls
{
public enum BalloonAlignment
{
TopLeft,
TopMiddle,
TopRight,
LeftMiddle,
RightMiddle,
BottomLeft,
BottomMiddle,
BottomRight
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace TigerLeqWindowsControlLibrary.Controls
{
public class BalloonHelper
{
private static ImpsBalloon _balloon;
private BalloonHelper()
{
}
public static void RemoveAll()
{
if (_balloon != null)
{
_balloon.Close();
_balloon.Dispose();
_balloon = null;
}
}
public static void ShowBallon(Control c, string message)
{
ShowBallon(c, message, -1);
}
public static void ShowBallon(Control c, string message, Point location)
{
ShowBallon(c, message, string.Empty, location);
}
public static void ShowBallon(Control c, string message, int duration)
{
ShowBallon(c, message, string.Empty, duration);
}
public static void ShowBallon(Control c, string message, string title, Point location)
{
ShowBallon(c, message, title, ToolTipIcon.Info, location);
}
public static void ShowBallon(Control c, string message, string title, int duration)
{
ShowBallon(c, message, title, ToolTipIcon.Info, duration);
}
public static void ShowBallon(Control c, string message, string title, ToolTipIcon icon)
{
ShowBallon(c, message, title, icon, Point.Empty, -1);
}
public static void ShowBallon(Control c, string message, string title, ToolTipIcon icon, Point location)
{
ShowBallon(c, message, title, icon, location, -1);
}
public static void ShowBallon(Control c, string message, string title, ToolTipIcon icon, int duration)
{
ShowBallon(c, message, title, icon, Point.Empty, duration);
}
public static void ShowBallon(Control c, string message, string title, ToolTipIcon icon, Point location, int duration)
{
RemoveAll();
_balloon = new ImpsBalloon();
_balloon.ParentControl = c;
_balloon.Title = title;
_balloon.Text = message;
if (duration > 0)
{
_balloon.Duration = duration;
}
switch (icon)
{
case ToolTipIcon.None:
_balloon.Icon = BalloonIcon.None;
break;
case ToolTipIcon.Info:
_balloon.Icon = BalloonIcon.Info;
break;
case ToolTipIcon.Warning:
_balloon.Icon = BalloonIcon.Warning;
break;
case ToolTipIcon.Error:
_balloon.Icon = BalloonIcon.Error;
break;
}
_balloon.Alignment = BalloonAlignment.BottomMiddle;
_balloon.IsPositionAbsolute = true;
_balloon.IsStemCentered = true;
try
{
_balloon.Show();
}
catch (Exception)
{
}
}
public static void ShowInputErrorBallon(Control c, string message)
{
ShowInputErrorBallon(c, message, string.Empty, 0x7d0);
}
public static void ShowInputErrorBallon(Control c, string message, string title)
{
ShowInputErrorBallon(c, message, title, 0x7d0);
}
public static void ShowInputErrorBallon(Control c, string message, string title, int duration)
{
if (c is TextBoxBase)
{
((TextBoxBase)c).SelectAll();
}
else if (c is ComboBox)
{
((ComboBox)c).SelectAll();
}
c.Focus();
ShowBallon(c, message, title, ToolTipIcon.Error, duration);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TigerLeqWindowsControlLibrary.Controls
{
public enum BalloonIcon
{
None,
Info,
Warning,
Error
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TigerLeqWindowsControlLibrary.Controls
{
public enum BalloonPosition
{
Absolute,
Track
}
}
using System;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
namespace TigerLeqWindowsControlLibrary.Controls
{
internal class BalloonWindow : NativeWindow
{
private const int WM_LBUTTONDOWN = 0x201;
public event BalloonWindowClosedEventHandler WindowClosed;
protected override void WndProc(ref Message m)
{
if ((m.Msg == 0x201) && (this.WindowClosed != null))
{
this.WindowClosed();
}
base.WndProc(ref m);
}
}
}
using System;
using System.Runtime.CompilerServices;
namespace TigerLeqWindowsControlLibrary.Controls
{
public delegate void BalloonWindowClosedEventHandler();
}
using System;
using System.Runtime.InteropServices;
namespace TigerLeqClassLibrary.Utils.Win32
{
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
private static RECT _empty;
public int Width
{
get
{
return (this.right - this.left);
}
}
public int Height
{
get
{
return (this.bottom - this.top);
}
}
public static RECT Empty
{
get
{
return _empty;
}
}
static RECT()
{
_empty = new RECT();
}
}
}
//控件使用
BalloonHelper.ShowBallon(this.tb_sell_bill_id, "内容", "标题", ToolTipIcon.Info, 0x5dc);