重叠半透明矩形

时间:2011-02-19 14:34:13

标签: c# winforms alphablending

是否可以绘制重叠的半透明(具有低alpha值),这不会累积alpha值?当我现在这样做时,如果在重叠区域我有一个alpha为50的矩形,重叠区域的alpha值将为100.

这是一个展示问题的示例程序 我添加了一个按钮,在同一个地方绘制矩形,增加了alpha值。

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

namespace Sample {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            richerTextBox1.MarkLine();
        }
    }


    public partial class RicherTextBox : RichTextBox
    {
        private const int WM_PAINT = 0x000F;
        private const int WM_VSCROLL = 277;
        private Graphics m_graphics = null;

        public void MarkLine()
        {
            if (m_graphics == null)
                m_graphics = this.CreateGraphics();

            int firstCharOfLine = this.GetFirstCharIndexFromLine(0);
            Rectangle marker = new Rectangle(0, 0, ClientSize.Width - 1, this.Font.Height);
            m_graphics.FillRectangle(new SolidBrush(Color.FromArgb(60, Color.Red)), marker);

        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_PAINT:
                    MarkLine();
                    break;
                case WM_VSCROLL:
                    MarkLine();
                    break;
            }

            base.WndProc(ref m);
        }
    } }

和设计师:

namespace Sample
{
    partial class Form1
    {
        /// <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 Windows Form 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.richerTextBox1 = new Sample.RicherTextBox();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // richerTextBox1
            // 
            this.richerTextBox1.Location = new System.Drawing.Point(22, 12);
            this.richerTextBox1.Name = "richerTextBox1";
            this.richerTextBox1.Size = new System.Drawing.Size(239, 183);
            this.richerTextBox1.TabIndex = 1;
            this.richerTextBox1.Text = "";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(168, 210);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(76, 26);
            this.button1.TabIndex = 2;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.richerTextBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }
        #endregion

        private RicherTextBox richerTextBox1;
        private System.Windows.Forms.Button button1;
    }
}

2 个答案:

答案 0 :(得分:1)

您不能以这种方式使用透明度绘制到窗口控件。合成只能在BeginPaint和EndPaint之间发生(或者在从位图创建的DC上)。

尝试自定义Windows富文本控件是可能的,但最终总是很糟糕。这就是为什么有那么多第三方文本编辑控件的原因。

我的建议是你问一个不同的问题,“我如何用不同的颜色在RichTextBox上绘制一条线的背景?”

或者获得第三方文本编辑器,它将为您提供此功能以及更多功能。

答案 1 :(得分:0)

目前仍然没有完全清楚发生了什么,特别是您的color是否不变等,但您可以尝试将m_graphics.CompositingMode属性设置为SourceCopy值。

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.compositingmode.aspx

相关问题