TextBoxButton控件的开发实现

效果图:

TextBoxButton控件的开发实现

实现代码:

    public TextBoxButton()
{
_button = new Button
{
ForeColor = System.Drawing.SystemColors.GrayText,
Name = "button1",
Padding = new System.Windows.Forms.Padding(, , , ),
Text = "…",
UseVisualStyleBackColor = true,
Size = new System.Drawing.Size(, ),
Dock = DockStyle.Right
}; this._button.SizeChanged += (o, e) => OnResize(e);
this._button.MouseMove += (o, e) => { this.Cursor = Cursors.Default; };
this.Controls.Add(_button);
} protected override void OnResize(EventArgs e)
{
base.OnResize(e);
_button.Size = new Size(_button.Width, this.ClientSize.Height + );
_button.Location = new Point(this.ClientSize.Width - _button.Width, -);
// Send EM_SETMARGINS to prevent text from disappearing underneath the button
SendMessage(this.Handle, 0xd3, (IntPtr), (IntPtr)(_button.Width << ));
}

调用时处理ButtonClick事件就行了。本身实现不难这里要记录的是下面这行代码的解释

SendMessage(this.Handle, 0xd3, (IntPtr)2, (IntPtr)(_button.Width << 16));

参数:0xd3   设置编辑控件的左、右边距

参数: 2  设置右边距

参数:_button.Width << 16

为什么要左移16位呢? 因为这个参数的高16位表示右边距,所以要先左移16位。

SendMessage 方法在控件开发中用的非常广泛,详细的解释和应用分析见随笔 http://www.cnblogs.com/zhaobl/p/3326332.html

上一篇:PHP源码分析-变量


下一篇:混搭.NET技术