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