TextBoxButton控件的开发实现

效果图:

实现代码:

 1    public TextBoxButton()

 2         {

 3             _button = new Button

 4             {

 5                 ForeColor = System.Drawing.SystemColors.GrayText,

 6                 Name = "button1",

 7                 Padding = new System.Windows.Forms.Padding(3, 1, 0, 0),

 8                 Text = "",

 9                 UseVisualStyleBackColor = true,

10                 Size = new System.Drawing.Size(20, 21),

11                 Dock = DockStyle.Right

12             };

13 

14             this._button.SizeChanged += (o, e) => OnResize(e);

15             this._button.MouseMove += (o, e) => { this.Cursor = Cursors.Default; };

16             this.Controls.Add(_button);

17         }

18 

19         protected override void OnResize(EventArgs e)

20         {

21             base.OnResize(e);

22             _button.Size = new Size(_button.Width, this.ClientSize.Height + 2);

23             _button.Location = new Point(this.ClientSize.Width - _button.Width, -1);

24             // Send EM_SETMARGINS to prevent text from disappearing underneath the button

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

26         }
View Code

调用时处理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

 

 

 

 

 

你可能感兴趣的:(button)