ホームページ  >  記事  >  バックエンド開発  >  C# スクロールバー付きラベルコントロールのサンプルコードを詳しく解説

C# スクロールバー付きラベルコントロールのサンプルコードを詳しく解説

黄舟
黄舟オリジナル
2017-03-13 17:49:203207ブラウズ

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;
            }
        }
    }
}

効果:



以上がC# スクロールバー付きラベルコントロールのサンプルコードを詳しく解説の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。