编写DLL--Balloon
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Balloon
{
//Defines a set of standardized icons that can be associated with a ToolTip
public enum TooltipIcon : int
{
None, //Not a standard icon.
Info, //An information icon.
Warning, //A warning icon.
Error //An error icon
}
public class XPBalloon
{
private Control _parent;
private string _text = "Balloon Tooltip Control Display Message";
private string _title = "Balloon Tooltip Message";
private TooltipIcon _titleIcon = TooltipIcon.None;
private const int ECM_FIRST = 0x1500;
private const int EM_SHOWBALLOONTIP = ECM_FIRST + 3;
[DllImport("User32",SetLastError=true)]
/*SendMessage:它把一条消息发送给指定窗口的窗口过程,
而且等待该窗口过程完成消息的处理之后才会返回。
当需要知道某个消息的处理结果时,
使用该函数发送消息,然后根据其返回值进行处理。
*/
private static extern int SendMessage(IntPtr hWnd,int Msg,int wParam,IntPtr lParam);
//StructLayoutAttribute 类使用户可以控制类或结构的数据字段的物理布局
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
private struct BALLOONTIP
{
public int cbStruct;
public string pszTitle;
public string pszText;
public int ttiIcon;
}
public XPBalloon()
{
}
public XPBalloon(Control parent)
{
_parent = parent;
}
///
/// Show a balloon tooltip for edit control.
///
public void balloonShow()
{
BALLOONTIP balloonTip = new BALLOONTIP();
/*
*Marshal:提供了一个方法集,这些方法用于分配非托管内存、复制非托管内存块、
*将托管类型转换为非托管类型,此外还提供了在与非托管代码交互时使用的其他杂项方法。
*/
balloonTip.cbStruct = Marshal.SizeOf(balloonTip);
balloonTip.pszText = _text;
balloonTip.pszTitle = _title;
balloonTip.ttiIcon = (int)_titleIcon;
//IntPtr : 用于表示指针或句柄的平台特定类型。
//AllocHGlobal : 使用 GlobalAlloc 从进程的非托管内存中分配内存。
IntPtr ptrStruct = Marshal.AllocHGlobal(Marshal.SizeOf(balloonTip));
//StructureToPtr:将数据从托管对象封送到非托管内存块。
Marshal.StructureToPtr(balloonTip, ptrStruct, true);
System.Diagnostics.Debug.Assert(_parent!=null, "Parent control is null", "Set parent before calling Show");
int ret = SendMessage(_parent.Handle,EM_SHOWBALLOONTIP,0, ptrStruct);
//FreeHGlobal : 释放以前使用 AllocHGlobal 从进程的非托管内存中分配的内存。
Marshal.FreeHGlobal(ptrStruct);
}
///
/// Sets or gets the Title.
///
public string balloonTitle
{
get
{
return _title;
}
set
{
_title = value;
}
}
///
/// Sets or gets the display icon.
///
public TooltipIcon balloonTitleIcon
{
get
{
return _titleIcon;
}
set
{
_titleIcon = value;
}
}
///
/// Sets or gets the display text.
///
public string balloonText
{
get
{
return _text;
}
set
{
_text = value;
}
}
///
/// Sets or gets the parent.
///
public Control balloonParent
{
get
{
return _parent;
}
set
{
_parent = value;
}
}
}
}
2005年10月31日20:17:41