WinForm:简单实现RichTextBox加载背景图

 

为RichTextBox加载背景图,尝试过不少方法,总结后,以下方法较为完善,且简单实现。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace RichTextBoxBackPicture
{
    public partial class UserRichTexBox : UserControl
    {
        public UserRichTexBox()
        {
            //为用户控制启用双缓冲等控件样式,否则RichTextBox输入时,会有残影
            this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer, true);

            InitializeComponent();
        }

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.alphaRichTextBox1 = new RichTextBoxBackPicture.AlphaRichTextBox();
            this.SuspendLayout();
            // 
            // alphaRichTextBox1
            // 
            this.alphaRichTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.alphaRichTextBox1.Location = new System.Drawing.Point(0, 0);
            this.alphaRichTextBox1.Name = "alphaRichTextBox1";
            this.alphaRichTextBox1.Size = new System.Drawing.Size(150, 150);
            this.alphaRichTextBox1.TabIndex = 0;
            this.alphaRichTextBox1.Text = "";
            // 
            // UserRichTexBox
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.alphaRichTextBox1);
            this.Name = "UserRichTexBox";
            this.ResumeLayout(false);

        }

        #endregion

        private AlphaRichTextBox alphaRichTextBox1;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //为测试效果,特加载背景图片
            this.BackgroundImageLayout = ImageLayout.Stretch;
            this.BackgroundImage = Image.FromFile(@"G:\Windows\Web\Wallpaper\Characters\img23.jpg");

        }
    }

    
    public class AlphaRichTextBox:RichTextBox 
    {
        //关键点:支持透明背景
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp= base.CreateParams;
                cp.ExStyle  |= 0x20;
                return cp;
            }
        }
    }
}


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值