码迷,mamicode.com
首页 > Windows程序 > 详细

C# 带滚动条的Label控件

时间:2014-11-17 17:49:51      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:label   控件   c#   textbox   

C# 带滚动条的Label控件,用鼠标选的时候还是有点闪烁:

namespace 带滚动条的Label控件
{
    public class TextBoxLabel : System.Windows.Forms.TextBox
    {
        [DllImport("user32", EntryPoint = "HideCaret")]
        private static extern bool HideCaret(IntPtr hWnd);

        [DllImport("user32", EntryPoint = "ShowCaret")]
        private static extern bool ShowCaret(IntPtr hWnd);

        public TextBoxLabel():base(){

            this.TabStop = false;
            this.SetStyle(ControlStyles.Selectable, false);
            this.Cursor = Cursors.Default;
            this.ReadOnly = true;
            this.ShortcutsEnabled = false;
            this.HideSelection = true;
            this.GotFocus += new EventHandler(TextBoxLabel_GotFocus);
            this.MouseMove += new MouseEventHandler(TextBoxLabel_MouseMove);
        }

        private void TextBoxLabel_GotFocus(Object sender, System.EventArgs e){
            if (ShowCaret(((TextBox)sender).Handle)){
                HideCaret(((TextBox)sender).Handle);
            }
        }

        private void TextBoxLabel_MouseMove(Object sender, MouseEventArgs e){
            if (((TextBox)sender).SelectedText.Length > 0){
                ((TextBox)sender).SelectionLength = 0;
            }
        }
    }
}

效果:

bubuko.com,布布扣

bubuko.com,布布扣

实现思路及用途参考:http://bbs.csdn.net/topics/390632325?page=1#post-398542672


C# 带滚动条的Label控件

标签:label   控件   c#   textbox   

原文地址:http://blog.csdn.net/testcs_dn/article/details/41211575

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!