[Klezard]插入符组件(Caret)

关于runtime的转载内容
此博客为转载内容,原文链接为https://www.cnblogs.com/zealic/archive/2005/03/16/119849.html ,涉及runtime相关知识。
在有些时候,我们需要在自己的控件中加入象TextBox一样的插入符
这个功能可以使用Windows API完成
因此我封装了Caret的API,形成了Caret组件,可以方便的在控件设计中使用


None.gifusing System;
None.gif
using System.Drawing;
None.gif
using System.Windows.Forms;
None.gif
using System.Runtime.InteropServices;
None.gif
None.gif
namespace Klezard.Windows.Forms
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 插入符
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public sealed class Caret : System.ComponentModel.Component
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Native#region Native
InBlock.gif        
private const string User32 = "User32.dll";
InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential)]
InBlock.gif        
public struct POINT
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public int x;
InBlock.gif            
public int y;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 产生一个新的系统插入符,并将其分配给指定的窗口
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="hWnd">窗口句柄</param>
InBlock.gif        
/// <param name="hBitmap">用作插入符的一幅位图的句柄.可以是0或1;在这种情况下,插入符可通过width和height参数创建.如设为1,则新插入符以灰色显示;而不是传统的黑色</param>
InBlock.gif        
/// <param name="nWidth">插入符宽度</param>
InBlock.gif        
/// <param name="nHeight">插入符宽度</param>
InBlock.gif        
/// <returns>成功创建返回True;失败返回False</returns>
ExpandedSubBlockEnd.gif        
/// <remarks>只有一个窗口得到键盘输入焦点或激活时才能给其创建插入符,在失去键盘输入焦点前应销毁插入符</remarks>

InBlock.gif        [DllImport(User32)]
InBlock.gif        
private static extern bool CreateCaret(IntPtr hWnd,bool hBitmap, int nWidth, int nHeight);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 使插入符出现在窗口的在插入符位置上,当插入符出现后即会自动闪烁
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="hWnd">窗口句柄</param>
InBlock.gif        
/// <returns>成功创建返回True;失败返回False</returns>
ExpandedSubBlockEnd.gif        
/// <remarks>只有窗口自己拥有插入符,且插入符有自己的形状,并且不被隐藏时才会出现,HideCaret是效果叠加的,举例说,如果你在程序中调过3次HideCaret,那就必需再调用3次ShowCaret </remarks>

InBlock.gif        [DllImport(User32)]
InBlock.gif        
private static extern bool ShowCaret(IntPtr hWnd);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 隐藏插入符,插入符隐藏后并没有破坏它的形状和插入符位置 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="hWnd">窗口句柄</param>
ExpandedSubBlockEnd.gif        
/// <returns>成功创建返回True;失败返回False</returns>

InBlock.gif        [DllImport(User32)]
InBlock.gif        
private static extern bool HideCaret(IntPtr hWnd);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 得到以毫秒为单位的插入符闪烁间隔 
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <returns>成功返回闪烁间隔;失败返回0</returns>

InBlock.gif        [DllImport(User32)]
InBlock.gif        
private static extern uint GetCaretBlinkTime();
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 以毫秒为单位的设置插入符闪烁间隔 
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="milliseconds">插入符闪烁间隔</param>
ExpandedSubBlockEnd.gif        
/// <returns>成功创建返回True;失败返回False</returns>

InBlock.gif        [DllImport(User32)]
InBlock.gif        
private static extern bool SetCaretBlinkTime(uint milliseconds);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 销毁指定窗口的插入符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="hWnd">窗口句柄</param>
InBlock.gif        
/// <returns>成功创建返回True;失败返回False</returns>
ExpandedSubBlockEnd.gif        
/// <remarks>只有窗口拥有插入符时才能销毁,否则该函数会立即返回FALSE</remarks>

InBlock.gif        [DllImport(User32)]
InBlock.gif        
private static extern bool DestroyCaret(IntPtr hWnd);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 移动插入符到指定位置
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="x">X坐标</param>
InBlock.gif        
/// <param name="y">Y坐标</param>
InBlock.gif        
/// <returns>成功创建返回True;失败返回False</returns>
ExpandedSubBlockEnd.gif        
/// <remarks>不管插入符是否可见,此函数都会移动插入符的位置</remarks>

InBlock.gif        [DllImport(User32)]
InBlock.gif        
private static extern bool SetCaretPos(int x, int y);
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 得到插入符的位置
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ppt">指向POINT结构的插入符位置 </param>
ExpandedSubBlockEnd.gif        
/// <returns>成功创建返回True;失败返回False</returns>

InBlock.gif        [DllImport(User32)]
InBlock.gif        
private static extern bool GetCaretPos(out POINT ppt);
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private Control _HostControl;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private Caret() dot.gif{ }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 创建一个插入符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ctl">插入符宿主</param>
ExpandedSubBlockEnd.gif        
/// <remarks>创建完成后插入符默认为隐藏的</remarks>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public Caret(Control ctl):this(ctl,8,16dot.gif{ }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 使用特定的大小创建一个插入符
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ctl">宿主</param>
InBlock.gif        
/// <param name="width">宽度</param>
InBlock.gif        
/// <param name="height">高度</param>
ExpandedSubBlockEnd.gif        
/// <remarks>创建完成后插入符默认为隐藏的</remarks>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public Caret(Control ctl, int width, int height):this(ctl,true,width,height)dot.gif{}
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 使用特定的大小创建一个插入符,可以指定黑色或灰色
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="ctl">插入符宿主</param>
InBlock.gif        
/// <param name="black">True为使用黑色,False为使用灰色</param>
InBlock.gif        
/// <param name="width">宽度</param>
InBlock.gif        
/// <param name="height">高度</param>
ExpandedSubBlockEnd.gif        
/// <remarks>创建完成后插入符默认为隐藏的</remarks>

InBlock.gif        public Caret(Control ctl,bool black, int width, int height)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _HostControl 
= ctl;
InBlock.gif            Caret.CreateCaret(
this._HostControl.Handle, !black, width, height);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 释放该插入符对象
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Destroy()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Caret.DestroyCaret(_HostControl.Handle);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 隐藏插入符,隐藏效果会叠加,即调用了几次Hide,要再次显示时就要调用几次Show
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Hide()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Caret.HideCaret(
this.HostControl.Handle);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 显示插入符
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public void Show()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Caret.ShowCaret(
this.HostControl.Handle);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 宿主窗体
ExpandedSubBlockEnd.gif        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public Control HostControl dot.gifget dot.gifreturn _HostControl; } }
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得或设置所有插入符闪烁时间间隔
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <value></value>

InBlock.gif        public static uint BlinkTime
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn Caret.GetCaretBlinkTime(); }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ Caret.SetCaretBlinkTime(value); }
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 获得或设置当前激活的控件的插入符的相对位置
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <value></value>

InBlock.gif        public static Point Position
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                POINT p 
= new POINT();
InBlock.gif                
if (Caret.GetCaretPos( out p))
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return new Point(p.x, p.y);
ExpandedSubBlockEnd.gif                }

InBlock.gif                
return Point.Empty;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{Caret.SetCaretPos( value.X, value.Y); }
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载请注明出处,如果您要使用该代码,请告之作者

转载于:https://www.cnblogs.com/zealic/archive/2005/03/16/119849.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值